• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Working of notifyAll()

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LINK: http://www.danchisholm.net/july21/mybook/chapter17/exam2.html



What are the possible results of attempting to compile and run the program?

a. All of the numbers 0 through 9 must always be printed
b. Some or all of the numbers 0 through 9 could be printed
c. Nothing is printed
d. Run-time error

Answers are b and c.
I checked the code and found that, sometimes I get no output, sometimes i get 012345 and sometimes 012345678. So answer seems to be right!

BUT ... My doubt is, notifyAll() when issued, will inform all of the waiting threads about the release of the lock on the object BUT ONLY ONE OF THOSE THREADS WILL BE GIVEN THE CHANCE. Am I right?

If I am right then, definitely ONLY ONE THREAD WILL ACQUIRE THE LOCK now and that thread's name will be printed. Where come "no output" scenario and "0123456" (multiple thread's name) kind of scenario come into picture. Please elucidate! Thanks!
[ October 30, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that, there are total 10 threads created. All threads are alive after notifyAll(). Instead of explaining in words, it is easy to run your program as follows and it should explain you all the facts. Don;t forget to read link

"http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ThreadGroup.html#setDaemon(boolean)"


Try following version:

class A extends Thread {
static int xx = 0;
private Object obj;
public A(Object obj) {
this.obj = obj;
}
public void run() {
try {
synchronized (obj) {
System.out.println("Waiting"+xx);
++xx;
obj.wait();
System.out.println("After Waiting"+Thread.currentThread().getName());
}
} catch (InterruptedException ie) {}
System.out.print(Thread.currentThread().getName());
}
}

class B {
private void m1() {
for (int i = 0; i < 10; i++) {
A t1 = new A(this);
t1.setName(String.valueOf(i));
t1.setDaemon(true);
t1.start();
}
synchronized (this) {
System.out.println("** NOTIFIED **");
notifyAll();
}
}
public static void main(String[] args) {new B().m1();}}
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,
Thanks! Your program was clear to me after I commented the t1.setDaemon(true) statement. Only then,, I was able to visualise what happens exactly.

When wait() is called on an object, all threads wait for the object or I can say that all threads are blocked for the object's access. notifyAll() brings all these threads back to Runnable state from where, there are scheduled by the JVM interleavingly to running state. Right?

Thanks again for your effort!
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I think is happening here:

When for loop in B class is executed, ten objects of class A are created and are on wait.
When notifyAll() is executed, then all ten objects are released.
Please note that, notifyAll() can be execyted at any point of time. So, notifyAll() can be executed after first object is on wait.
It is important to note that notifyAll() can be executed anytime. Based on notifyAll(), objects are released. Again, only those objects are released those are in wait before execution of notifyAll(). Those objects goes on wait after notifyAll(), can not be released and those objects stops when thread ends by setDaemon.

So, when you comment setDaemon command, program goes to wait mode as it is still waiting for some objects to get Notified.

setDaemon can end thread at any point. When this happens, execution stops.

Hope this helps
 
reply
    Bookmark Topic Watch Topic
  • New Topic