• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Is HashMap.put() atomic?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a Question. Consider that i have a global HashMap hashMap1. If 1 execute the below statment, it will insert a new entry with key = "k1" (If the key wasnt there) or it will replace the value for "k1" with hashMap2, if the key "k1" is already there.
hashMap1.put("k1",hashMap2);
Thus I assume that during the time when hashMap1.put("k1",hashMap2); is being executed there would be no time when hashMap1.doesnt contain the key "k1".
To make the question clearer, can i assume that
hashMap1.put("k1",hashMap2) is an atomic operation, i.e hashMap1.put() doesnt internally consist of a remove() and then put(). If it internally consist of a remove() and then put(), then for the small inverval between the remove() and put(), the key "k1" wont have a value.
Please let me know
 
Marshal
Posts: 28296
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
No, you cannot assume that will be an atomic operation. The Java language specification describes what operations are atomic, and they are all simple things like assigning an int. Complex things like what you describe are not atomic. If you need them to be, then you need to synchronize access to those operations.
 
author
Posts: 23956
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

If it internally consist of a remove() and then put(), then for the small inverval between the remove() and put(), the key "k1" wont have a value.



No... it is not atomic. And it is worse than you think. During a small interval during the operation, the interal references of the hashmap may not even be valid. If you try to get(), while a put() is happening, you can get k1, or you can not get k1, or the map may just not work.

Henry
[ November 06, 2007: Message edited by: Henry Wong ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if the put() happens to cause the HashMap to resize itself and copy its contents into a new, larger array, then you can get some completely nonsensical results, including NullPointerException or ArrayIndexOutOfBounds. If you want to use a HashMap from multiple threads, you must either (a) not change its data, (b) use synchronization, or (c) use a ConcurrentHashMap from JDK 5 or later. Using synchronization may also be accomplished by using Collections.synchronizedMap() or using Hashtable, but in each of these cases you may need additional synchronization - do not assume that just because the individual methods are synchronized, that all problems are solved.
 
Joe Joseph
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked up the code begin HashMap.put(). I have give the code below




From the statement "e.value = value;", isnt it apparent that will either have the old value or the new value at any point of time??

[ added code tags - Jim ]
[ November 06, 2007: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Joe]: From the statement "e.value = value;", isnt it apparent that will either have the old value or the new value at any point of time??

At that instant, yes. But there are plenty of things that could have gone wrong before that, and a few things that can go wrong after that. If a rehash is occurring, then the entire chain of enries for th at bucket (all the entries obtained by following e.next) will be reprocessed and put in different buckets. So while following that chain you could start in one bucket and end up in another, while the entry you're looking for could end up in yet a different bucket. So you'd never find the entry you're looking for, even though it's in the hashmap.
[ November 06, 2007: Message edited by: Jim Yingst ]
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic