Hello
I want to create a kind of shared object. I have created a class,
Counter, which you can then import. Through the Counter class, i would
like to have access to a shared object.
Lets say that Counter contains only an int, initialised to 1. Each
time a class importing Counter would do Counter.getNext Number(), the
getNextNumber() method would increase the local int, and return the
next number.
What i want now is when an entirely different class, also importing
Counter, calls the getNextNumber() method, it does not start out from
1, but keeps counting from where another class left off.
I thought of maybe just writing the number to a file, and updating it
each time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen 7 7346
In article <43************ ***********@dre ader2.cybercity .dk>, "Carsten H. Pedersen" <no@nono.no> wrote: Hello
I want to create a kind of shared object. I have created a class, Counter, which you can then import. Through the Counter class, i would like to have access to a shared object.
Lets say that Counter contains only an int, initialised to 1. Each time a class importing Counter would do Counter.getNext Number(), the getNextNumber( ) method would increase the local int, and return the next number.
What i want now is when an entirely different class, also importing Counter, calls the getNextNumber() method, it does not start out from 1, but keeps counting from where another class left off.
I thought of maybe just writing the number to a file, and updating it each time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen
Make the counter int static. Assuming this is a simple POJO, it should work.
(All accesses to the class are using the same JVM.)
Hi Carsten.
public class Counter extends .... {
public Counter() {
super(...);
}
private void updateCounter() {
count += 1;
}
public static int count = 1;
}
class AnotherClass extends ... {
public AnotherClass()
super(...);
}
Counter counter = new Counter();
counter.updateC ounter();
This should the job.
Warm Regards
Darko Topolsek
"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43******** *************** @dreader2.cyber city.dk... Hello
I want to create a kind of shared object. I have created a class, Counter, which you can then import. Through the Counter class, i would like to have access to a shared object.
Lets say that Counter contains only an int, initialised to 1. Each time a class importing Counter would do Counter.getNext Number(), the getNextNumber() method would increase the local int, and return the next number.
What i want now is when an entirely different class, also importing Counter, calls the getNextNumber() method, it does not start out from 1, but keeps counting from where another class left off.
I thought of maybe just writing the number to a file, and updating it each time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen
Darko Topolsek wrote: public class Counter extends .... { public Counter() { super(...); }
private void updateCounter() { count += 1; }
Shouldn't this be public? How else can it be accessed from the
outside? Or did i miss some trick? :)
[...]
Thanks for responding, but this doesn't quite do the trick. I think i
have to elaborate on what i want. From what i wrote previously, i
don't think it came out clearly.
I have a package, foo, containing the Counter class. Now i have a
class, A, which does: import foo.Counter;
A has a main method, which starts an instance of itself, wrapped in a
Thread. The Thread works as a ticker, counting the shared object
Counter up one.
Now i start another instance of A, from another shell. Since this does
the exact same thiing as the first A, it also imports foo.Counter.
What i want is for the second instance to start counting from where
the first instance was at when the second was started.
An example printout would be like this:
(Start A1, first instance of A, with java A)
A1: count: 1
A1: count: 2
A1: count: 3
(start A2, second instance of B, in another shell, with java A)
A2: count: 4
A1: count: 5
A2: count: 6
A1: count: 7
And so on...
From what i tried, A2 just starts counting from 1, and A1 just keeps
counting.
Any suggestions on how to crack this one? :)
- Carsten H. Pedersen
"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43******** *************** @dreader2.cyber city.dk... Hello
I want to create a kind of shared object. I have created a class, Counter, which you can then import. Through the Counter class, i would like to have access to a shared object.
Lets say that Counter contains only an int, initialised to 1. Each time a class importing Counter would do Counter.getNext Number(), the getNextNumber() method would increase the local int, and return the next number.
What i want now is when an entirely different class, also importing Counter, calls the getNextNumber() method, it does not start out from 1, but keeps counting from where another class left off.
I thought of maybe just writing the number to a file, and updating it each time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen
Stay away from static instances/variables, in spite of the earlier
responses. Although this is always a good idea the fact that you want
separate sequences implies that you should go for multiple insatnces of
sequence generators. If you could construct such a generator with an initial
sequence value (as opposed to 1) it would be easy to spawn sequences.
Silvio Bierman
"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43******** *************** @dreader1.cyber city.dk... Thanks for responding, but this doesn't quite do the trick. I think i have to elaborate on what i want. From what i wrote previously, i don't think it came out clearly.
I have a package, foo, containing the Counter class. Now i have a class, A, which does: import foo.Counter;
A has a main method, which starts an instance of itself, wrapped in a Thread. The Thread works as a ticker, counting the shared object Counter up one.
Now i start another instance of A, from another shell. Since this does the exact same thiing as the first A, it also imports foo.Counter. What i want is for the second instance to start counting from where the first instance was at when the second was started.
An example printout would be like this:
(Start A1, first instance of A, with java A) A1: count: 1 A1: count: 2 A1: count: 3 (start A2, second instance of B, in another shell, with java A) A2: count: 4 A1: count: 5 A2: count: 6 A1: count: 7
And so on...
From what i tried, A2 just starts counting from 1, and A1 just keeps counting.
Any suggestions on how to crack this one? :)
Your example is completely different from the explanation of your first
post, and completely different from your explanation in this post. I will
ignore both explanations and assume what you want is what the example shows.
In that case, you want to use the Singleton design pattern.
public class SingletonCounte r {
private static final SingletonCounte r soleInstance = new
SingletonCounte r();
private SingleCounter() {
//Private constructor to prevent instantiation.
}
private int value = 1;
public getNextValue() {
return this.value++;
}
public static SingletonCounte r getInstance() {
return soleInstance;
}
}
- Oliver
EricF wrote: In article <43************ ***********@dre ader2.cybercity .dk>, "Carsten H. Pedersen" <no@nono.no> wrote: Hello
I want to create a kind of shared object. I have created a class, Counter, which you can then import. Through the Counter class, i would like to have access to a shared object.
Lets say that Counter contains only an int, initialised to 1. Each time a class importing Counter would do Counter.getNext Number(), the getNextNumber() method would increase the local int, and return the next number.
What i want now is when an entirely different class, also importing Counter, calls the getNextNumber() method, it does not start out from 1, but keeps counting from where another class left off.
I thought of maybe just writing the number to a file, and updating it each time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen
Make the counter int static. Assuming this is a simple POJO, it should work. (All accesses to the class are using the same JVM.)
This would have worked if you were using the same JVM as EricF
suggested. A later message mentions that you are creating a second
object in a second shell indicating to me that you are using two JVM
instances. This approach doesn't work because each JVM instance has its
own environment and they are not shared.
What you want is that when a second "object" is "created" it really
accesses the first object created regardless of whether that happens in
the same JVM or not. What about if you create a Counter object in a
second computer all together? Should it connect to the first Counter
object created (on some other unknown computer)?
You are trying to solve a very hard problem here. As ugly a solution as
using the file system as memory might be the easiest solution for single
computer processing. Make sure you use appropriate locking mechanisms
since two JVM processes (and hence two instances of Counter) could try
to access the file at the same time.
You also don't know when the Counter object "dies". Is the Counter alive
only when there are instances of it alive or is it alive regardless? You
have a lot of things to think about here.
RMI, Beans, CORBA, COM, .NET, web services, etc. all exists as
frameworks to help formulate solutions to this very hard problem. Most
of these (if not all) will probably be too complex for what you are
doing. Then again, locking access to a file is hard as well.
--
Edwin
Edwin Castro wrote: RMI, Beans, CORBA, COM, .NET, web services, etc. all exists as frameworks to help formulate solutions to this very hard problem. Most of these (if not all) will probably be too complex for what you are doing. Then again, locking access to a file is hard as well.
I'm looking into RMI now, and it seems to be able to do exactly what i
want. Thanks for the pointer. :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Pro Grammer |
last post by:
Hello, all,
I am not sure if this is the right place to ask, but could you kindly
tell me how to "load" a shared object (like libx.so) into python, so
that the methods in the .so can be used? That too, given that the shared
object was written in c++, compiled with g++ ?
Thanks,
Pro Grammer
|
by: santa19992000 |
last post by:
Confusing th eword with "library", "shared library" and how to use
these things in real C project, is there any small example I can take a
look. Thanks.
|
by: Jenna Schmidt |
last post by:
I know that one of the benefits of using "Shared" methods
is you do not explicitly have to Dim as New object to
access the method.
Are there some other implications with memory and
concurrency issues with lets say 25-50 concurrent users?
What are the pros and cons of using this type of method?
Thanks so much,
|
by: Ross |
last post by:
MyWebProject.MyWebForm1.someset.somedata is a datatable
within a dataset. Displays quite nicely, too. Now I want
to use the same data in MyWebProject.MyWebForm2. Being a
old, er, experienced Java programmer, I thought I could
write this as a member of MyWebForm1:
Public Shared Function getSomeData() As DataTable
getSomeData = somedata
End Function
|
by: Henri |
last post by:
Hi,
Sorry for my bad English.
Could you tell me if the context of a member declared as Shared is limited
to the instance of the page the object that owns this member is declared in,
or does it go to the whole application?
I mean: if one user accesses a page that updates the shared member of this
object, will it affect all the users of the application or not?
Thanks!
| |
by: David Sanschagrin |
last post by:
(I previously posted this problem on vb.general.discussion but I've been told
that this question is more related to VB.NET than VB6 and so that I should
post that here.)
I'm trying to call a "Public Shared" function in a dll built in VB.NET from a
VB6 program.
Whenever I try, I get an error saying that the function doesnt exist. I had
doubts that the "Shared" in the VB.NET dll could be the problem. I tried to
|
by: HmFireBall |
last post by:
Hi,
This is a question about a shared property in ASP.NET
Imagine there's an assembly called my.dll that is in the /bin directory
of several websites.
This assembly contains a class with a shared property called
MySharedProp. Its value is initialized inside the shared constructor of
the class and should be based on values declared in the web.config of
|
by: Kenneth Kahl |
last post by:
Hello,
I would like to call a C++ programm out of Java with help of JNI. By the
followed command I created a "shared library":
g++ -shared -o libcalculate.so rechner.cpp
When I create an object from the existing program inside a method of my
class rechner.cpp, and then call the method out of java, a following
|
by: jbeteta |
last post by:
Hello,
I have a problem declaring variables. I need to create an object oRpte
as ReportClass on WebForm1.aspx and be able to use its value on
WebForm2.aspx.
For declaring the property oRpte() on WebForm1.aspx, I use "Public
Property" and I declare variable _oRpte as Friend Shared. That's my
problem. If I don't declare _oRpte as Friend Shared, I can't use
WebForm1.oRpte() on other webpage. If I declare _oRpte as Friend
Shared, I can use...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |