ce961468b7
2004-07-27 Bryce McKinlay <mckinlay@redhat.com> * testsuite/libjava.lang/TLtest.java: Reduce sleep time. * testsuite/libjava.lang/Thread_Alive.java: Remove old email address. Reduce sleep time. * testsuite/libjava.lang/Thread_HoldsLock.java: Modify to work around compiler bug. * testsuite/libjava.lang/Thread_Interrupt.java: Remove old email address. Reduce sleep times. Synchronize with target threads before attempting to interrupt them. Don't try to calibrate yeild count, instead, always loop for a fixed time. * testsuite/libjava.lang/Thread_Join.java: Remove old email address. * testsuite/libjava.lang/Thread_Monitor.java: Likewise. * testsuite/libjava.lang/Thread_Wait.java: Likewise. * testsuite/libjava.lang/Thread_Wait_2.java: Likewise. * testsuite/libjava.lang/Thread_Wait_Interrupt.java: Likewise. * testsuite/libjava.lang/pr179.java: Likewise. * testsuite/libjava.lang/Thread_Sleep.java: Likewise. Reduce sleep time. Remove upper bounds check on sleep time. From-SVN: r85248
157 lines
3.0 KiB
Java
157 lines
3.0 KiB
Java
// Test interrupt() behaviour on a thread in wait(), sleep(), and spinning
|
|
// in a loop.
|
|
|
|
class ThreadBase extends Thread
|
|
{
|
|
boolean ready = false;
|
|
|
|
synchronized void ready()
|
|
{
|
|
ready = true;
|
|
}
|
|
}
|
|
|
|
class Waiter extends ThreadBase
|
|
{
|
|
public synchronized void run()
|
|
{
|
|
super.ready();
|
|
System.out.println ("wait()");
|
|
try
|
|
{
|
|
wait();
|
|
System.out.println("Error: wait() completed normally.");
|
|
}
|
|
catch (InterruptedException x)
|
|
{
|
|
if (isInterrupted() || interrupted())
|
|
System.out.println("Error: interrupt flag is still set.");
|
|
|
|
}
|
|
System.out.println("interrupted - ok");
|
|
}
|
|
}
|
|
|
|
class Sleeper extends ThreadBase
|
|
{
|
|
public void run()
|
|
{
|
|
super.ready();
|
|
System.out.println ("sleep()");
|
|
try
|
|
{
|
|
sleep(5000);
|
|
System.out.println("Error: sleep() completed normally.");
|
|
}
|
|
catch (InterruptedException x)
|
|
{
|
|
if (isInterrupted() || interrupted())
|
|
System.out.println("Error: interrupt flag is still set.");
|
|
|
|
System.out.println("interrupted - ok");
|
|
}
|
|
}
|
|
}
|
|
|
|
class Looper extends ThreadBase
|
|
{
|
|
public void run()
|
|
{
|
|
super.ready();
|
|
System.out.println ("Busy waiting");
|
|
|
|
int count = 0;
|
|
long start = System.currentTimeMillis();
|
|
while (true)
|
|
{
|
|
Thread.yield();
|
|
if (isInterrupted ())
|
|
break;
|
|
long now = System.currentTimeMillis();
|
|
if ((now - start) > 5000)
|
|
break;
|
|
}
|
|
synchronized (this)
|
|
{
|
|
if (interrupted ())
|
|
{
|
|
System.out.println ("interrupted - ok");
|
|
if (isInterrupted () || interrupted ())
|
|
System.out.println("Error: interrupt flag is still set.");
|
|
}
|
|
else
|
|
System.out.println ("Error: Busy wait was not interrupted.");
|
|
}
|
|
}
|
|
}
|
|
|
|
class Joiner extends ThreadBase
|
|
{
|
|
public void run()
|
|
{
|
|
super.ready();
|
|
System.out.println("join()");
|
|
try
|
|
{
|
|
join(2000);
|
|
System.out.println("Error: join() completed normally??!");
|
|
}
|
|
catch (InterruptedException x)
|
|
{
|
|
if (isInterrupted() || interrupted())
|
|
System.out.println("Error: interrupt flag is still set.");
|
|
|
|
System.out.println("interrupted - ok");
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public class Thread_Interrupt
|
|
{
|
|
public static void main(String args[])
|
|
{
|
|
Waiter w = new Waiter();
|
|
w.start ();
|
|
sleep_and_interrupt (w);
|
|
|
|
Sleeper s = new Sleeper();
|
|
s.start ();
|
|
sleep_and_interrupt (s);
|
|
|
|
Looper l = new Looper ();
|
|
l.start ();
|
|
sleep_and_interrupt (l);
|
|
|
|
Joiner j = new Joiner ();
|
|
j.start ();
|
|
sleep_and_interrupt (j);
|
|
}
|
|
|
|
public static void sleep_and_interrupt(ThreadBase t)
|
|
{
|
|
try
|
|
{
|
|
synchronized (t)
|
|
{
|
|
while (!t.ready)
|
|
t.wait(10);
|
|
}
|
|
|
|
Thread.sleep (50);
|
|
t.interrupt ();
|
|
long t1 = System.currentTimeMillis();
|
|
t.join (5000);
|
|
long time = System.currentTimeMillis() - t1;
|
|
if (time > 2900)
|
|
{
|
|
System.out.println ("Error: join() from main thread timed out");
|
|
}
|
|
}
|
|
catch (InterruptedException x)
|
|
{
|
|
System.out.println("Error: main thread interrupted.");
|
|
}
|
|
}
|
|
}
|