OSDN Git Service

PR java/16789:
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / libjava.lang / Thread_Monitor.java
1 // Test that monitor locks work and are recursive.
2
3 class T implements Runnable
4 {
5   public int count = 0;
6   Counter c;
7   
8   public T (Counter c)
9   {
10     this.c = c;
11   }
12
13   public void run()
14   {
15     while (true)
16       {
17         // NOTE: double-synchronization here.
18         synchronized (c)
19         {
20           if (c.getCount() <= 100000)
21             count++;
22           else
23             break;
24         }
25       }
26   }
27 }
28
29 class Counter
30 {
31   int i = 0;
32   public synchronized int getCount ()
33   {
34     return ++i; 
35   }
36 }
37
38 public class Thread_Monitor
39 {
40   public static void main(String args[])
41   {
42     Counter c = new Counter();
43     T t1 = new T(c);
44     T t2 = new T(c);
45     
46     Thread th1 = new Thread(t1);
47     Thread th2 = new Thread(t2);
48     th1.start();
49     th2.start();
50     try
51     {
52       th1.join();
53       th2.join();
54     } 
55     catch (InterruptedException x)
56     {
57       System.out.println("failed: Interrupted");
58     }
59     if (t1.count + t2.count == 100000)
60       System.out.println ("ok");
61     else
62       System.out.println ("failed: total count incorrect");
63   }
64 }