Java多线程之wait()和notify()

Linux大全评论540 views阅读模式
  1. package com.jadyer.thread.wait; 
  2.  
  3. /** 
  4.  * Java多线程之wait()和notify()的妙用 
  5.  * @see ================================================================================================================= 
  6.  * @see 问题:同时启动两个线程和同时启动四个线程,控制台打印结果是不同的 
  7.  * @see      同时启动两个线程时,控制台会很规律的输出1010101010101010 
  8.  * @see      同时启动四个线程时,控制台起初会规律的输出10101010,一旦某一刻输出一个负数,那么后面的输出就会"一错再错" 
  9.  * @see 分析:对线程而言,任何一种情况,都是合理的 
  10.  * @see      这里假设其中的一种情况:tt22先执行,此时number=0,所以执行到了decrease()方法中的wait()方法,于是tt22被阻塞 
  11.  * @see      接着tt44执行了,此时number=0,所以也执行到了decrease()方法中的wait()方法,于是tt44也被阻塞了 
  12.  * @see      然后tt11执行了,此时number=0,www.linuxidc.com于是便执行到了increase()方法中的number++和notify()方法 
  13.  * @see      重点在于tt11执行到notify()方法时,我们假设该方法唤醒了tt44线程,于是tt44开始执行decrease()方法中的number-- 
  14.  * @see      此时number=-1,然后执行到了decrease()方法中notify()方法,我们同样假设该notify()方法唤醒的是tt22线程 
  15.  * @see      同样的道理,number又被减减了,于是number=-2,并被打印到控制台了,然后再假设tt22中的notify()方法唤醒的是tt11 
  16.  * @see      如此的循环往复,就看到那种"一错再错"的效果了 
  17.  * @see ================================================================================================================= 
  18.  * @see 修复:我们应当在wait()被唤醒的时候,再判断一次,然后再决定是否让该线程继续wait()下去 
  19.  * @see      因为,当某个线程被唤醒时,它不知道外界在其睡眠的期间发生了神马,所以要再判断一次。所以把if()改为while()判断,即可 
  20.  * @see ================================================================================================================= 
  21.  * @see 补充:如果只有两个线程的话,一个是对number增加的线程,一个是对number减少的线程,此时用if()判断是没有问题的 
  22.  * @see      因为无论线程如何的唤醒,它所唤醒的都是另一个线程,不存在第三个线程插进来捣乱的情况 
  23.  * @see ================================================================================================================= 
  24.  * @author 宏宇 
  25.  * @create Feb 22, 2012 3:20:05 PM 
  26.  */ 
  27. public class WaitNotifyTest { 
  28.     public static void main(String[] args) { 
  29.         Count count = new Count(); 
  30.          
  31.         Thread tt11 = new Thread(new IncreaseThread(count)); 
  32.         Thread tt22 = new Thread(new DecreaseThread(count)); 
  33.          
  34.         Thread tt33 = new Thread(new IncreaseThread(count)); 
  35.         Thread tt44 = new Thread(new DecreaseThread(count)); 
  36.          
  37.         tt11.start(); 
  38.         tt22.start(); 
  39.          
  40.         tt33.start(); 
  41.         tt44.start(); 
  42.     } 
  43.  
  44.  
  45. class IncreaseThread implements Runnable{ 
  46.     private Count count; 
  47.     public IncreaseThread(Count count){ 
  48.         this.count = count; 
  49.     } 
  50.     @Override 
  51.     public void run() { 
  52.         for(int i=0; i<20; i++){ 
  53.             try { 
  54.                 Thread.sleep((long)(Math.random()*1000)); 
  55.             } catch (InterruptedException e) { 
  56.                 e.printStackTrace(); 
  57.             } 
  58.             count.increase(); 
  59.         } 
  60.     } 
  61.  
  62.  
  63. class DecreaseThread implements Runnable{ 
  64.     private Count count; 
  65.     public DecreaseThread(Count count){ 
  66.         this.count = count; 
  67.     } 
  68.     @Override 
  69.     public void run() { 
  70.         for(int i=0; i<20; i++){ 
  71.             try { 
  72.                 Thread.sleep((long)(Math.random()*1000)); 
  73.             } catch (InterruptedException e) { 
  74.                 e.printStackTrace(); 
  75.             } 
  76.             count.decrease(); 
  77.         } 
  78.     } 
  79.  
  80.  
  81. class Count{ 
  82.     private int number; 
  83.      
  84.     public synchronized void increase(){ 
  85.         if(0 != number){ 
  86.             try { 
  87.                 //在同步方法(或者同步语句块)中,被锁定的对象可以调用wait()方法,这将导致当前线程被阻塞并释放该对象的互斥锁  
  88.                 //即解除了wait()方法所对应的当前对象的锁定状态,然后,其它的线程就有机会访问该对象了  
  89.                 wait(); 
  90.             } catch (InterruptedException e) { 
  91.                 e.printStackTrace(); 
  92.             } 
  93.         } 
  94.         number++; 
  95.         System.out.println(number); 
  96.         //唤醒其它的由于调用了wait()方法而在等待同一个对象的线程  
  97.         //该方法每次运行时,只能唤醒等待队列中的一个线程,至于是哪一个线程被唤醒,则由线程调度器来决定,程序员无法控制  
  98.         notify(); 
  99.     } 
  100.      
  101.     public synchronized void decrease(){ 
  102.         if(0 == number){ 
  103.             try { 
  104.                 wait(); 
  105.             } catch (InterruptedException e) { 
  106.                 e.printStackTrace(); 
  107.             } 
  108.         } 
  109.         number--; 
  110.         System.out.println(number); 
  111.         notify(); 
  112.     } 

企鹅博客
  • 本文由 发表于 2020年9月26日 18:10:11
  • 转载请务必保留本文链接:https://www.qieseo.com/174511.html

发表评论