Description
The synchronizedMap() method is used to return a synchronized (thread-safe) map backed by the specified map.
Declaration
Following is the declaration for java.util.Collections.synchronizedMap() method.
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
Parameters
m − This is the map to be "wrapped" in a synchronized map.
Return Value
-
The method call returns a synchronized view of the specified map.
Collections.synchronizedMap()与ConcurrentHashM两者都提供了线程同步的功能
package com.ldd;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class Functiontest {
private static Map<String, Object> map1 = new HashMap<String, Object>();
private static Map<String, Object> map2 = new Hashtable<String, Object>();
private static Map<String, Object> map3 = new ConcurrentHashMap<String, Object>();
private static Map<String, Object> map4 = Collections.synchronizedMap(new HashMap<String, Object>());
private static Map<String, Object> map = map4;
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (map.size() > 0) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(String.format("%s: %s", entry.getKey(), entry.getValue()));
}
// Iterator<Map.Entry<String, Object>> iterator = map4.entrySet().iterator();
// while(iterator.hasNext()){
// Map.Entry<String,Object>entry=iterator.next();
// System.out.println("key= "+entry.getKey()+" , "+entry.getValue());
}
map.clear();
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
map.put("key" + i, "value" + i);
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
Exception in thread "Thread-0" java.util.ConcurrentModificationException
Collections.synchronizedMap()
guarantees that each atomic operation you want to run on the map will be synchronized.
Running two (or more) operations on the map however, must be synchronized in a block.
使用synchronizedMap() 没报错,但缺失一部分数据
加上锁:
package com.ldd;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class Functiontest {
private static Map<String, Object> map1 = new HashMap<String, Object>();
private static Map<String, Object> map2 = new Hashtable<String, Object>();
private static Map<String, Object> map3 = new ConcurrentHashMap<String, Object>();
private static Map<String, Object> map4 = Collections.synchronizedMap(new HashMap<String, Object>());
private static Map<String, Object> map = map3;
private static int count=0;
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
synchronized (map) {
map.put("key" + i, "value" + i);
}
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (map) {
if (map.size() > 0) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
count++;
System.out.println(String.format("%s: %s %d", entry.getKey(), entry.getValue(), count));
}
}
// Iterator<Map.Entry<String, Object>> iterator = map4.entrySet().iterator();
// while(iterator.hasNext()){
// Map.Entry<String,Object>entry=iterator.next();
// System.out.println("key= "+entry.getKey()+" , "+entry.getValue());
map.clear();
}
try {
Thread.sleep((new Random().nextInt(10) + 1) * 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
https://www.tutorialspoint.com/java/util/collections_synchronizedmap.htm