Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / testsuite / libjava.lang / Thread_Wait_Interrupt.java
blob609527cda9963e78a09e9b684b7461fdf261cd1e
1 // Create two threads waiting on a monitor. Interrupt one of them. Does the
2 // other wake up correctly?
4 class Waiter extends Thread
6 Object monitor;
7 int thread_num;
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;
17 public void run()
19 synchronized (monitor)
21 System.out.println ("Thread waiting.");
22 try
24 long start = System.currentTimeMillis();
25 monitor.wait(1000);
26 long time = System.currentTimeMillis() - start;
27 if (time > 990)
28 System.out.println ("Error: wait on thread " + thread_num
29 + " timed out.");
30 else
31 notified = true;
33 catch (InterruptedException x)
35 interrupted = true;
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);
49 w1.start();
50 w2.start();
51 try
53 Thread.sleep(250);
55 synchronized (monitor)
57 w1.interrupt();
58 monitor.notify();
61 w1.join();
62 w2.join();
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);