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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 老外 中级黑马   /  2015-1-19 15:57  /  1261 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

线程间的通讯:
其实就是多个线程在操作统一资源。
到时操作的动作不同。

  1. class Res
  2. {
  3.         private String name;
  4.         private String sex;
  5.         private boolean flag = false;

  6.         public synchronized void set(String name, String sex)
  7.         {
  8.                 if(flag)
  9.                         try{this.wait();}catch(Exception e){}
  10.                 this.name = name;
  11.                 this.sex = sex;
  12.                 flag = true;
  13.                 this.notify();
  14.         }
  15.         public synchronized void out()
  16.         {
  17.                 if(!flag)
  18.                         try{this.wait();}catch(Exception e){}
  19.                 System.out.println(name+"..."+sex);
  20.                 flag = false;
  21.                 this.notify();
  22.         }
  23. }

  24. class Input implements Runnable
  25. {
  26.         private Res r;
  27.         Input(Res r)
  28.         {
  29.                 this.r = r;
  30.         }
  31.         public void run()
  32.         {
  33.                 int x = 0;
  34.                
  35.                         while (true)
  36.                         {
  37.                                
  38.                                 if(x==0)
  39.                                         r.set("mike","man");
  40.                                 else
  41.                                         r.set("丽丽","女女女女女");
  42.                                 x = (x+1)%2;
  43.                         }
  44.         }
  45.        
  46. }

  47. class Output implements Runnable
  48. {
  49.         private Res r;
  50.         Output(Res r)
  51.         {
  52.                 this.r = r;
  53.         }
  54.         public void run()
  55.         {
  56.                 while(true)
  57.                 {
  58.                         r.out();
  59.                 }
  60.         }
  61. }

  62. class  InputOutputDemo
  63. {
  64.         public static void main(String[] args)
  65.         {
  66.                 Res r = new Res();
  67.                
  68.                 new Thread(new Input(r)).start();
  69.                 new Thread(new Output(r)).start();
  70.                 /*
  71.                 Input in = new Input(r);
  72.                 Output out = new Output(r);

  73.                 Thread t1 = new Thread(in);
  74.                 Thread t2 = new Thread(out);

  75.                 t1.start();
  76.                 t2.start();
  77.                 */
  78.         }
  79. }
复制代码


wait;
notify();
notifyAll();
都使用在同步中,因为要对持有监视器(锁)的线程操作。
所以要使用在同步中,因为只有同步才具有锁。

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

也就是说,等待和唤醒必须是同一个锁。

而锁可以是任意对象,所以可以被任意对象调用的方法定义在Object类中

4 个回复

倒序浏览
顶个           
回复 使用道具 举报
看了一下
回复 使用道具 举报
咳咳,楼主注意错别字
回复 使用道具 举报
这是炫酷?   
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马