blocking calls in java

Discussion in 'Java' started by Gyro, Apr 29, 2005.

  1. Gyro

    Gyro Guest

    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 ?
     
    Gyro, Apr 29, 2005
    #1
    1. Advertisements

  2. Gyro

    Daniel Dyer Guest

    On Fri, 29 Apr 2005 13:22:16 +0100, Gyro <> wrote:

    > 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.

    --
    Daniel Dyer
    http://www.footballpredictions.net
     
    Daniel Dyer, Apr 29, 2005
    #2
    1. Advertisements

  3. Gyro

    Daniel Dyer Guest

    On Fri, 29 Apr 2005 13:52:23 +0100, Daniel Dyer
    <> wrote:

    > private volatile flag = false;


    That should be:

    private volatile boolean flag = false;

    Dan.


    --
    Daniel Dyer
    http://www.footballpredictions.net
     
    Daniel Dyer, Apr 29, 2005
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendra Gunawan
    Replies:
    1
    Views:
    16,951
    Allan Herriman
    Apr 8, 2004
  2. Jonathan Trevor
    Replies:
    6
    Views:
    3,807
    sahar
    Nov 5, 2010
  3. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,280
    Valentin Tihomirov
    Oct 12, 2003
  4. nukleus
    Replies:
    14
    Views:
    1,284
    Chris Uppal
    Jan 22, 2007
  5. Christian
    Replies:
    5
    Views:
    945
    Esmond Pitt
    Dec 2, 2007
  6. Jonathan Trevor
    Replies:
    1
    Views:
    427
    Sami Vaaraniemi
    Feb 12, 2004
  7. Bret Pettichord

    Blocking Calls on Win32 Ruby

    Bret Pettichord, Feb 17, 2004, in forum: Ruby
    Replies:
    6
    Views:
    214
    Shashank Date
    Feb 23, 2004
  8. Serge Savoie
    Replies:
    4
    Views:
    539
    Serge Savoie
    Oct 1, 2008
Loading...