• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding values to a hashtable from a different class

 
bharath sina
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are only talking about a single JVM, this question does not belong to "distributed java" forum.
Moving to "java in general"
 
Kaustav Ganguly
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic