Package com.jay.test.map;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.concurrent.ConcurrentHashMap;
public class Maptest {public
static void Main (string[] args) {
final map<integer, integer> Map = new Hashma P<integer, integer> (); final Map<integer, integer> Map = new concurrenthashmap<integer,integer> ();
New Thread (New Runnable () {public
void run () {
map.put (1, 1);
}
}). Start ();
New Thread (New Runnable () {public
void run () {
map.put (2, 1);
}
}). Start ();
New Thread (New Runnable () {public
void run () {
map.put (3, 1);
}
}). Start ();
SYSTEM.OUT.PRINTLN (map);
}
Today on the Internet to see the next multithreading use HashMap will have problems, because HashMap is not thread-safe, multithreading or use Concurrenhashmap this than Hashtable
"Thread-safe" is more efficient. Problems caused by
1, the probability of loss of data, the above program runs, 40% of the probability of loss of data.
{1=1, 2=1}
2, there is the probability of abnormal information, 10% bar.
Exception in thread "main" java.util.ConcurrentModificationException
At Java.util.hashmap$hashiterator.nextentry (Unknown Source)
At Java.util.hashmap$entryiterator.next (Unknown Source)
At Java.util.hashmap$entryiterator.next (Unknown Source)
At java.util.AbstractMap.toString (Unknown Source)
At Java.lang.String.valueOf (Unknown Source)
At Java.io.PrintStream.println (Unknown Source)
At Com.jay.test.map.MapTest.main (maptest.java:28)
In view of the above problems or use Concurrenthashmap bar.