package com.dtz.ThreadAndGui.day25.Thread;
import com.dtz.Thread.day24.syn.Demo1_synchronized;
public class Demo1_Notify {
public static void main(String[] args) {
final Demo d = new Demo();
new Thread(){
@Override
public void run() {
while(true) {
try {
d.print1();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
new Thread(){
@Override
public void run() {
while(true) {
try {
d.print2();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
class Demo {
private int flag = 1;
public void print1() throws InterruptedException {
synchronized (this) { //这里是Demo.class的话,下面的wait()和notify()会抛异常
if (flag != 1) {
this.wait();
}
System.out.print("a");
System.out.print("a");
System.out.print("a");
System.out.println();
flag = 2;
this.notify();
}
}
public void print2() throws InterruptedException {
synchronized (this) {
if (flag != 2) {
this.wait();
}
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.print("b");
System.out.println();
flag = 1;
this.notify();
}
}
}
wati() 和 notify() 、notifyAll() 方法是 Object 类的两个方法,它的语法就是这样规定的,你可以看看 JDK 的 Object 类的方法说明,这是 wait 的:
This method should only be called by a thread that is the owner of this object's monitor.
就是说,要调用某个对象的 wait 的方法,必须是由持有该对象锁的线程进行的。
就是必须 synchronized(obj){ } 然后才能在同步代码块中 obj.wait()。
这些是 Java 的同步的基本语法,你可以测测,如果不获取锁直接调用 wait 方法的结果。
举个例子,对于一个锁A,对应的钥匙A 。现在有个钥匙B肯定是打不开锁A的。 针对于该题目来说,如果你对ObjA加锁,
那么只有使用ObjA的notify和wait,才能让竞争ObjA锁的线程被唤醒,或者使其进入等待。 你可以使用其他对象的notify或者wait,但
是只能作用于其他对象锁下的线程
锁和调用对象不一致,直接会报错吧
不一定要锁this,任何对象都可以,但是锁this比较好一点,你也可以单独创建一个锁对象专门做这个事:
private Object mLock = new Object();