Why notifyAll couldn't wake up waiting threads ?

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; }
}
 
A

A. Bolmarcich

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 ?

The notifyAll is being invoked on an object of class notifypoint. Here is
a copy of the pageAll() method of notifypoint with suggested changes
public synchronized void pageAll() {

Remove "synchronized" on the above line.
System.out.println("\n"+Thread.currentThread().getName()+" will
notify all waiting threads");
synchronized(o) {
notifyAll();

Change the above line to "o.notifyAll()" so that notifyAll() is invoked
on an object on which other threads may be waiting.

Those changes do not guarantee that the other threads are waiting when
notifyAll() is invoked. To do that you have to add code to have
pageAll() wait until the other threads are waiting before invoking
notifyAll().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top