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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 苑桥别馆 中级黑马   /  2014-12-6 14:22  /  1024 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Input implements Runnable//实现Runnable接口
  2. {
  3.         private IO io;
  4.         public Input(IO io){//传递IO的引用,设置IO引用中的值
  5.                 this.io=io;
  6.         }
  7.         public void run(){//复写run()方法
  8.                 boolean flag=true;
  9.                 while(true){//使用while-if-else一直对IO引用循环交替赋值
  10.                         synchronized(Object.class){
  11.                                 if(io.iswait)
  12.                                         try{io.wait();}catch(Exception e){}
  13.                                                 if(flag){
  14.                                                         io.name="小明";
  15.                                                         io.sex="男";
  16.                                                         flag=false;
  17.                                                         }
  18.                                                         else{
  19.                                                                 io.name="Lili";
  20.                                                                 io.sex="female";
  21.                                                                 flag=true;
  22.                                                         }
  23.                                 io.iswait=true;
  24.                                 io.notify();
  25.                         }
  26.                 }
  27.         }
  28. };
  29. class Output implements Runnable
  30. {
  31.         private IO io;
  32.         public Output(IO io){//传递IO的引用,获取IO引用中的值
  33.                 this.io=io;
  34.         }
  35.         public void run(){
  36.                 while(true){
  37.                         synchronized(Object.class){//输出IO的值100次
  38.                                 if(!io.iswait)
  39.                                         try{io.wait();}catch(Exception e){}
  40.                                 System.out.println(io.name+"----"+io.sex);
  41.                                 io.iswait=false;//设置当前为等待
  42.                                 io.notify();//唤醒
  43.                         }
  44.                 }
  45.         }
  46. };
  47. class IO
  48. {
  49.         String name;
  50.         String sex;
  51.         boolean iswait=false;
  52. };
  53. class IoWaitMain
  54. {
  55.         public static void main(String[] args)
  56.         {
  57.                 IO io=new IO();
  58.                 Input in=new Input(io);//创建要加入线程的引用
  59.                 Output out=new Output(io);
  60.                 Thread t1=new Thread(in);//将对象加入线程
  61.                 Thread t2=new Thread(out);
  62.                 Thread t3=new Thread(in);
  63.                 Thread t4=new Thread(out);
  64.                 t1.start();//开启线程
  65.                 t2.start();
  66.                 //t3.start();
  67.                 //t4.start();
  68.         }
  69. }
复制代码
在上面代码中,为什么synchronized加的锁要和wait的锁一样?

2 个回复

倒序浏览
wait()方法是锁的方法,表示让持有该锁的线程执行等待操作
回复 使用道具 举报
Mr.Ni 发表于 2014-12-6 16:28
wait()方法是锁的方法,表示让持有该锁的线程执行等待操作

谢谢你的回答,我已经明白了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马