Java initialization thread safety issues

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");
        }
    }
Mar.31,2021

if (queue == null) {
    synchronized (synlock){
        if(queue == null){
            queue = new LinkedBlockingQueue();
            DATA_QUEUE.put(key, queue);
            System.out.println("init======"+queue.hashCode());
        } else {
            queue = DATA_QUEUE.get(key);
        }
    }
}

I think it should be like this. If you put DATA_QUEUE.put (key, queue); outside, there is no point in locking it.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e7d2-2bc6e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e7d2-2bc6e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?