aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify()" Watch "notify()" New topic
Author

notify()

manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
Please observe the following code:

This code works fine, even after I delete notify() in the bottom of the code. Is it not required to notify back to where the wait() method is called after completing it's job in run(). I thought, it would throw some error. Can any one explain ??
thanks
[ June 29, 2002: Message edited by: Murthy Ram ]

MT
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
first off, correct me if I am wrong folks.
Okay, wait() is not same as sleep(). Wait() is simply asking folks (other thread) around you to take over the resources you were using. If no one is around you (i.e. no other thread), then JVM would just put the resources right back into your hand.
In this case, I believe you only start one thread to run the ThreadB object. Since there is only one thread, when you request wait() the JVM finds no other threads waiting for the synchronized object, thus it puts the thread back into action. That is why your notify doesn't seem to be doing anything.
I hope this (1) is correct (2) is helpful


Let us be showered in the light of confusion!
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
I am sorry, but I don't see the point of your synchronized code block in your ThreadA1 class. It doesn't seem right...
I'll work on this and see what is it that I feel wrong (I do hope its not me ), would some body take a look at this and tell me what is wrong?
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
I wrote the following code, compile them as ThreadA1 file and run them. I hope this would help.

Okay, here is what happened. notify() and wait() would only make sense if you have more than one thread running same object (its like bunch folks working with one computer). I create one Runnable object and use it as my limited resource (i.e. the one computer). Next I got two threads (i.e. bunch folks). I synchronized the codes that does things (there are other variations on what to synchronize) so that I won't have any thread collision. Then, I have the thread pause ( the wait() method ) midway through the for loop and let the other thread run. Notice how I notify in the for loop? Basically, the threads continousely notify any one waitting (i.e. other threads that has execute the wait() method). However, because the codes are synchronized, even know other threads are notified they can't run since the active thread has not let go of the lock.
Look at the out put and compare it with the codes, you'll figure it out.
[ June 29, 2002: Message edited by: Chung Huang ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I agree with Murthy. Theoretically if notify is not called the waiting thread should be waiting for ever.
It is possible to see this behaviour commenting out b.start();
The code is behaving AS IF once the thread b ends notify were called. This seems a bug.
Just a test: comment out b.start() and notify() , but call b.run() instead. Yes, now the expected behaviour ocurrs, the program doesn't end.
I have found two related bug reports in Sun's database: 4308396 and 4022532.
The solution is to use wait and notify "properly" as The Java Tutorial states, within a condition variable loop. Within this loop if a, let's say, spurious notify happens the thread will still continue waiting because the condition is not satisfied.


SCJP2. Please Indent your code using UBB Code
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
I also tested the code for a while last night, and it does seem like notify() or notifyAll() is automatically called after b thread dies. However, when I reverse the scenario, so that b thread calls wait() and the main thread runs the loop and exits, b thread waits forever after the main thread exits, just as expected. I could not find any documentation that supports the first senario where a thread is notified without any explicit calls to the notify methods. Most likely a bug or some kind of undocumented safety feature in the JVM


SCJP2
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
Thanks Chaung!!! thats a nice explanation..
Hence, what do we have to do , if such Questions come in the exam??
please give me some guideliines..
thanks
Murthy
[ July 01, 2002: Message edited by: Murthy Ram ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Don't be afraid of these kind of questions appearing in the exam. It is the interest of Sun not showing the weaknesses or drawbacks of either the language or the compiler.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify()
 
Similar Threads
Concept of locks and synchronization !
Multithreading: How can total be nearly 5,000 even though loop is up to 100?
wait and notify problem from K&B.
problem with wait()- notify()
notify(); doesn't work?