为什么在将物品推入队列时会出现死锁?

How this code may cause deadlock? If q1 is full, t1 will releasw the lock and t2 can enter.. Resourse: https://www.cs.bgu.ac.il/~spl181/index.php?page=Liveliness-and-performance#deadlock_caused_by_wait

class WaitLock {
public static void main(String [] args){
    SimpleQueue<Integer> q1 = new SimpleQueue<Integer>(5);
    SimpleQueue<Integer> q2 = new SimpleQueue<Integer>(5);

    Thread t1 = new Thread(new Runnable(){
        public void run(){
            Integer i1 = q1.take();
            q2.put(i1+1);                
        }});
    Thread t2 = new Thread(new Runnable(){
        public void run(){
            Integer i2 = q2.take();
            q1.put(i2+1);
        }});
    t1.start();
    t2.start();
}
}
评论