Win a copy of The Java Performance Companion this week in the Performance forum!
    Bookmark Topic Watch Topic
  • New Topic

wait in while loop?

 
Vasim Patel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi,

1. Why is it necessary to put a wait() in a while loop. What happens if we don't.
Is it a good programming practise or is it a MUST for wait() to function properly?

2. Also, when is an InterrupedException thrown? Can you anyone give me me a code snippet in which an InterruptedException can be forced/simulated?

regards
Vasim
 
Ashish Vegaraju
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Vasim,

we generally use wait() inside a While when we use threads in our programs.

obviously, if u want to block a process from being executed u have to use wait() method, and use notify() method to start the blocked thread again...hence it is a good and widely accepted programming practice.

Try the InterruptedException example urself...a good hint will be..."try to simulate a deadlock process"

cheers
Ashish.
 
Vasim Patel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks Ashish.


we generally use wait() inside a While when we use threads in our programs.


Can you please be a little more elaborate. And why 'while' and not 'if' or a 'for' block. is there a specific reason why it is a good programming practise.

regards
Vasim
 
Ashish Vegaraju
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi Vasim,

By saying "good programming practice", i dont mean to say that wait() should be used with while. u can use it even with "if" "else", its ur choice.

by saying "good programming practice", i mean that their is no other way to block a thread inside a while loop, so we can only use wait() for that, the question of good or bad programming practice doesnt arise here.

Ashish.
 
Stephen Bloch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Report post to moderator
No, wait should not be used inside an if (unless it is itself inside a while). It is important to check the availability condition after returning from wait, before going on to operate on the shared resource that is allegedly available.

The standard idiom (paraphrasing Joshua Bloch, no relation) is


If you used only an if:

then several Bad Things (tm) could happen:
  • some other thread might have woken up before you did, grabbed the lock on obj, and started playing with it. (I think this can only happen with notifyAll, but I'm not sure);
  • the thread that notifyed you might have (accidentally or maliciously) not really left the object readyToUse;
  • you might be one of several waiting threads that have different criteria for readyToUse, and the thread that notifyAlled you didn't consider itself responsible for checking exactly which threads really should be woken up (that's your problem).
  • you could have woken up even though nobody did a notify. Joshua Bloch says this is rare, but does happen on some JVM implementations.

  •  
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Report post to moderator
    I see this thread is a duplicate of this thread in the Threads forum. Please don't post the same question in multiple forums, as it wastes people's time to reply in two different conversations, not knowing which issues have already been addressed by other posters. Since these questions are specifically about threads, I will close this thread. Followups can go to the other one.
     
      Bookmark Topic Watch Topic
    • New Topic