黑马程序员技术交流社区

标题: synchronized问题, 求指点 [打印本页]

作者: 张然龙    时间: 2014-7-11 16:45
标题: synchronized问题, 求指点
本帖最后由 张然龙 于 2014-7-11 19:48 编辑
  1. class Show
  2. {
  3.         boolean flag = true;
  4.         void run1() throws InterruptedException
  5.         {
  6.                 synchronized (Show.class)
  7.                 {
  8.                         while(true)
  9.                         {
  10.                                 if(flag)
  11.                                         wait();
  12.                                 System.out.println("--------");
  13.                                 flag = true;
  14.                                 notify();
  15.                         }
  16.                 }
  17.         }
  18.         void run2() throws InterruptedException
  19.         {
  20.                 synchronized (Show.class)
  21.                 {
  22.                         while(true)
  23.                         {
  24.                                 if(!flag)
  25.                                         wait();
  26.                                 System.out.println("****");
  27.                                 flag = false;
  28.                                 notify();
  29.                         }
  30.                 }
  31.         }
  32. }
  33. class Run1 implements Runnable
  34. {
  35.         Show show;
  36.         Run1(Show show)
  37.         {
  38.                 this.show = show;
  39.         }
  40.         public void run()
  41.         {
  42.                 try {
  43.                         show.run1();
  44.                 } catch (InterruptedException e) {
  45.                         e.printStackTrace();
  46.                 }
  47.         }
  48. }
  49. class Run2 implements Runnable
  50. {
  51.         Show show;
  52.         Run2(Show show)
  53.         {
  54.                 this.show = show;
  55.         }
  56.         public void run()
  57.         {
  58.                 try {
  59.                         show.run2();
  60.                 } catch (InterruptedException e) {
  61.                         e.printStackTrace();
  62.                 }
  63.         }
  64. }
  65. public class Demo
  66. {
  67.         public static void main(String[] args) throws Exception
  68.         {
  69.                 Show show = new Show();
  70.                 new Thread(new Run1(show)).start();
  71.                 new Thread(new Run2(show)).start();
  72.         }
  73. }
复制代码




为什么会报错??.....

作者: s526349668    时间: 2014-7-11 17:31
你的方法不是静态的,6行和20行不用show.class,用this就对了
作者: 张然龙    时间: 2014-7-11 18:30
本帖最后由 张然龙 于 2014-7-11 18:40 编辑
s526349668 发表于 2014-7-11 17:31
你的方法不是静态的,6行和20行不用show.class,用this就对了

确实, 你说的很对, 但我想请教下,为什么呢?我的锁也是同一把锁啊??
作者: s526349668    时间: 2014-7-11 18:42
张然龙 发表于 2014-7-11 18:30
确实, 你说的很对, 但我想请教下,为什么呢?我的锁也是同一把锁才对啊??

同步函数使用的锁是this啊,静态方法中不能使用this,所以使用的类.class
作者: 张然龙    时间: 2014-7-11 18:47
s526349668 发表于 2014-7-11 18:42
同步函数使用的锁是this啊,静态方法中不能使用this,所以使用的类.class
多谢指点,
可是,,既然是非静态的,那我用Show.class也应该是正确的啊?  求指点。。
作者: s526349668    时间: 2014-7-11 19:21
张然龙 发表于 2014-7-11 18:47
多谢指点,
可是,,既然是非静态的,那我用Show.class也应该是正确的啊?  求指点。。 ...

你要是要写成show.class就得把write()和notify()写成Show.class.write和Show.class.notify()
作者: 张然龙    时间: 2014-7-11 19:31
s526349668 发表于 2014-7-11 19:21
你要是要写成show.class就得把write()和notify()写成Show.class.write和Show.class.notify() ...

我靠,,,真的啊,兄弟谢谢你啊。。。好人啊。。
作者: ws484664    时间: 2014-7-11 20:24
来学习学习
作者: rekirt    时间: 2014-7-11 21:10
张然龙 发表于 2014-7-11 19:31
我靠,,,真的啊,兄弟谢谢你啊。。。好人啊。。

你写的wait,notify其实前面都有一个this.谁的锁就用谁的wait和notify,
作者: 沐子松/kf    时间: 2014-7-11 21:41
学习           
作者: 张然龙    时间: 2014-7-12 14:29
rekirt 发表于 2014-7-11 21:10
你写的wait,notify其实前面都有一个this.谁的锁就用谁的wait和notify,

多谢提醒!




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