我有一个扩展Thread类的MyTunnel类:
public class MyTunnel extends Thread {
protected Object obj;
public MyTunnel() {
super(MyTunnel.class.getName());
obj = new Object();
prepare();
}
public void prepare() {
System.out.println("Before starting...");
start();
synchronized(obj) {
try {
obj.wait(3000);
} catch (InterruptedException e) {
System.out.println("Error while waiting thread to start");
}
}
System.out.println("After starting...");
}
@Override
public void run() {
System.out.println("running...");
}
}
当我在主线程上运行以下代码时:
System.out.println("Before creating tunnel...");
MyTunnel tunnel = new MyTunnel();