线程(7)--wait()方法、notify()方法、notifyAll()方法

来源:互联网 发布:中建七局网络协同平台 编辑:程序博客网 时间:2024/05/18 02:54

wait()方法:线程阻塞,如果传入了时间参数,阻塞时长到达执行的时间参数值,则自动恢复运行;如果没有传入时间参数,则直到调用该对象的notify或notifyAll方法才能使被该对象阻塞的线程恢复运行
notify()方法: 用于唤醒该对象阻塞的一个线程
notifyAll()方法: 用于唤醒该对象阻塞的所有线程
注意:

  1. 使用这三个方法前,必须同步调用这三个方法的对象
  2. 保证调用wait方法和notify或notifyAll方法的对象是同一个对象

下面用一个案例,来说明wait()方法、notify()方法和 notifyAll()方法的使用
案例说明:
案例一:八戒被妖精迷倒了,被悟空知道了,悟空要让那个妖精把八戒救醒!
案例二:八戒被妖精迷倒了,沙僧也被妖精迷倒了,被悟空知道了,悟空要让那个妖精把八戒和沙僧救醒!

案例一:(代码)分析:在本案例中,八戒需要被妖精迷倒(wait()方法),悟空需要通过妖精让八戒醒来(notify),使用同一个妖精,所以需要创建妖精线程、八戒线程、悟空线程(同步)

  • 创建妖精线程
 package com.wait.entity;/** * @version 2017-12-20 上午10:34:00 * 妖精类:默认继承object方法 * wait方法:阻塞线程(施法) * notify:释放第一个被wait阻塞的线程 * notifyAll:释放所有被wait阻塞的线程 */public class Ghost extends Thread{    @Override    public void run() {        System.out.println("释放迷药或是解开迷药!");    }}
  • 创建八戒线程
package com.wait.entity;/** * @version 2017-12-20 下午3:49:20 */public class BaJie extends Thread{    private Ghost ghost;    public BaJie(Ghost ghost){        this.ghost = ghost;    }    //重写run()方法    @Override    public void run() {        super.run();        System.out.println("我(八戒)被妖精迷倒了!");        //调用wait、notify、notifyAll方法之前,必须先同步对象        //wait()方法,如果带有时间参数的话,等时间结束,线程自动向下执行。如果没有时间参数,线程一直处于等待当中,直到执行notify方法或是notifyAll方法        synchronized (ghost) {            try {                ghost.wait(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }        System.out.println("我(八戒)醒了!");    }}
  • 创建悟空线程
package com.wait.entity;/** * @version 2017-12-20 下午4:07:02 */public class WuKong extends Thread{    private Ghost ghost;    public WuKong(Ghost ghost) {        super();        this.ghost = ghost;    }    @Override    public void run() {        super.run();        System.out.println("悟空抓住了妖精");        synchronized (ghost) {            //案例一使用            ghost.notify();            //案例二使用            ghost.notifyAll();        }    }}
  • 创建测试类
package com.wait.entity;/** * @version 2017-12-20 下午4:10:40 */public class TestNotify {    public static void  main(String args[]){        //创建妖精对象        Ghost ghost = new Ghost();        ghost.start();        //创建八戒线程        BaJie bajie  = new BaJie(ghost);        bajie.start();        //创建悟空的线程        WuKong wukong = new WuKong(ghost);        wukong.start();    }}

案例二:在案例一的基础上,加上沙僧类和将TestNotify类,替换成TestNotifyAll类

  • 创建沙僧类
package com.wait.entity;/** * @author 张丹华 * @version 2017-12-20 下午4:21:42 */public class ShaSeng extends Thread{    private Ghost ghost;    public ShaSeng(Ghost ghost) {        super();        this.ghost = ghost;    }    @Override    public void run() {        System.out.println("徒儿,快来救师傅!");        synchronized(ghost){            try {                ghost.wait(4000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }         System.out.println("徒儿,我们继续赶路吧!");    }}
  • TestNotifyAll类
 package com.wait.entity;/** * @version 2017-12-20 下午4:10:40 */public class TestNotify {    public static void  main(String args[]) throws InterruptedException{        //创建妖精对象        Ghost ghost = new Ghost();        ghost.start();        //创建八戒线程        BaJie bajie  = new BaJie(ghost);        bajie.start();        //创建沙僧线程        ShaSeng shaseng = new ShaSeng(ghost);        shaseng.start();        Thread.sleep(1000);        //创建悟空的线程        WuKong wukong = new WuKong(ghost);        wukong.start();    }}

以上就是我关于wait()方法、notify()方法和notifyAll()方法的理解;如有不懂的地方,欢迎在下面留言。

阅读全文
0 0