2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / testsuite / libjava.lang / Thread_Interrupt.java
blobcb569c827c4aa8be028b19cdced4b9125b7a18fb
1 // Test interrupt() behaviour on a thread in wait(), sleep(), and spinning
2 // in a loop.
3 // Origin: Bryce McKinlay <bryce@albatross.co.nz>
5 class Waiter extends Thread
7 public synchronized void run()
9 System.out.println ("wait()");
10 try
12 wait();
13 System.out.println("Error: wait() completed normally.");
15 catch (InterruptedException x)
17 if (isInterrupted() || interrupted())
18 System.out.println("Error: interrupt flag is still set.");
21 System.out.println("interrupted - ok");
25 class Sleeper extends Thread
27 public void run()
29 System.out.println ("sleep()");
30 try
32 sleep(2000);
33 System.out.println("Error: sleep() completed normally.");
35 catch (InterruptedException x)
37 if (isInterrupted() || interrupted())
38 System.out.println("Error: interrupt flag is still set.");
40 System.out.println("interrupted - ok");
45 class Looper extends Thread
47 // Return the number of Thread.yield()s we can do in 500ms.
48 static long calibrate ()
50 long i = 1;
52 for (int tries = 0; tries < 40; tries++)
54 long t = System.currentTimeMillis();
55 for (long n = 0; n < i; n++)
56 Thread.yield();
57 long t_prime = System.currentTimeMillis();
58 if (t_prime - t > 500)
59 return i;
60 i *= 2;
62 // We have no system clock. Give up.
63 throw new RuntimeException ("We have no system clock.");
66 static long yields = calibrate ();
68 public void run()
70 System.out.println ("Busy waiting");
72 int count = 0;
73 for (long i=0; i < yields; i++)
75 Thread.yield();
76 count += 5;
77 if (isInterrupted ())
78 break;
80 synchronized (this)
82 if (interrupted ())
84 System.out.println ("interrupted - ok");
85 if (isInterrupted () || interrupted ())
86 System.out.println("Error: interrupt flag is still set.");
88 else
89 System.out.println ("Error: Busy wait was not interrupted.");
94 class Joiner extends Thread
96 public void run()
98 System.out.println("join()");
99 try
101 join(2000);
102 System.out.println("Error: join() completed normally??!");
104 catch (InterruptedException x)
106 if (isInterrupted() || interrupted())
107 System.out.println("Error: interrupt flag is still set.");
109 System.out.println("interrupted - ok");
115 public class Thread_Interrupt
117 public static void main(String args[])
119 Waiter w = new Waiter();
120 w.start ();
121 sleep_and_interrupt (w);
123 Sleeper s = new Sleeper();
124 s.start ();
125 sleep_and_interrupt (s);
127 Looper l = new Looper ();
128 l.start ();
129 sleep_and_interrupt (l);
131 Joiner j = new Joiner ();
132 j.start ();
133 sleep_and_interrupt (j);
136 public static void sleep_and_interrupt(Thread t)
140 Thread.sleep (250);
141 t.interrupt ();
142 long t1 = System.currentTimeMillis();
143 t.join (5000);
144 long time = System.currentTimeMillis() - t1;
145 if (time > 2900)
147 System.out.println ("Error: join() from main thread timed out");
150 catch (InterruptedException x)
152 System.out.println("Error: main thread interrupted.");