多线程的lock和condition为什么锁没有被释放

public static void main(String[] args) throws InterruptedException {
//        Producer producer = new Producer();
//        Concumer concumer = new Concumer();
//
//        producer.producer();
//        concumer.foo();
        Lock lock =new ReentrantLock();
        Condition condition = lock.newCondition();

        AtomicInteger count = new AtomicInteger(10);
        ConcurrentHashMap<Object,Object> map = new ConcurrentHashMap<>();

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.lock();
                try {
                    int value = new Random().nextInt();
                    map.put(value,value);
                    condition.signal();
                }finally {
                    lock.unlock();
                }
            }).start();
        }

        lock.lock();
        try {
            while (count.get()>0){
                condition.await();
            }
        }finally {
            lock.unlock();
        }

        System.out.println(map.toString());
    }

为什么这段代码的锁没有被释放一直卡死在那里

因为你的AtomicInteger count = new AtomicInteger(10);没有做递减,count是一直大于0,进入死循环。应该在线程设置map后执行递减。

try {
                    int value = new Random().nextInt();
                    map.put(value, value);
                    count.decrementAndGet();
                    condition.signal();
                } finally {
                    lock.unlock();
                }

map后执行递减,要不然一直大于0,死循环了。