• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait and notify..test

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Basically, what is happening is that you are waiting on the same object used internally to implement join(). The join() method is implemented internally by doing a check on the isAlive() and doing a wait() on the thread object. When a thread completes, one of the tasks, after changing the thread to no longer be alive, is to send a notifyAll() on the thread object.

Or in other words, don't use the Thread object for wait() and notify() -- it is already being used.

Henry



In response to the discussion in www.coderanch.com/t/489800/Programmer-Certification-SCJP/certification/Confusing-wait-notify
Sir,
As we know that before the termination of main thread it waits for the other threads to terminate.. ie.. according to you it will implicitly call join() on each thread object.... that's right

also sir

this code..
view plaincopy to clipboardprint?


class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB();
b.start();

synchronized(b) //1
{
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {}
System.out.println("Tota is: " + b.total);
}
}
}

class ThreadB extends Thread {
int total;


public void run( ) { //2
synchronized(this) {
for(int i=0;i<10;i++) {
total += i;
}

}
}
}




This can result on

wait..
as because it is not guaranteed that every time 1 will get first cpu time to execute.... It may happen that 2 get first and ten it will wait... because ... you are calling wait on the thread that has already terminated

Lets see another modification of this code..
view plaincopy to clipboardprint?


class ThreadA {
public static void main(String [] args) {
ThreadB b = new ThreadB();
b.start();
System.out.println("here 1 ");
System.out.println("here 2 ");
System.out.println("here 3 ");
System.out.println("here 4 ");
synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();
} catch (InterruptedException e) {}
System.out.println("Tota is: " + b.total);
}
}
}

class ThreadB extends Thread {
int total;


public void run( ) {
synchronized(this) {
for(int i=0;i<10;i++) {
total += i;
}

}
}
}




This will result in output

here 1
here 2
here 3
here 4

waiting for b to complete..
....................
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the question is .... ???


Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

narendra bhattacharya wrote:
Sir,
As we know that before the termination of main thread it waits for the other threads to terminate.. ie.. according to you it will implicitly call join() on each thread object.... that's right



Absolutely NOT !!! There is no implicit join(). What I said was, your class is using the same object to wait on as used by the join() implementation, and hence, you will get some notifications intended to handle join(). It absolutely doesn't imply that your thread will do a join() without actually calling join() -- as joining is more than just calling wait.

Henry
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir...
main thread lets assume start first ...ok and then we are creating a new Thread object .. and then call start on that object....now lets assume that there are no statements after Thread start

then what happen as we see in output it (main thread) usually wait for the thread started to complete if it was not like that then certainly main thread is going to terminate..

without waiting for the other thread to complete...
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry , is there a guarantee that the notifyAll() will always be called.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simran Dass wrote:
Henry , is there a guarantee that the notifyAll() will always be called.



Again, this (notifyAll() being called to support join()) is an implementation detail. It is not even guaranteed that this is what will happen -- to have a "guarantee" on an un-intended side effect is just silly.

As for all the examples, where it doesn't receive the notification -- this is happening because the wait() is being called after the thread has already terminated and sent out the notifications. The join() method check the isAlive first, and doesn't wait if the thread isn't, so this isn't an issue for join(). And as I already said, doing wait(), and getting a notification intended for join(), doesn't mean that you are doing an implicit join().

Henry
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic