1 // Test interrupt() behaviour on a thread in wait(), sleep(), and spinning
4 class ThreadBase
extends Thread
8 synchronized void ready()
14 class Waiter
extends ThreadBase
16 public synchronized void run()
19 System
.out
.println ("wait()");
23 System
.out
.println("Error: wait() completed normally.");
25 catch (InterruptedException x
)
27 if (isInterrupted() || interrupted())
28 System
.out
.println("Error: interrupt flag is still set.");
31 System
.out
.println("interrupted - ok");
35 class Sleeper
extends ThreadBase
40 System
.out
.println ("sleep()");
44 System
.out
.println("Error: sleep() completed normally.");
46 catch (InterruptedException x
)
48 if (isInterrupted() || interrupted())
49 System
.out
.println("Error: interrupt flag is still set.");
51 System
.out
.println("interrupted - ok");
56 class Looper
extends ThreadBase
61 System
.out
.println ("Busy waiting");
64 long start
= System
.currentTimeMillis();
70 long now
= System
.currentTimeMillis();
71 if ((now
- start
) > 5000)
78 System
.out
.println ("interrupted - ok");
79 if (isInterrupted () || interrupted ())
80 System
.out
.println("Error: interrupt flag is still set.");
83 System
.out
.println ("Error: Busy wait was not interrupted.");
88 class Joiner
extends ThreadBase
93 System
.out
.println("join()");
97 System
.out
.println("Error: join() completed normally??!");
99 catch (InterruptedException x
)
101 if (isInterrupted() || interrupted())
102 System
.out
.println("Error: interrupt flag is still set.");
104 System
.out
.println("interrupted - ok");
110 public class Thread_Interrupt
112 public static void main(String args
[])
114 Waiter w
= new Waiter();
116 sleep_and_interrupt (w
);
118 Sleeper s
= new Sleeper();
120 sleep_and_interrupt (s
);
122 Looper l
= new Looper ();
124 sleep_and_interrupt (l
);
126 Joiner j
= new Joiner ();
128 sleep_and_interrupt (j
);
131 public static void sleep_and_interrupt(ThreadBase t
)
143 long t1
= System
.currentTimeMillis();
145 long time
= System
.currentTimeMillis() - t1
;
148 System
.out
.println ("Error: join() from main thread timed out");
151 catch (InterruptedException x
)
153 System
.out
.println("Error: main thread interrupted.");