Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2005

Tip: Looking for answers? Try searching our database.

blocking calls in java

Thread view: 
Gyro - 29 Apr 2005 13:22 GMT
Hello, I need to block current thread, waiting for flag/event. Plz give advice.

My attempts:

public synchronized void blockingFormCreate() {
   createAndShowForm();     // Create and show screen form
   wait();                  // Fall asleep
   . . .
}

public synchronized void setEvent() {
   notify();               // wakeup, continue processing
}

volatile boolean event;  // field

private void blockingFormCreate() {
   event = false;
   createAndShowForm();     // Make a form
   for(; !flag; yield()) ;  // Blocking
   . . .
}

where I`m correct/incorrect ?
Daniel Dyer - 29 Apr 2005 13:52 GMT
> Hello, I need to block current thread, waiting for flag/event. Plz give  
> advice.

...

> where I`m correct/incorrect ?

Generally you should call wait in a loop (the thread may be notified  
before you want wait to return).  You should usually call notifyAll rather  
than notify, unless you have a good reason not to.  A thread must own an  
object's monitor to call wait or notify, so you need to synchronize these  
calls (not necessarily the whole method, but you need to make sure the  
flag does not change between the evaluation of the while condition and the  
call to wait).

This should work:

private volatile flag = false;

public synchronized void blockingCall()
{
    while (!flag) // Loop so this method doesn't return before the flag is  
true.
    {
        try
        {
            wait();
        }
        catch (InterruptedException ex)
        {
            // Ignore and loop again until the flag is true.
        }
    }
}

/**
 * Call this from another thread while the above method is blocking
 * to make it return.
 */
public synchronized void unblock()
{
    flag = true;
    notifyAll();
}

Dan.

Signature

Daniel Dyer
http://www.footballpredictions.net

Daniel Dyer - 29 Apr 2005 13:56 GMT
> private volatile flag = false;

That should be:

    private volatile boolean flag = false;

Dan.

Signature

Daniel Dyer
http://www.footballpredictions.net



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.