A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小虎199406 中级黑马   /  2015-7-21 20:17  /  425 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.itheima2;

  2. public class TheadTest {


  3.         public static void main(String[] args) {
  4.                
  5.                 Res r = new Res();
  6.                
  7.                 Input in = new Input(r);
  8.                 Output out = new Output(r);

  9.                 Thread t1 = new Thread(in);
  10.                 Thread t2 = new Thread(out);
  11.                
  12.                 t1.start();
  13.                 t2.start();

  14.         }

  15. }
  16. class Input implements Runnable        //往资源中存入值
  17. {
  18.         private Res r;
  19.        
  20.         Input(Res r){
  21.                
  22.                 this.r = r;
  23.                
  24.         }
  25.         public void run() {
  26.        
  27.                 int x= 0;
  28.                 while(true)
  29.                 {
  30.                   synchronized(r){
  31.                        
  32.                           if(r.flag)
  33.                                 try {        //如果r为true时则等待
  34.                                         r.wait();  
  35.                                 } catch (InterruptedException e) {
  36.                        
  37.                                         e.printStackTrace();
  38.                                 }
  39.                           
  40.                           if(x==0){
  41.                                   r.name = "mike";
  42.                                   r.sex = "man";
  43.                           }else{
  44.                                   r.name ="丽丽";
  45.                                   r.sex = "女女女女";
  46.                           }
  47.                         //  x++;
  48.                         //   x=x%2;
  49.                         x= (x+1)%2;
  50.                          r.flag= true;
  51.                          r.notify();      //唤醒r锁属上等待的线程  即唤醒Output中等待的线程
  52.                           
  53.                   }
  54.                   
  55.                 }
  56.         }
  57.        
  58. }

  59. class Output implements Runnable       //输出资源中的值
  60. {
  61.     private Res r;
  62.   
  63.     Output(Res r){
  64.             this.r = r;
  65.     }
  66.         public void run() {
  67.                
  68.                 while(true){
  69.                        
  70.                         synchronized(r){
  71.                                
  72.                                 if(!r.flag)   
  73.                                         try {       //如果r为false时则等待
  74.                                                 r.wait();
  75.                                         } catch (InterruptedException e) {
  76.                                                
  77.                                                 e.printStackTrace();
  78.                                         }
  79.                                
  80.                                
  81.                                 System.out.println(r.name+"::::::"+r.sex);
  82.                                 r.flag=false;
  83.                                 r.notify();       //唤醒r锁属上等待的线程  即唤醒Input中等待的线程
  84.                         }

  85.                 }
  86.         }
  87.        
  88. }
  89. class Res            //被多个线程共同操作的资源
  90. {
  91.           String name;  
  92.           String sex;
  93.           boolean flag = false;

  94. }
复制代码
wait();
  notify();
  notifyAll();

  都是用在同步中,因为要对持有监视器(锁)的线程操作.
  所以要使用在同步中,因为只有同步才有锁.

  为什么这些操作线程的方法又要定义在Object类中呢
  因为这些方法在操作同步线程时,都必须要标识他们所操作的线程持有的锁
  只有同一个锁上的被等待线程,可以被同一个锁上的notify唤醒.
  不可以对不同所中的线程进行唤醒

也就是说等待和唤醒必须是同属一个锁的线程

  而锁可以是任意对象,所以可以被任意对象叼哦有那个的方法定义在Object类中

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马