| Author |
wait and notify..test
|
narendra bhattacharya
Ranch Hand
Joined: Feb 17, 2010
Posts: 66
|
|
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..
....................
|
SCJP1.6,SCWCD1.5
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16809
|
|
And the question is .... ???
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16809
|
|
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
Joined: Feb 17, 2010
Posts: 66
|
|
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...
|
 |
Simran Dass
Ranch Hand
Joined: Jan 09, 2010
Posts: 183
|
|
Henry , is there a guarantee that the notifyAll() will always be called.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16809
|
|
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
|
 |
 |
|
|
subject: wait and notify..test
|
|
|