Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

"shared" object

Thread view: 
Carsten H. Pedersen - 19 Dec 2005 15:11 GMT
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
EricF - 20 Dec 2005 05:00 GMT
>Hello
>
[quoted text clipped - 15 lines]
>
>- 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.)
Edwin Castro - 26 Dec 2005 12:37 GMT
>> Hello
>>
[quoted text clipped - 18 lines]
> 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
Carsten H. Pedersen - 28 Dec 2005 15:06 GMT
> 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. :)
Darko Topolsek - 20 Dec 2005 13:03 GMT
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

> Hello
>
[quoted text clipped - 15 lines]
>
> - Carsten H. Pedersen
Carsten H. Pedersen - 21 Dec 2005 09:00 GMT
> public class Counter extends .... {
>     public Counter() {
[quoted text clipped - 4 lines]
>         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
Oliver Wong - 23 Dec 2005 18:58 GMT
> 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
[quoted text clipped - 30 lines]
>
> 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
Silvio Bierman - 21 Dec 2005 12:25 GMT
> Hello
>
[quoted text clipped - 15 lines]
>
> - 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.