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).
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)
Not really a Threads & Synchronization question. Please CarefullyChooseOneForum
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.
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.
This is what Collections.synchronizeXXX() methods give you.
In general, there are two ways to approach this.
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.
i am getting an error pool 3-1 thread null....
when i passed this object to 2 objects of same Class
basically what i am trying to do
is the following( its a peer 2 peer chat)
when i open window the hash map store the address and store true as value
till now i am able to control how many window i can open for each user
but my problem is that when i receive a message a window pop up! so using get function hash map i wanna know if its already open or no!
when i am accessing the hash map in this case i am getting a null error
dont know if there is another way to do it
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!
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 :
if i put this map2.wmap.get(address2) into the first object of IM window
it works fine!!!
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.