• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding values to a hashtable from a different class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic