• Post Reply Bookmark Topic Watch Topic
  • New Topic

race condition in a hashmap

 
andy kumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 22526
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 22526
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!