1 // Create two threads waiting on a monitor. Interrupt one of them. Does the
2 // other wake up correctly?
4 class Waiter
extends Thread
8 boolean interrupted
= false;
9 boolean notified
= false;
11 Waiter (Object monitor
, int thread_num
)
13 this.monitor
= monitor
;
14 this.thread_num
= thread_num
;
19 synchronized (monitor
)
21 System
.out
.println ("Thread waiting.");
24 long start
= System
.currentTimeMillis();
26 long time
= System
.currentTimeMillis() - start
;
28 System
.out
.println ("Error: wait on thread " + thread_num
33 catch (InterruptedException x
)
42 public class Thread_Wait_Interrupt
44 public static void main(String args
[])
46 Object monitor
= new Object();
47 Waiter w1
= new Waiter(monitor
, 1);
48 Waiter w2
= new Waiter(monitor
, 2);
55 synchronized (monitor
)
63 System
.out
.println("join ok");
64 System
.out
.println("Thread 1 " +
65 (w1
.interrupted ?
"interrupted ok" : "error"));
66 System
.out
.println("Thread 2 " +
67 (w2
.notified ?
"notified ok" : "error"));
70 catch (InterruptedException x
)
72 System
.out
.println (x
);