码迷,mamicode.com
首页 > 其他好文 > 详细

4.等待和通知

时间:2019-01-27 14:29:28      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:其他   对象   ace   port   over   cat   package   一个   interrupt   

等待和通知

Object对象提供了wait( ) 方法和 notify ( ) 方法。

wait( )方法需要配对着synchronized 关键字使用。

当一个线程的资源调用 wait( )方法时,它会失去锁,然后进入等待序列。

当其他线程调用notify方法时才会重新激活。

实例:

package com.xm.thread.t_19_01_27;

import java.util.concurrent.TimeUnit;

public class NotifyAndWait{

    private static Object object = new Object();


    static class WaitThread extends Thread{

        @Override
        public void run() {
            System.out.println("WaitThread Start!");
            try {
                System.out.println("Object wait!!!");
                synchronized (object) {
                    object.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("WaitThread Finish!");
        }
    }

    static class NotifyThread extends Thread{

        @Override
        public void run() {
            System.out.println("NotifyThread Start!");
            System.out.println("Object notify");
            synchronized (object) {
                object.notify();
            }
            System.out.println("NotifyThread Finish!");
        }
    }



    public static void main(String[] args) throws InterruptedException {
        new WaitThread().start();

        TimeUnit.SECONDS.sleep(5);

        new NotifyThread().start();

    }
}


运行结果:

WaitThread Start!

Object wait!!!

NotifyThread Start!

Object notify

NotifyThread Finish!

WaitThread Finish!

notify( )方法只会随机激活等待序列中的一个线程,而notifyAll( )方法激活所有等待的线程。

实例:

package com.xm.thread.t_19_01_27;

import java.util.concurrent.TimeUnit;

public class NotifyAndWait{

    private static Object object = new Object();


    static class WaitThread extends Thread{

        @Override
        public void run() {
            System.out.println("WaitThread Start!");
            try {
                System.out.println("Object wait!!!");
                synchronized (object) {
                    object.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("WaitThread Finish!");
        }
    }

    static class NotifyThread extends Thread{

        @Override
        public void run() {
            System.out.println("NotifyThread Start!");
            System.out.println("Object notify");
            synchronized (object) {
                object.notify();
            }
            System.out.println("NotifyThread Finish!");
        }
    }



    public static void main(String[] args) throws InterruptedException {
        new WaitThread().start();

        new WaitThread().start();

        TimeUnit.SECONDS.sleep(5);

        new NotifyThread().start();

    }
}


运行结果:

WaitThread Start!

Object wait!!!

WaitThread Start!

Object wait!!!

NotifyThread Start!

Object notify

NotifyThread Finish!

WaitThread Finish!

结果分析:

两个WaitThread线程只被唤醒了一个。

实例:

package com.xm.thread.t_19_01_27;

import java.util.concurrent.TimeUnit;

public class NotifyAndWait{

    private static Object object = new Object();


    static class WaitThread extends Thread{

        @Override
        public void run() {
            System.out.println("WaitThread Start!");
            try {
                System.out.println("Object wait!!!");
                synchronized (object) {
                    object.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("WaitThread Finish!");
        }
    }

    static class NotifyThread extends Thread{

        @Override
        public void run() {
            System.out.println("NotifyThread Start!");
            System.out.println("Object notify");
            synchronized (object) {
                object.notifyAll();
            }
            System.out.println("NotifyThread Finish!");
        }
    }



    public static void main(String[] args) throws InterruptedException {
        new WaitThread().start();

        new WaitThread().start();

        TimeUnit.SECONDS.sleep(5);

        new NotifyThread().start();

    }
}


运行结果:

WaitThread Start!

Object wait!!!

WaitThread Start!

Object wait!!!

NotifyThread Start!

Object notify

NotifyThread Finish!

WaitThread Finish!

WaitThread Finish!

结果分析:

notifyAll( )方法会唤醒处在等待序列中的所有线程。

4.等待和通知

标签:其他   对象   ace   port   over   cat   package   一个   interrupt   

原文地址:https://www.cnblogs.com/TimerHotel/p/thread04.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!