public class Test {
class TestRunnable implements Runnable {
private String TName="TName";
private Object obj=new Object();
private int a=5;
public void run() {
synchronized (obj) {
a--;
if (a <= 0) {
System.out.println(TName + " notifyALL:" + a);
obj.notifyAll();
} else {
System.out.println(TName + " wait:" + a);
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(TName + " relese :" + a);
}
}
public static void main (String[] args){
System.out.println("hello word");
Test t=new Test();
Runnable run_a=t.new TestRunnable();
for(int i=0; i<5;i++){
new Thread(run_a).start();
}
}
}
// wait是让使用wait方法的对象等待,暂时先把对象锁给让出来,给其它持有该锁的对象用,其它对象用完后再告知(notify)等待的那个对象可以继续执// 行了,整个过程就是这样