1 // Create two threads waiting on a monitor. Interrupt one of them. Does the
2 // other wake up correctly?
3 // Origin: Bryce McKinlay <bryce@albatross.co.nz>
5 class Waiter
extends Thread
9 boolean interrupted
= false;
10 boolean notified
= false;
12 Waiter (Object monitor
, int thread_num
)
14 this.monitor
= monitor
;
15 this.thread_num
= thread_num
;
20 synchronized (monitor
)
22 System
.out
.println ("Thread waiting.");
25 long start
= System
.currentTimeMillis();
27 long time
= System
.currentTimeMillis() - start
;
29 System
.out
.println ("Error: wait on thread " + thread_num
34 catch (InterruptedException x
)
43 public class Thread_Wait_Interrupt
45 public static void main(String args
[])
47 Object monitor
= new Object();
48 Waiter w1
= new Waiter(monitor
, 1);
49 Waiter w2
= new Waiter(monitor
, 2);
56 synchronized (monitor
)
64 System
.out
.println("join ok");
65 System
.out
.println("Thread 1 " +
66 (w1
.interrupted ?
"interrupted ok" : "error"));
67 System
.out
.println("Thread 2 " +
68 (w2
.notified ?
"notified ok" : "error"));
71 catch (InterruptedException x
)
73 System
.out
.println (x
);