- <p>class Print {
- private int flag = 1;
- public void print1() throws InterruptedException {
- synchronized(this) {
- if (flag != 1) {
- this.wait();
- }
- System.out.print("黑");
- System.out.print("马");
- System.out.print("程");
- System.out.print("序");
- System.out.print("员");
- System.out.println();
- flag = 2;
- this.notify();
- }
- }
-
- public void print2() throws InterruptedException {
- synchronized(this) {
- if (flag != 2) {
- this.wait();
- }
- System.out.print("传");
- System.out.print("智");
- System.out.print("播");
- System.out.print("客");
- System.out.println();
- flag = 1;
- this.notify();
- }
- }
- }</p><p>
- </p><p>为什么用类名.class锁会出错啊</p><p>错误如下:</p><p>java.lang.IllegalMonitorStateException
- 黑马程序员
- at java.lang.Object.notify(Native Method)
- at com.heima.test.Print.print2(Test_Wait.java:68)
- at com.heima.test.Test_Wait$2.run(Test_Wait.java:26)
- java.lang.IllegalMonitorStateException
- at java.lang.Object.notify(Native Method)
- at com.heima.test.Print.print1(Test_Wait.java:53)
- at com.heima.test.Test_Wait$1.run(Test_Wait.java:14)
- java.lang.IllegalMonitorStateException传智播客</p>
复制代码
|
|