看这段程序
package org.arcie.simulator.test;public class NotifyTest implements Runnable {
private Wait wait;
public NotifyTest(Wait wait){
this.wait = wait;
}
public void run() {
wait.doSomthing();
}
public static void main(String [] args){
Wait wait = new Wait(4,"DAVID");
Thread t1 = new Thread(new NotifyTest(wait));
Thread t2 = new Thread(new NotifyTest(wait));
Thread t3 = new Thread(new NotifyTest(wait));
Thread t4 = new Thread(new NotifyTest(wait));
t1.start();
t2.start();
t3.start();
t4.start();
}
} class Wait {
private int counter = 0;
private String name = null;
public Wait(int counter,String name){
this.counter = counter;
this.name = name;
}
public synchronized void doSomthing(){
int tempCounter = --counter;
if(tempCounter <= 0){
customizedNotifyAll();
}
else
{
while(tempCounter > 0){
try {
System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"will invoke WAIT()");
--tempCounter;
wait();
} catch (InterruptedException e) {
e.printStackTrace();
notifyAll();
}
System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"has been ACTIVED");
}
customizedNotifyAll();
}
}
public void customizedNotifyAll(){
notify(); //可以把这句换成notifyAll();
System.out.println(Thread.currentThread().getName()+"-<"+name+counter+">"+"::"+"INVOKED NOTIFYALL() AND FINISHED");
}
} 问题:不是都说notify() 方法是随机唤醒等待的线程吗?但为什么我运行的结果每次都是一样的呢??
运行结果始终是:
Thread-0-<DAVID3>will invoke WAIT()
Thread-1-<DAVID2>will invoke WAIT()
Thread-2-<DAVID1>will invoke WAIT()
Thread-3-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-0-<DAVID2>has been ACTIVED
Thread-0-<DAVID2>will invoke WAIT()还有把public void customizedNotifyAll()方法里的notify()语句换成notifyAll()语句,唤醒所有等待的线程,让这些线程去竞争同步资源,既然是竞争,按理说运行结果也应该是随机的才对啊,为什么我的运行结果每次又都是一样的呢?
结果如下:
Thread-0-<DAVID3>will invoke WAIT()
Thread-1-<DAVID2>will invoke WAIT()
Thread-2-<DAVID1>will invoke WAIT()
Thread-3-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-2-<DAVID0>has been ACTIVED
Thread-2-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-1-<DAVID1>has been ACTIVED
Thread-1-<DAVID1>will invoke WAIT()
Thread-0-<DAVID2>has been ACTIVED
Thread-0-<DAVID2>will invoke WAIT()
package org.arcie.simulator.test;public class NotifyTest implements Runnable {
private Wait wait;
public NotifyTest(Wait wait){
this.wait = wait;
}
public void run() {
wait.doSomthing();
}
public static void main(String [] args){
Wait wait = new Wait(4,"DAVID");
Thread t1 = new Thread(new NotifyTest(wait));
Thread t2 = new Thread(new NotifyTest(wait));
Thread t3 = new Thread(new NotifyTest(wait));
Thread t4 = new Thread(new NotifyTest(wait));
t1.start();
t2.start();
t3.start();
t4.start();
}
} class Wait {
private int counter = 0;
private String name = null;
public Wait(int counter,String name){
this.counter = counter;
this.name = name;
}
public synchronized void doSomthing(){
int tempCounter = --counter;
if(tempCounter <= 0){
customizedNotifyAll();
}
else
{
while(tempCounter > 0){
try {
System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"will invoke WAIT()");
--tempCounter;
wait();
} catch (InterruptedException e) {
e.printStackTrace();
notifyAll();
}
System.out.println(Thread.currentThread().getName()+"-<"+name+tempCounter+">"+"has been ACTIVED");
}
customizedNotifyAll();
}
}
public void customizedNotifyAll(){
notify(); //可以把这句换成notifyAll();
System.out.println(Thread.currentThread().getName()+"-<"+name+counter+">"+"::"+"INVOKED NOTIFYALL() AND FINISHED");
}
} 问题:不是都说notify() 方法是随机唤醒等待的线程吗?但为什么我运行的结果每次都是一样的呢??
运行结果始终是:
Thread-0-<DAVID3>will invoke WAIT()
Thread-1-<DAVID2>will invoke WAIT()
Thread-2-<DAVID1>will invoke WAIT()
Thread-3-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-0-<DAVID2>has been ACTIVED
Thread-0-<DAVID2>will invoke WAIT()还有把public void customizedNotifyAll()方法里的notify()语句换成notifyAll()语句,唤醒所有等待的线程,让这些线程去竞争同步资源,既然是竞争,按理说运行结果也应该是随机的才对啊,为什么我的运行结果每次又都是一样的呢?
结果如下:
Thread-0-<DAVID3>will invoke WAIT()
Thread-1-<DAVID2>will invoke WAIT()
Thread-2-<DAVID1>will invoke WAIT()
Thread-3-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-2-<DAVID0>has been ACTIVED
Thread-2-<DAVID0>::INVOKED NOTIFYALL() AND FINISHED
Thread-1-<DAVID1>has been ACTIVED
Thread-1-<DAVID1>will invoke WAIT()
Thread-0-<DAVID2>has been ACTIVED
Thread-0-<DAVID2>will invoke WAIT()
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货