L
lonelyplanet999
Hi,
I wrote below program to test functionality of notifyAll(). From
program execution output, I found all threads nt1,2,3,4,5 entered wait
state after started. Also, the notifyAll() method invoked from
np.pageAll() was executed. However, no thread was notified at all.
In the last five if statements, I checked the Dum object referenced by
np & nt1,2,3,4,5 all referred the same object. If so, why the
notifyAll didn't work ?
Tks
public class NotifyAll2 {
public static void main (String [] args) {
Dum o = new Dum();
nThread nt1 = new nThread("nt1",o);
nThread nt2 = new nThread("nt2",o);
nThread nt3 = new nThread("nt3",o);
nThread nt4 = new nThread("nt4",o);
nThread nt5 = new nThread("nt5",o);
nt1.start();
nt2.start();
nt3.start();
nt4.start();
nt5.start();
notifypoint np = new notifypoint(o);
np.pageAll();
if (np.returnDum()==nt1.returnDum())
System.out.println("\nnp.Dum==nt1.Dum!");
if (np.returnDum()==nt2.returnDum())
System.out.println("\nnp.Dum==nt2.Dum!");
if (np.returnDum()==nt3.returnDum())
System.out.println("\nnp.Dum==nt3.Dum!");
if (np.returnDum()==nt4.returnDum())
System.out.println("\nnp.Dum==nt4.Dum!");
if (np.returnDum()==nt5.returnDum())
System.out.println("\nnp.Dum==nt5.Dum!");
}
}
class Dum {}
class notifypoint {
private Dum o;
notifypoint (Dum o) {
this.o=o;
}
public synchronized void pageAll() {
System.out.println("\n"+Thread.currentThread().getName()+" will
notify all waiting threads");
synchronized(o) {
notifyAll();
}
}
Dum returnDum() { return o; }
}
class nThread extends Thread {
private String name;
private Dum waitfor;
nThread(String s, Dum o) {
name=s;
waitfor=o;
}
public void run() {
System.out.print(name+" starts, ");
synchronized(waitfor) {
try {
System.out.print(name+" wait ");
waitfor.wait();
} catch(InterruptedException e) {}
System.out.println("\n"+name+" notified");
}
}
Dum returnDum() { return waitfor; }
}
I wrote below program to test functionality of notifyAll(). From
program execution output, I found all threads nt1,2,3,4,5 entered wait
state after started. Also, the notifyAll() method invoked from
np.pageAll() was executed. However, no thread was notified at all.
In the last five if statements, I checked the Dum object referenced by
np & nt1,2,3,4,5 all referred the same object. If so, why the
notifyAll didn't work ?
Tks
public class NotifyAll2 {
public static void main (String [] args) {
Dum o = new Dum();
nThread nt1 = new nThread("nt1",o);
nThread nt2 = new nThread("nt2",o);
nThread nt3 = new nThread("nt3",o);
nThread nt4 = new nThread("nt4",o);
nThread nt5 = new nThread("nt5",o);
nt1.start();
nt2.start();
nt3.start();
nt4.start();
nt5.start();
notifypoint np = new notifypoint(o);
np.pageAll();
if (np.returnDum()==nt1.returnDum())
System.out.println("\nnp.Dum==nt1.Dum!");
if (np.returnDum()==nt2.returnDum())
System.out.println("\nnp.Dum==nt2.Dum!");
if (np.returnDum()==nt3.returnDum())
System.out.println("\nnp.Dum==nt3.Dum!");
if (np.returnDum()==nt4.returnDum())
System.out.println("\nnp.Dum==nt4.Dum!");
if (np.returnDum()==nt5.returnDum())
System.out.println("\nnp.Dum==nt5.Dum!");
}
}
class Dum {}
class notifypoint {
private Dum o;
notifypoint (Dum o) {
this.o=o;
}
public synchronized void pageAll() {
System.out.println("\n"+Thread.currentThread().getName()+" will
notify all waiting threads");
synchronized(o) {
notifyAll();
}
}
Dum returnDum() { return o; }
}
class nThread extends Thread {
private String name;
private Dum waitfor;
nThread(String s, Dum o) {
name=s;
waitfor=o;
}
public void run() {
System.out.print(name+" starts, ");
synchronized(waitfor) {
try {
System.out.print(name+" wait ");
waitfor.wait();
} catch(InterruptedException e) {}
System.out.println("\n"+name+" notified");
}
}
Dum returnDum() { return waitfor; }
}