2000-03-23 13:35:44 +01:00
|
|
|
// Test interrupt() behaviour on a thread in wait(), sleep(), and spinning
|
|
|
|
// in a loop.
|
|
|
|
|
2004-07-28 04:44:06 +02:00
|
|
|
class ThreadBase extends Thread
|
|
|
|
{
|
|
|
|
boolean ready = false;
|
|
|
|
|
|
|
|
synchronized void ready()
|
|
|
|
{
|
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Waiter extends ThreadBase
|
2000-03-23 13:35:44 +01:00
|
|
|
{
|
|
|
|
public synchronized void run()
|
|
|
|
{
|
2004-07-28 04:44:06 +02:00
|
|
|
super.ready();
|
2000-03-23 13:35:44 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-28 04:44:06 +02:00
|
|
|
class Sleeper extends ThreadBase
|
2000-03-23 13:35:44 +01:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
2004-07-28 04:44:06 +02:00
|
|
|
super.ready();
|
2000-03-23 13:35:44 +01:00
|
|
|
System.out.println ("sleep()");
|
|
|
|
try
|
|
|
|
{
|
2004-07-28 04:44:06 +02:00
|
|
|
sleep(5000);
|
2000-03-23 13:35:44 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-28 04:44:06 +02:00
|
|
|
class Looper extends ThreadBase
|
2000-03-23 13:35:44 +01:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
2004-07-28 04:44:06 +02:00
|
|
|
super.ready();
|
2000-03-23 13:35:44 +01:00
|
|
|
System.out.println ("Busy waiting");
|
|
|
|
|
|
|
|
int count = 0;
|
2004-07-28 04:44:06 +02:00
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
while (true)
|
2000-03-23 13:35:44 +01:00
|
|
|
{
|
|
|
|
Thread.yield();
|
|
|
|
if (isInterrupted ())
|
|
|
|
break;
|
2004-07-28 04:44:06 +02:00
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
if ((now - start) > 5000)
|
|
|
|
break;
|
2000-03-23 13:35:44 +01:00
|
|
|
}
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-28 04:44:06 +02:00
|
|
|
class Joiner extends ThreadBase
|
2000-03-23 13:35:44 +01:00
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
2004-07-28 04:44:06 +02:00
|
|
|
super.ready();
|
2000-03-23 13:35:44 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-07-28 04:44:06 +02:00
|
|
|
public static void sleep_and_interrupt(ThreadBase t)
|
2000-03-23 13:35:44 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2004-07-28 04:44:06 +02:00
|
|
|
synchronized (t)
|
|
|
|
{
|
|
|
|
while (!t.ready)
|
|
|
|
t.wait(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.sleep (50);
|
2000-03-23 13:35:44 +01:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|