zoukankan      html  css  js  c++  java
  • 4.等待和通知

    等待和通知

    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( )方法会唤醒处在等待序列中的所有线程。

  • 相关阅读:
    jQuery源码学习9——DOMReady加载
    jQuery源码学习8——工具方法之init
    jQuery源码学习7——实例成员
    jQuery源码学习6——工具方法之事件系统
    SQL中EXCEPT函数在 Mysql 和 sqlServer 中的替代方法
    关系型数据库及优势
    jsp小基础归纳
    eclipse换了高版本的maven插件后报错:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project
    开发常用网站收藏
    Struts2
  • 原文地址:https://www.cnblogs.com/TimerHotel/p/thread04.html
Copyright © 2011-2022 走看看