2013年11月14日星期四

Use ConcurrentHashMap appear strange problem

Using scene is multi-threaded , as follows:
ConcurrentHashMap map = initMap () / / Assuming the already initialized .
Iterator iterator = map.values ​​(). iterator ();
while (iterator.hasNext ()) {
Object o = iterator.next ();
dosomething (o) ;/ / (1) here will burst nullpointerexception
}
the other threads will map ( the same ) operation , add, remove and the like.

According to my understanding :
1.ConcurrentHashMap is not allowed to have a null value , either key or value
2.ConcurrentHashMap.values ​​(). iterator (), is the view of the iterator , the official document which describes other threads on the map changes will not affect the iterator 's iteration . That is, even if the external threads modifies the map, at this moment the thread is normal iterator
Based on the above two points , map.values ​​(). iterator (). next () will not be any null values ​​, that o is impossible null values ​​.
but indeed in ( a ) where a null pointer burst .

seeking expert answers, breaking three of my view of it. . . . . . .
------ Solution ---------------------------------------- ----
values
public Collection values ​​() returns the values ​​contained in this map Collection view . the collection backed by the map , so changes to the map are reflected in this collection , and vice versa. The collection supports element removal , which through Iterator.remove, Collection.remove, removeAll, retainAll and clear operation removes the corresponding mapping from the mapping . It does not support the add or addAll operations.
The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse existed upon construction of the iterator element , in addition to possible ( but not guaranteed ) reflect any modifications subsequent to construction .


Specified by:
The values ​​in interface Map
coverage :
class AbstractMap of values
Returns:
this map collection view of the values ​​contained in


may be the reason it
------ Solution ------------------------------- -------------
Iterator iterator = map.values ​​(). iterator ();
ConcurrentHashMap in values ​​can be NULL!
------ Solution ---------------------------------------- ----
if it is in ( a ) where the explosion , you are not the look dosomething is doing it . .
------ Solution ---------------------------------------- ----

public class Microphone {
private static final Logger LOG = LoggerFactory.getLogger(Microphone.class);
public String s = "111";
public static void main(String[] args) {
ConcurrentHashMap map =  new ConcurrentHashMap();
Microphone  c = new Microphone();
map.put(2, c);
c = null;
Iterator<Object> iterator=map.values().iterator();
while(iterator.hasNext()){
Object o=iterator.next();
System.out.println( c.s );
}
}
}

------ For reference only ---------------------------- -----------
there will not be a burst inside other objects null pointer ?
------ For reference only -------------------------------------- -


No, that is o burst abnormal
------ For reference only ---------------------------- -----------


No, that is o itself is null
------ For reference only --------------------------- ------------
seemingly not right , jdk that part explain quite a mouthful
------ For reference only ----------- ----------------------------

this wrong sorry
----- - For reference only ---------------------------------------
public class Microphone {
private static final Logger LOG = LoggerFactory.getLogger(Microphone.class);
public Son son = new Son();
class Son{
public String sss = "33";
}

public static void main(String[] args) {
ConcurrentHashMap map =  new ConcurrentHashMap();
Microphone  c = new Microphone();
map.put(2, c);
Microphone o = (Microphone) map.get(2);
o.son = null;
Iterator<Object> iterator=map.values().iterator();
while(iterator.hasNext()){
Microphone d= (Microphone)iterator.next();
System.out.println( d.son.sss );
}
}
}
< br> ------ For reference only ---------------------------------------


If this process is to follow your My case is that d is a null directly , rather than son is null.
------ For reference only -------------------------------------- -


It seems clear enough I said , I understand that it write

ConcurrentHashMap map = initMap () / / Assuming the already initialized .
Iterator iterator = map.values ​​(). iterator ();
while (iterator.hasNext ()) {
Object o = iterator.next ();
if (o! = null) {
dosomething (o) ;/ / (1) where will burst nullpointerexception, attention is o is null, not its member variables appear inside null.
}
}
------ For reference only --------------------------------- ------

Tell me what my problem has been resolved. Now I can confirm that from ConcurrentHashMap out null value is not likely the case , this by its own characteristics identified , put the time will detect . Also in map.values ​​(). Iterator () to iterate when other threads on ConcurrentHashMap modifications are possible reflected in the iterator 's


没有评论:

发表评论