473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

"shared" object

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.getNextNumber(), 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
Dec 19 '05 #1
7 7332
In article <43***********************@dreader2.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.getNextNumber(), 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.)
Dec 20 '05 #2
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.updateCounter();

This should the job.

Warm Regards
Darko Topolsek

"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43***********************@dreader2.cybercity. 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.getNextNumber(), 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

Dec 20 '05 #3
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
Dec 21 '05 #4

"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43***********************@dreader2.cybercity. 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.getNextNumber(), 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
Dec 21 '05 #5

"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43***********************@dreader1.cybercity. 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 SingletonCounter {
private static final SingletonCounter soleInstance = new
SingletonCounter();

private SingleCounter() {
//Private constructor to prevent instantiation.
}

private int value = 1;

public getNextValue() {
return this.value++;
}

public static SingletonCounter getInstance() {
return soleInstance;
}
}

- Oliver
Dec 23 '05 #6
EricF wrote:
In article <43***********************@dreader2.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.getNextNumber(), 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
Dec 26 '05 #7
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. :)
Dec 28 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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...
2
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.
2
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...
6
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...
3
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...
1
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...
2
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...
3
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...
3
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()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.