• 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

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

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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...








 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
joe vasher
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic