I am not very clear about your question. Are you asking that multiple threads needs to access the same HashMap? (If so, then HashMap is not thread-safe so you must use some other data structure like ConcurrentHashMap).
OR
You are asking that there is a single HashMap that needs to be accessed from multiple classes(not essentially from different threads)
(I am not sure what is the problem you can face here. If you send us the code you have written, we can help you better)
As I understand your requirement, there is a hashmap that is accessed and modified by several instances of(same or different) classes. And I think you might also need to ensure that the modifications to the hashmap are synchronized so that different threads accessing the map, see inconsistent states of the hashmap.
One approach could be do write a class containing his hashmap as its member and methods to access and modify the hashmap. The other classes can call the methods on this class to access the hashmap. These methods would need to be syncronized so that the map modifications are atomic.You could consider using ConcurrentHashMap for this.
Thanks and Regards
Mohamad Obagi
Ranch Hand
Joined: Oct 25, 2008
Posts: 37
posted
0
yes guys what satya said is what i want
but my problem is if i created an object of the hashmap and initialized inside a class not in the Main how can other classes use this object without initializing it again
Satya: One approach could be do write a class containing his hashmap as its member and methods to access and modify the hashmap. The other classes can call the methods on this class to access the hashmap. These methods would need to be syncronized so that the map modifications are atomic.
1) Put a reference to the HashMap somewhere where all your different classes can get it. For example, store a reference to it in a static member variable:
Now any code can access that HashMap as "BadExample.badIdea". Hopefully you can tell from my class and variable names that this isn't my recommended way of doing this!
2) The other way is to give a reference to the HashMap to any object that needs it. For example, if your main() method calls a method that needs the HashMap, then pass it as an argument. If your main() method creates an object that will later need the HashMap, then pass the HashMap as a constructor parameter, and store it in an instance variable.
I'm going to move this to Java in General (Beginner), as it really has nothing to do with Threads and Synchronization.
Are you trying to share your HashMap between two separate JVMs? i.e., are there two different separately-launched client programs both expecting to share data via this HashMap? Because that won't work.
Otherwise, I'm going to need to see some code.
Mohamad Obagi
Ranch Hand
Joined: Oct 25, 2008
Posts: 37
posted
0
trying to share a hash map between 2 different classes
here the code its scratch)
Than inside this class there is a function that create an object of IM
Now when i create another instance of instantMessaging on the receive of a message
i am unable to use Mapaccess object it give me the error i stated
i tried to create a new object of hash map and copy them but also it gave me the same error!
Mohamad Obagi
Ranch Hand
Joined: Oct 25, 2008
Posts: 37
posted
0
i defined the hash map as static and it worked fine when i create many object of the hash map class they all contain same result
however i am still having this error at :
window.ReceivedM(); //calls
if i put this map2.wmap.get(address2) into the first object of IM window it works fine!!!
Mohamad Obagi
Ranch Hand
Joined: Oct 25, 2008
Posts: 37
posted
0
ok i fixed it
i created an internal hash map and copied everything to it
Satya Maheshwari
Ranch Hand
Joined: Jan 01, 2007
Posts: 368
posted
0
Hi Mohamad Could you please explain about what was the exact problem and how did you resolve so that we can all learn from it? Maybe you can quote a simple code example to explain instead of the actual code where you faced the problem.
Mohamad Obagi
Ranch Hand
Joined: Oct 25, 2008
Posts: 37
posted
0
Ok satya
what i was doing is the following:
I used a static HashMap in a class called UserHashMap and i created an object inside a class called MainUI(inside this class there is an InstantMessagingUI object)
generally from this class MainUI i get to select a user and save this user IP inside the HashMap( the Hash Map is static so it will not reset each time i call an object since its a threaded program)
than in another class called instantMessagingUI i created a local HashMap which each time is called it creates a copy of the UserHashMap and save it in its local HashMap so it will be able to use it.
some of you might say why didnt you call another object of Hashmap since its static!
the main reason was cause when you are calling UserHashMap using 2 different object of another class it will create a new copy for each object!
here some code:
i already wrote the userhashmap class just change it to static and synchronized map. thats all