如果定义在Thread里面,有什么影响吗?
Thread lock = new Thread();
我这样使用
和定义在Object中
Object lock=new Object();
这样有什么区别吗
我的代码如下:
public class Test6 {
static Thread lock = new Thread();
static class Thread0 extends Thread {
@Override
public void run() {
System.out.println("开始唤醒");
synchronized (lock) {
lock.notify();
}
System.out.println("唤醒结束");
}
}
static class Thread1 extends Thread {
@Override
public void run() {
System.out.println("开始wait");
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("wait结束");
}
}
public static void main(String[] args) throws InterruptedException {
Thread0 t0 = new Thread0();
Thread1 t1 = new Thread1();
t1.start();
Thread.sleep(100);
t0.start();
}
}