• 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

Marcus Green tutorial on Thread

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was going through the Java Certification tutorial downloaded
from Marcus Green's site. I have a question regarding threads based on what he has explained. The relevant text is
reproduced below.
quote
A typical example of using the wait/notify
protocol to allow communication between Threads
appears to involve apparently endless loops such as
//producing code
while(true){
try{
wait();
}catch (InterruptedException e) {}
}
//some producing action goes here
notifyAll();
As true is notorious for staying true this, code looks at first glance like it will just loop forever. The wait method however effectively means give up the lock on the object and wait until the notify or notifyAll method tells you to wake up.
unquote
Okay, so the wait is in a loop that looks like it is an infinite loop but notifyAll() will take care that it does not wait forever. Fine, I got that. My question is, shouldn't the notifyAll() call be within the while loop, after the
catch statement ? How can it be executed unless we exit from the while block ? Can someone explain where I am going wrong here ?
Thanks a ton!
Sajida
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai!
If "notifyAll()" method given inside the "while"loop, it will be never reached, because the "while" loop will continue only after the call of "notifyAll()" method from any other synchronized block on the same Object.
<marquee>s.a.kugan</marquee>
------------------
<i><marquee>s.a.kugan</marquee></i>
 
sajida kal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kugan,
So you mean to say that the notify all should be called within
a sychronized block which is being executed by say thread b.Also thread b is DIFFERENT from say thread a which invoked wait(). (Of course, as you pointed out both threads are executing on the same object)
Is my understanding now complete?
Thanks !
Sajida
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
What is the URL for Marcus Green's site. I also want to download the tutorial.
Thanks in advance.
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus Green's site is www.jchq.net (Think: Java Certification HeadQuarters) Marcus and Paul Wheaton (JavaRanch trailboss) have a friendly rivalry between their sites.
Paul R
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't believe a word of it ... we love JCHQ and Marcus loves javaranch so much he has a lovely link to us on his main page.
It's not so much a friendly rivalry as a mutal appreciation (Although neither Marcus or Paul would admit to this )
 
Zahid, Butt
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link Paul.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the example should be modified to:
boolean someCondition;
while(someCondition){
try{
wait();
}catch(InterruptedException e){}
}
//some processing...
notifyAll();
}
Obviously, if the while loop is infinite, even if its thread did get back the lock, it will still get stuck in the loop again and calls wait() again and relinquishes the lock again.
My two cents.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an excellnt example of this in chapter 7 of RHE. Almost identical in concept, except the while loop is conditional, not infinite.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing, think of the while loop as a way to check for certain conditions, if those conditions are not met(false), the thread will keep on waiting. Once the conditions become true, you go right down to normal processing. Don't forget to call notifyAll while the thread still has the lock.
 
sajida kal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron,
Thanks for your comments and the pointer to the RHE chapter. I don't have that book, but I shall try and check it out.
Will need to code a few examples to get the concept straight.
Thanks!
Sajida
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic