expectation:
Map each key maintains a singleton LinkedBlockingQueue
problem points:
how to ensure thread safety during initialization? double locking does not work due to visibility reasons. Ask God for advice on whether you can get a thread safe object when map.get (key) is empty, so as to avoid the problem of data inconsistency caused by multiple initialization
the case code is as follows:
Map<String, LinkedBlockingQueue<Map>> DATA_QUEUE = new ConcurrentHashMap<>();
Object synlock = new Object();
@Test
public void test1(){
new Thread(()-> produceData("1","2")).start();
new Thread(()-> produceData("1","3")).start();
new Thread(()-> produceData("1","4")).start();
new Thread(()-> produceData("1","5")).start();
new Thread(()-> produceData("1","6")).start();
new Thread(()-> produceData("1","7")).start();
System.out.println(DATA_QUEUE);
}
public void produceData(String key, Object data) {
LinkedBlockingQueue queue = DATA_QUEUE.get(key);
if (queue == null) {
synchronized (synlock){
if(queue == null){
queue = new LinkedBlockingQueue();
System.out.println("init======"+queue.hashCode());
}
}
}
try {
queue.offer(data, 500, TimeUnit.MILLISECONDS);
DATA_QUEUE.put(key, queue);
} catch (InterruptedException e) {
System.out.println("error");
}
}