2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / testsuite / libjava.lang / Thread_Monitor.java
blobf1ffa674c37278e2fec62223755170e6d99f608f
1 // Test that monitor locks work and are recursive.
2 // Origin: Bryce McKinlay <bryce@albatross.co.nz>
4 class T implements Runnable
6 public int count = 0;
7 Counter c;
9 public T (Counter c)
11 this.c = c;
14 public void run()
16 while (true)
18 // NOTE: double-synchronization here.
19 synchronized (c)
21 if (c.getCount() <= 100000)
22 count++;
23 else
24 break;
30 class Counter
32 int i = 0;
33 public synchronized int getCount ()
35 return ++i;
39 public class Thread_Monitor
41 public static void main(String args[])
43 Counter c = new Counter();
44 T t1 = new T(c);
45 T t2 = new T(c);
47 Thread th1 = new Thread(t1);
48 Thread th2 = new Thread(t2);
49 th1.start();
50 th2.start();
51 try
53 th1.join();
54 th2.join();
56 catch (InterruptedException x)
58 System.out.println("failed: Interrupted");
60 if (t1.count + t2.count == 100000)
61 System.out.println ("ok");
62 else
63 System.out.println ("failed: total count incorrect");