奇怪的线程
来源:百度知道 编辑:UC知道 时间:2024/07/07 03:02:14
public class two {
public two() {
two e1 = this;
two e2 = this;
synchronized (e1) {
try {
e2.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
为什么我只要一new一个two对象,以后的任何操作都不执行了?
public two() {
two e1 = this;
two e2 = this;
synchronized (e1) {
try {
e2.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
为什么我只要一new一个two对象,以后的任何操作都不执行了?
package com.zte;
public class Two {
public Two() {
Two e1 = this;
Two e2 = this;
synchronized (e1) {
try {
System.out.println("before wait...");
e2.wait();
System.out.println("after wait...");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Two t = new Two();
}
}
因为你定义了两个一样的对象。线程同步锁,是this,让this进行等待,会陷入无限等待,因为任何时候都无法获得this这个钥匙了。无法解锁。
不懂。哪里有线程了?