zoukankan      html  css  js  c++  java
  • Java中的对象Object方法之---wait()和notifiy()

    上一篇说到了suspend()和resume()方法,这一篇咋们继续,接着来介绍wait()和notify()方法,我们都知道这两个方法和之前介绍的方法不太一样,那就是这两个方法是对象Object上的,不属于Thread类上的。我们也知道这两个方法是实现多个线程之间的通信和互斥的,不多说了,下面就来看一下例子吧:

    例子描述:

    开启两个线程,子线程循环10次,主线程循环100次,如此反复循环50次

    代码如下:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. package cn.itcast.heima;  
    2.   
    3. public class TraditionalThreadCommunication {  
    4.   
    5.     public static void main(String[] args) {  
    6.         final Business business = new Business();  
    7.         //子线程循环10次  
    8.         new Thread(new Runnable(){  
    9.             @Override  
    10.             public void run() {  
    11.                 for(int i=0;i<50;i++){  
    12.                     business.sub(i);  
    13.                 }  
    14.             }  
    15.         }).start();  
    16.           
    17.         //主线程循环100次  
    18.         for(int i=0;i<50;i++){  
    19.             business.main(i);  
    20.         }  
    21.           
    22.     }  
    23.       
    24.     /** 
    25.      * 业务逻辑类 
    26.      * @author weijiang204321 
    27.      * 
    28.      */  
    29.     static class Business{  
    30.         private boolean bShouldSub = true;//true表示sub执行,false表示main执行  
    31.         public synchronized void sub(int i){  
    32.             //将这里的if改成while,效果更好,while会再次判断,安全性高  
    33.             //因为有时候线程可能被假唤醒  
    34.             while(!bShouldSub){  
    35.                 try {  
    36.                     //等待  
    37.                     this.wait();  
    38.                 } catch (InterruptedException e) {  
    39.                     e.printStackTrace();  
    40.                 }  
    41.             }  
    42.               
    43.             for(int j=0;j<=10;j++){  
    44.                 System.out.println("sub thread sequece of" + j + ",loop of"+i);  
    45.             }  
    46.             bShouldSub = false;  
    47.             this.notify();//唤醒该锁的等待线程  
    48.         }  
    49.           
    50.         public synchronized void main(int i){  
    51.             while(bShouldSub){  
    52.                 try {  
    53.                     this.wait();  
    54.                 } catch (InterruptedException e) {  
    55.                     e.printStackTrace();  
    56.                 }  
    57.             }  
    58.             for(int j=0;j<=100;j++){  
    59.                 System.out.println("main thread sequece of" + j + ",loop of"+i);  
    60.             }  
    61.             bShouldSub = true;  
    62.             this.notify();  
    63.         }  
    64.     }  
    65.   
    66. }  
    通过wait和notify机制来实现这两个线程的循环的有序性,能够保证子线程循环10次,主线程循环100次这样交替运行。

    运行结果很多,这里就不截图了!这里我们也是可以看到的,调用wait方法是会释放锁的,所以他会被用到很多,这个和之前的几个方法是不同的!


    注意的两点:

    第一:同步代码块中的锁和调用wait和notifiy方法的对象锁一定要是同一个。

    第二:wait和notify方法的调用一定要在同步代码块中,不然会报异常,可以自行测试一下。


    不要因为这样就结束了,这一篇还不是最终篇,等待下一篇吧!

  • 相关阅读:
    CQUOJ 10819 MUH and House of Cards
    CQUOJ 9920 Ladder
    CQUOJ 9906 Little Girl and Maximum XOR
    CQUOJ 10672 Kolya and Tandem Repeat
    CQUOJ 9711 Primes on Interval
    指针试水
    Another test
    Test
    二分图匹配的重要概念以及匈牙利算法
    二分图最大匹配
  • 原文地址:https://www.cnblogs.com/roccheung/p/5797384.html
Copyright © 2011-2022 走看看