M
mikew01
I am trying to write a method that detects when a thread has timed out
waiting for a resource, for brevity I have included the important
parts below in the lock method.
I am finding that when I specify a value of around 6000ms and above
the period is calculated lower than the timeout which results in the
code thinking that the thread has not timed out waiting but has simply
been released from a notify.
It appears that either wait( timeout ) is releasing early or I am not
using the correct method for gauging the time to obtain period.
Any ideas?
Thanks
public void lock( Object requester, long timeout ) throws
InterruptedException
{
long start = System.currentTimeMillis();
synchronized ( requester )
{
requester.wait( timeout );
}
long period = System.currentTimeMillis() - start;
if ( period >= timeout )
{
System.out.println( "Timed out waiting for resource: " +
period );
}
}
waiting for a resource, for brevity I have included the important
parts below in the lock method.
I am finding that when I specify a value of around 6000ms and above
the period is calculated lower than the timeout which results in the
code thinking that the thread has not timed out waiting but has simply
been released from a notify.
It appears that either wait( timeout ) is releasing early or I am not
using the correct method for gauging the time to obtain period.
Any ideas?
Thanks
public void lock( Object requester, long timeout ) throws
InterruptedException
{
long start = System.currentTimeMillis();
synchronized ( requester )
{
requester.wait( timeout );
}
long period = System.currentTimeMillis() - start;
if ( period >= timeout )
{
System.out.println( "Timed out waiting for resource: " +
period );
}
}