public class Test extends Thread{ public Test() {
System.out.println("start...");
}
public void run() {
System.out.println("waiting...");
synchronized(this) {
try {
wait();
} catch(Exception ex) {
System.out.println(ex);
}
}
System.out.println("notify");
} public static void main(String[] args) {
Test t = new Test();
t.start();
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
System.out.println(ex);
}
System.out.println("notifying...");
synchronized(t) {
t.notify();
}
}
}
System.out.println("start...");
}
public void run() {
System.out.println("waiting...");
synchronized(this) {
try {
wait();
} catch(Exception ex) {
System.out.println(ex);
}
}
System.out.println("notify");
} public static void main(String[] args) {
Test t = new Test();
t.start();
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
System.out.println(ex);
}
System.out.println("notifying...");
synchronized(t) {
t.notify();
}
}
}
改成上面的样子就可以了
如果在没个线程里都用上,如果线程很多怎么办,比如上千个?
不过跟线程的数量没关系。
多看书。仔细看api。
join()这个方法到底是什么意思,书上说是使调用线程等待另一线程完成,我不太理解啊。