java.util.concurrent.locks.Condition.await(1000, TIME.MILLISECONDS) issue

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

java.util.concurrent.locks.Condition.await(1000, TIME.MILLISECONDS) issue

Bhasin, Vishal
java.util.concurrent.locks.Condition.await(1000, TIME.MILLISECONDS) issue

I noticed that using java.util.concurrent.locks.Condition.await(1000, TIME.MILLISECONDS) causes the application to hang for a few seconds (9-10 secs) every so often while running a load test. However, this doesn't happen when I use boolean and use java.util.concurrent.locks.Condition.await() in a while loop until this is set to false. I wonder if anyone has seen this before.

This one causes the lag to happen -

public abstract class BaseSyncService

{

    /** Timeout for process method, default 10 seconds */

    protected int _timeOut = 10 * 1000;

    final Lock lock = new ReentrantLock();

    final Condition notDone  = lock.newCondition();

    protected void releaseMutex()

    {

        lock.lock();

        try {

          notDone.signalAll();

        } finally {

          lock.unlock();

        }

    }

    public Object waitForResponse(int timeout)

        throws InterruptedException

    {

        lock.lock();

        try {

              notDone.await(timeout, TimeUnit.MILLISECONDS);

          } finally {

            lock.unlock();

          }

          return responseObject;

    }

}


The following code works fine



public abstract class BaseSyncService

{

    final Lock lock = new ReentrantLock();

    final Condition notDone  = lock.newCondition();

    protected void releaseMutex()

    {

        lock.lock();

        try {

            waitFlag = false;

          notDone.signalAll();

        } finally {

          lock.unlock();

        }

    }

    boolean waitFlag = true;

    public Object waitForResponse(int timeout)

        throws InterruptedException

    {

        lock.lock();

        try {

            while(waitFlag)

            {

                notDone.await();

            }

          } finally {

            lock.unlock();

          }

          return responseObject;

    }

}

Id appreciate any suggestions..

Thanks,

Vishal Bhasin


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: java.util.concurrent.locks.Condition.await(1000, TIME.MILLISECONDS) issue

David Holmes
java.util.concurrent.locks.Condition.await(1000, TIME.MILLISECONDS) issue
Vishal,
 
I'm not at all clear on exactly what you are reporting. If I understand you right, the code that waits until the condition is signalled works fine, but the code that uses a timeout (and which never actually checks for being signalled) occasionally hangs for 9-10 seconds. Without seeing the code that uses the BaseSyncService it is very hard to get an idea of what might be happening. And I'm assuming that all else is "equal" when you run the two different versions.
 
Can you post more code?
 
David Holmes
 
 
-----Original Message-----
From: [hidden email] [mailto:[hidden email]]On Behalf Of Bhasin, Vishal
Sent: Tuesday, 11 October 2005 7:56 AM
To: Doug Lea; [hidden email]
Subject: [concurrency-interest] java.util.concurrent.locks.Condition.await(1000,TIME.MILLISECONDS) issue

I noticed that using java.util.concurrent.locks.Condition.await(1000, TIME.MILLISECONDS) causes the application to hang for a few seconds (9-10 secs) every so often while running a load test. However, this doesn't happen when I use boolean and use java.util.concurrent.locks.Condition.await() in a while loop until this is set to false. I wonder if anyone has seen this before.

This one causes the lag to happen -

public abstract class BaseSyncService

{

    /** Timeout for process method, default 10 seconds */

    protected int _timeOut = 10 * 1000;

    final Lock lock = new ReentrantLock();

    final Condition notDone  = lock.newCondition();

    protected void releaseMutex()

    {

        lock.lock();

        try {

          notDone.signalAll();

        } finally {

          lock.unlock();

        }

    }

    public Object waitForResponse(int timeout)

        throws InterruptedException

    {

        lock.lock();

        try {

              notDone.await(timeout, TimeUnit.MILLISECONDS);

          } finally {

            lock.unlock();

          }

          return responseObject;

    }

}


The following code works fine



public abstract class BaseSyncService

{

    final Lock lock = new ReentrantLock();

    final Condition notDone  = lock.newCondition();

    protected void releaseMutex()

    {

        lock.lock();

        try {

            waitFlag = false;

          notDone.signalAll();

        } finally {

          lock.unlock();

        }

    }

    boolean waitFlag = true;

    public Object waitForResponse(int timeout)

        throws InterruptedException

    {

        lock.lock();

        try {

            while(waitFlag)

            {

                notDone.await();

            }

          } finally {

            lock.unlock();

          }

          return responseObject;

    }

}

Id appreciate any suggestions..

Thanks,

Vishal Bhasin


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: [concurrency-interest]

wpugh
In reply to this post by Bhasin, Vishal
The code below is broken.

For one thing, if the releaseMutex call occurs before the call to  
occurs before the
call to waitForResponse, the signal will be missed.

Also, await is allowed to return spuriously, so waitForResponse might  
return
even though no signal or timeout has occurred.

Use the boolean variable.

     Bill

On Oct 10, 2005, at 5:55 PM, Bhasin, Vishal wrote:

> I noticed that using java.util.concurrent.locks.Condition.await
> (1000, TIME.MILLISECONDS) causes the application to hang for a few  
> seconds (9-10 secs) every so often while running a load test.  
> However, this doesn't happen when I use boolean and use  
> java.util.concurrent.locks.Condition.await() in a while loop until  
> this is set to false. I wonder if anyone has seen this before.
>
> This one causes the lag to happen -
>
> public abstract class BaseSyncService
>
> {
>
>     /** Timeout for process method, default 10 seconds */
>
>     protected int _timeOut = 10 * 1000;
>
>     final Lock lock = new ReentrantLock();
>
>     final Condition notDone  = lock.newCondition();
>
>     protected void releaseMutex()
>
>     {
>
>         lock.lock();
>
>         try {
>
>           notDone.signalAll();
>
>         } finally {
>
>           lock.unlock();
>
>         }
>
>     }
>
>     public Object waitForResponse(int timeout)
>
>         throws InterruptedException
>
>     {
>
>         lock.lock();
>
>         try {
>
>               notDone.await(timeout, TimeUnit.MILLISECONDS);
>
>           } finally {
>
>             lock.unlock();
>
>           }
>
>           return responseObject;
>
>     }
>
> }
>
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest