黑马程序员技术交流社区

标题: [打印本页]

作者: hadexs    时间: 2013-5-22 17:44
标题:
为什么wait ,notify, notifyAll ,这些方法要定义在object类中?
作者: Super_Class    时间: 2013-5-22 18:13
  1. class Resource{  <font color="#ff0000">//这里的这个Resource类中封装了线程任务类中要执行的的代码。要实现的是两个线程要交替运行。</font>
  2.                                 String name;
  3.                                 String sex;

  4.                                  boolean flag = true;
  5.                                 public synchronized void put(String name,String sex){
  6.                                         if(!flag){
  7.                                                 try {
  8.                                                         wait();//当标志位为false,线程wait这里其实是r.wait();<span style="color: rgb(255, 0, 0);">Resource r = new Resource().对象r就要使用wait方法。</span>
  9.                                                 } catch (InterruptedException e1){}
  10.                                         }
  11.                                         this.name = name;
  12.                                         this .sex = sex;
  13.                                        
  14.                                         flag = false;        //执行完后,将标志位状态改变。
  15.                                         notify();                //并且唤醒一个线程这里其实是r.notify();
  16.                                 }
  17.                                
  18.                                 public synchronized void get(){
  19.                                         if(flag){
  20.                                                 try {
  21.                                                         wait();    //当标志位为true,线程wait。这里其实是r.wait();
  22.                                                 } catch (InterruptedException e1){}
  23.                                                 }
  24.                                         System.out.println(name+"....."+sex);
  25.                                        
  26.                                         flag = true;//执行完后,将标志位状态改变。
  27.                                         notify();//并且唤醒一个线程这里其实是r.notify();
  28.                                         }
  29.                                 }

  30.                                 class Input implements Runnable{
  31.                                         Resource r ;
  32.                                         private int x=0;
  33.                                         Input(Resource r){
  34.                                                 this.r = r;
  35.                                         }
  36.                                         public  void run(){   //这里有一个线程任务
  37.                                                 while(true){
  38.                                                         if(x == 0){
  39.                                                                 r.put("zhangsan","nan");//这里可以将同步代码封装
  40.                                                         }else{
  41.                                                                 r.put("lisi","nv");
  42.                                                         }
  43.                                                         x = (x+1)%2;
  44.                                                 }
  45.                                         }
  46.                                 }

  47.                                 class Output implements Runnable{
  48.                                         Resource r ;
  49.                                         Output(Resource r){
  50.                                                 this.r = r;
  51.                                         }
  52.                                        
  53.                                         public void run(){ //这里有一个线程任务
  54.                                                 while(true){
  55.                                                         r.get();        //这里可以将同步代码封装
  56.                                                 }
  57.                                         }
  58.                                 }
复制代码

作者: 江大海    时间: 2013-5-22 18:31
因为锁的对象可以是任意类型的,就是synchronized(e)中的e是任意类型的,然后wait   notify啊等等都要标明是哪个锁的,所以是Object类的
作者: 王靖远    时间: 2013-5-22 19:48
任意子类都能用的当然就定义在Object(所有类的父类)中了。
await asignal这些就没有定义在Object中了。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2