wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Reading and writing to a Hashmap from other threads. Not sure how syncronizing works. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Reading and writing to a Hashmap from other threads. Not sure how syncronizing works." Watch "Reading and writing to a Hashmap from other threads. Not sure how syncronizing works." New topic
Author

Reading and writing to a Hashmap from other threads. Not sure how syncronizing works.

joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
(PLEASE answer in "little java words" for I am learning. <Very big smile)

In my main server thread it contains a queue (of out going packets) and a client ref hashmap with id's and the ref to each new client OBJ I create. Also, this same server generates a thread which handles the packets queue which then is distributed to the hashmaps (client ref).queues (probably more info then is needed for this question.)

Any who, I need my server dispatch thread to walk the client ref hashmap and plug in the packets into each client's queue.. this same hashmap is is added to each time a client connects.. (So I'm seeing a need to syncronize)

In the main server thread syncronizing the read and write methods to the hashmap is not how I want to do this right?

Somehow which I'm not sure of I need to syncronize my queue's and hashmap.

Anyone have a good article or some code snipets to explain how to do this.

Man I was so pleased with myself How well I was creating threads and having them work sending packets back and forth to my client. Then I started thinking....

P.S. I created a 1 does all method for my queues that I think is safe code listed below...








Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


1. hashmap is not synchronized so it must be operated in single thread environment.
2. if you want to use hashmap with more than one thread consider using synchronized hashmap or Concurrent hashmap.
3. you can get synchronized hashmap by calling Collections.synchronizedMap() method.

you may also find my blog post about How HashMap works in Java interesting.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
Thanks, I converted it to a list which seems to be working fine.

One related question, if I call anyone of the synchronized methods which work with the list in an object, all the other synchronized methods are blocked correct?

thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

joe vasher wrote:One related question, if I call anyone of the synchronized methods which work with the list in an object, all the other synchronized methods are blocked correct?


If they are synchronizing on the same object, then yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading and writing to a Hashmap from other threads. Not sure how syncronizing works.
 
Similar Threads
Would writing my ownlock routines for hashmap and queue reads and writes be a better solution to syn
Threads and Sockets
Creating inputstream from MulticastSocket
Reading and writing to a socket concurrently
Why notify/notifyAll() method doesnt invoke a waiting thread quickly?????