美文网首页
wait() 和 notify()

wait() 和 notify()

作者: zheng7 | 来源:发表于2018-04-19 04:32 被阅读0次

wait() : 线程挂起,等待被唤醒。
notify() : 通知其他某一个线程从挂起态转换到就绪态 。
这两个方法都要在sychronized块中使用;都会释放锁,不过wait()是立刻释放锁,notify()是在退出sychronized块时释放锁。

题目
建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C,要求线程同时运行,交替打印10次ABC。

public class Print10TimesABCThread implements Runnable {     
        
    private String name;     
    private Object prev;     
    private Object self;     
    
    private Print10TimesABCThread(String name, Object prev, Object self) {     
        this.name = name;     
        this.prev = prev;     
        this.self = self;     
    }     
    
    @Override    
    public void run() {     
        int count = 10;     
        while (count > 0) {     
            synchronized (prev) {     
                synchronized (self) {     
                    System.out.print(name);     
                    count--;    
                      
                    self.notify();   
                }   
                try {     
                    prev.wait();     
                } catch (InterruptedException e) {     
                    e.printStackTrace();     
                }       
            }     
    
        }     
    }     
    
    public static void main(String[] args) throws Exception {     
        Object a = new Object();     
        Object b = new Object();     
        Object c = new Object();     
        Print10TimesABCThread pa = new Print10TimesABCThread("A", c, a);     
        Print10TimesABCThread pb = new Print10TimesABCThread("B", a, b);     
        Print10TimesABCThread pc = new Print10TimesABCThread("C", b, c);     
             
             
        new Thread(pa).start();  
        Thread.sleep(100);  //确保按顺序A、B、C执行  
        new Thread(pb).start();  
        Thread.sleep(100);    
        new Thread(pc).start();     
        Thread.sleep(100);    
        }     
}  

解析
按照A->B->C的执行顺序,每个线程都被它的前一个线程唤醒,因此一个线程打印字符需要拿到两个锁,自身的锁和前一个线程的锁,在打印结束之后,要通知下一个线程,也就是说要调用自身锁的notify,之后再等待被前一个线程唤醒,为下一次打印作准备。

相关文章

网友评论

      本文标题:wait() 和 notify()

      本文链接:https://www.haomeiwen.com/subject/nujikftx.html