File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Adding values to a hashtable from a different class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding values to a hashtable from a different class" Watch "Adding values to a hashtable from a different class" New topic
Author

Adding values to a hashtable from a different class

bharath sina
Greenhorn

Joined: Dec 20, 2009
Posts: 8
Hi,

I am new to Java Programming.
I have two classes. One has a static hashtable and another class on the same machine trying to add values to the database. Kindly tell me if I can implement this using Singleton Pattern.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
That description is pretty sketchy, you need to fill in some details.

What database does "add values to the database" refer to?

Are these two classes being executed in separate JVM instances?

I see no need to be talking about Patterns before we understand more about your problem.

Bill
bharath sina
Greenhorn

Joined: Dec 20, 2009
Posts: 8
Hi,

Sorry for the mistake. What I meant was that I have a class which maintains a hashtable. Another class in the same package running on the same JVM tries to add data to that hashtable. There are like many instances of the second class and all they do is just add data to the hashtable. Please suggest a way of implementing this.

Thanks in advance,

Bharath.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Since you are only talking about a single JVM, this question does not belong to "distributed java" forum.
Moving to "java in general"


apigee, a better way to API!
Kaustav Ganguly
Greenhorn

Joined: Dec 14, 2009
Posts: 28
I dont think singleton implementation is required if the only requirement is to have a hashtable with global scope. You just need a static final hashtable.
bharath sina
Greenhorn

Joined: Dec 20, 2009
Posts: 8
Thanks for the reply,

I have tried that but it did not work. Its like this,

Lets say ClassA has a hashtable(static final as you said) and ClassB or rather various instances of ClassB try and add to that hashtable. ClassA has a thread which is waiting for any changes to the hashtable. I have tried using a static hashtable but the changes were only seen from ClassB instance which actually added the value but not in ClassA and neither from a different class.

Probably I have some difficulty grasping the whole lifetime of static variables and it would be really great if you can help.
Kaustav Ganguly
Greenhorn

Joined: Dec 14, 2009
Posts: 28
I need to see what you are acutually trying to acheive in the code , only then I would be able to help you out. Could you please provide the code snippet for class A and B ?
bharath sina
Greenhorn

Joined: Dec 20, 2009
Posts: 8


ClassB




As I said, the size in ClassA is not changing even after ClassB is adding to the Hashtable.

Thanks for your concern,
Kaustav Ganguly
Greenhorn

Joined: Dec 14, 2009
Posts: 28
I dont see any problem in the code it should work perfectly. The output for ClassA is "size 3" which gets repeated after every 1000 ms and for ClassB it should be 3 and 4. I ran both as Java application and the output were perfect. The size of the hashtable is increasing as I observed in the output of ClassB.
bharath sina
Greenhorn

Joined: Dec 20, 2009
Posts: 8
Exactly, it increases in ClassB but not when checked from ClassA, i mean since the size has changed, isnt ClassA supposed to show size as 4 after ClassB has added??
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

bharath sina wrote:Exactly, it increases in ClassB but not when checked from ClassA, i mean since the size has changed, isnt ClassA supposed to show size as 4 after ClassB has added??


ClassA and ClassB run in their own JVMs (Since you have a main method in both the classes I assume you run these as > java ClassA and > java ClassB), no data is shared between JVMs unless you make provisions to do so.
bharath sina
Greenhorn

Joined: Dec 20, 2009
Posts: 8

ClassA and ClassB run in their own JVMs

Thanks for replying.
Yes. Just realized that they are separate process and not different threads in a single JVM.


no data is shared between JVMs unless you make provisions to do so


Can you please tell me how to do this? Is RMI a solution?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38478
    
  23
Sockets may be an easier way to share information.

I fail to see what relation there is between Maps (HashMap is probably now a better class) and singletons. Why do you want a singleton in the first place?
bharath sina
Greenhorn

Joined: Dec 20, 2009
Posts: 8
As I have said, I was under the wrong impression that those two classes share a common address space or at least ClassB can access and change the HashMap/Hashtable in ClassA which is reflected on ClassA as well(I was using Singleton for this). Then I tried static Hashtable which did not work and decided to give Singleton and RMI a try.
There seems to be no simple way of doing this(without rmi/sockets). Can I try persisting those objects(the ones I was thinking of putting in a hashtable) in a file(no db)?

But I read somewhere that one cannot append objects to a file..

Really confused. Kindly suggest me the best possible way out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding values to a hashtable from a different class