my dog learned polymorphism*
The moose likes Java in General and the fly likes wait in while loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

wait in while loop?

Vasim Patel
Ranch Hand

Joined: Apr 29, 2004
Posts: 87
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

Joined: Aug 19, 2004
Posts: 47
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

Joined: Apr 29, 2004
Posts: 87
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

Joined: Aug 19, 2004
Posts: 47
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

Joined: Aug 19, 2003
Posts: 48
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.


  • SCJP 1.4
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    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.


    "I'm not back." - Bill Harding, Twister
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: wait in while loop?
     
    Similar Threads
    Having trouble getting to while loop
    Can i make a program wait?
    Thread (if or while loop)
    for loop within a while?
    wait in while loop?