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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李彦来 中级黑马   /  2014-8-14 11:50  /  1073 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

问题在注释处
  1. public class WaitNotify {

  2.         public static void main(String[] args) {
  3.                 Stu s=new Stu();
  4.                 WriteInfo w=new WriteInfo(s);
  5.                 ReadInfo r=new ReadInfo(s);
  6.                 Thread t1=new Thread(w);
  7.                 Thread t2=new Thread(r);
  8.                 t1.start();

  9.                 t2.start();
  10.         }

  11. }
  12. class Stu{
  13.         String name;
  14.         int age;
  15.         boolean flag=false;
  16. }
  17. class WriteInfo implements Runnable{
  18.         private Stu s;
  19.         int num=0;
  20.         WriteInfo(Stu s){
  21.                 this.s=s;
  22.         }

  23.         public void run() {
  24.                 while(true){
  25.                         synchronized(Stu.class){    //为什么换成r就没有问题了???
  26.                                 if(s.flag){
  27.                                         try {
  28.                                                 s.wait();
  29.                                         } catch (InterruptedException e) {
  30.                                                 // TODO Auto-generated catch block
  31.                                                 e.printStackTrace();
  32.                                         }
  33.                                 }
  34.                                 if(num==0){
  35.                                         s.name="李";
  36.                                         s.age=20;
  37.                                 }else{
  38.                                         s.name="li";
  39.                                         s.age=13;
  40.                                 }
  41.                                 num=(num+1)%2;
  42.                                 s.flag=true;
  43.                                 s.notify();
  44.                         }
  45.                 }
  46.         }

  47. }
  48. class ReadInfo implements Runnable{
  49.         private Stu s;
  50.         ReadInfo(Stu s){
  51.                 this.s=s;
  52.         }
  53.         public void run() {
  54.                 while(true){

  55.                         synchronized(Stu.class){  //为什么换成r就没问题了???
  56.                                 if(!s.flag){
  57.                                         try {
  58.                                                 s.wait();
  59.                                         } catch (InterruptedException e) {
  60.                                                 // TODO Auto-generated catch block
  61.                                                 e.printStackTrace();
  62.                                         }
  63.                                 }
  64.                                 System.out.println(s.name+s.age);
  65.                                 s.flag=false;
  66.                                 s.notify();
  67.                         }

  68.                 }

  69.         }

  70. }
复制代码

2 个回复

倒序浏览
不是r,是Stu的实例 s,糊涂了....
回复 使用道具 举报
因为里面是s.wait()和s.notify();以s对象作为锁的对象,所以同步代码块里以s做锁不会出问题,如果使用Stu.class做锁对象时,直接用Stu.class.wait();、Stu.class.notify();或wait();、notify();就不会有问题了
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马