线程 wait 等待与notify 唤醒 使用 java 代码

package cn.com.cfets.cfib.tbs.threadwait;

/**
 * Created by cfets on  2018/6/19.12:20
 */
public class WaitNotifyTest {

    //在多线程间共享的对象上使用wait
    private String[] shareObj = {"true"};

    public static void main(String[] args) {
        WaitNotifyTest test = new WaitNotifyTest();
        ThreadWait threadWait1 = test.new ThreadWait("wait thread1");
        threadWait1.setPriority(2);
        ThreadWait threadWait2 = test.new ThreadWait("wait thread2");
        threadWait2.setPriority(3);
        ThreadWait threadWait3 = test.new ThreadWait("wait thread3");
        threadWait3.setPriority(4);

        threadWait1.start();
        threadWait2.start();
        threadWait3.start();

        ThreadNotify threadNotify = test.new ThreadNotify("notify thread");
        threadNotify.start();
    }

    class ThreadWait extends Thread {

        public ThreadWait(String name) {
            super(name);
        }

        public void run() {
            synchronized (shareObj) {
                if ("true".equals(shareObj[0])) {
                    System.out.println("线程" + this.getName() + "开始等待");
                    long startTime = System.currentTimeMillis();
                    try {
                        shareObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    long endTime = System.currentTimeMillis();
                    System.out.println("线程" + this.getName() + "等待时间为:" + (endTime - startTime)/1000 + " 秒");
                }
            }
            System.out.println("线程" + getName() + "等待结束");
        }
    }

    class ThreadNotify extends Thread {

        public ThreadNotify(String name) {
            super(name);
        }


        public void run() {
            try {
                // 给等待线程等待时间
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (shareObj) {
                System.out.println("线程" + this.getName() + "开始准备通知");
                shareObj[0] = "false";
                shareObj.notifyAll();
                System.out.println("线程" + this.getName() + "通知结束");
            }
            System.out.println("线程" + this.getName() + "运行结束");
            System.out.println("------------------------------");
        }
    }
}



--------------------------------------------------------------------
package cn.com.cfets.cfib.tbs.threadwait;

/**
 * Created by cfets on  2018/6/19.9:06
 */
public class WaitTest {

    public static void main(String[] args) {
        ThreadA t1 = new ThreadA("t1");

        synchronized (t1) {
            try {
                // 启动 "线程t1"
                System.out.println(Thread.currentThread().getName() + " start t1");
                t1.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    public static class ThreadA extends Thread {

        public ThreadA(String name) {
            super(name);
        }

        public void run() {
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + " call notify()");
                try {
                    System.out.println(Thread.currentThread().getName() + " wait()");
//                    this.wait();
                    this.wait(2000); // 2秒后自动唤醒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " continue");
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/xiaolei2017/p/9198364.html