• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

race condition in a hashmap

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to get some examples of race condition in hashmap. One example I found is that if 2 thread try to do a put then the array resizing can cause an infinite loop which I understand completely.(http://mailinator.blogspot.com/2009/06/beautiful-race-condition.html)
But I am trying to think of another example of a race condition in a hashmap and how a concurrenthashmap fixes the issue?

Similarly for a queue interface, linkedlist is considered as a non thread safe queue and concurrentlinkedqueue is considered as thread safe. I am trying to find an example of a race condition in linkedlist and how it is solved by using a concurrentlinkedqueue.
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

andy kumar wrote:
I am trying to think of another example of a race condition in a hashmap and how a concurrenthashmap fixes the issue?



A race condition is simply multiple threads accessing the same data in parallel -- and as such, there is a race on doing the tasks simultaneously.

A race condition does *not* always mean that there is an issue. It is perfectly fine to have a race condition and for it to still be thread safe.


So, your question can't really be directly answered -- as you don't necessary fix race conditions. You have to fix issues (thread safety) that are related to race conditions.

Henry
 
andy kumar
Ranch Hand
Posts: 64
  • 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 think I did not ask the question properly. What I am looking for is an example where if i have a hashmap/queue which is shared by more than 2 threads how it will cause an issue and if I replace it with concurrenthashmap/concurrentlinkedqueue, how does it fix it. I know this is a very basic question but any help would be appreciated.
 
Henry Wong
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

andy kumar wrote:What I am looking for is an example where if i have a hashmap/queue which is shared by more than 2 threads how it will cause an issue and if I replace it with concurrenthashmap/concurrentlinkedqueue, how does it fix it. I know this is a very basic question but any help would be appreciated.



There are countless things that two threads can do with a collection. And hence, there are countless things that could happen due to race conditions. Of these countless possible outcomes, some will be fine and not need synchronization, some will not be fine and will need thread safe collections like the concurrent hashmap, and some, will not be thread safe, even with the use of a concurrent hashmap.

How will examples help you understand it? Especially, since concurrent hashmaps may not work in every case?

Henry
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may not be exactly what the OP wanted. But still is a good read.

http://mailinator.blogspot.in/2009/06/beautiful-race-condition.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic