OSDN Git Service

PR java/16789:
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / libjava.lang / SyncTest.java
1 // Test atomic increment via synchronized blocks.
2 public class SyncTest implements Runnable {
3   static int counter;
4
5   public void run() {
6     // We cache the .class value; otherwise this code is
7     // slow enough that it will time out in some situations.
8     Object lock = SyncTest.class;
9     for (int n = 0; n < 1000000; n++)
10       synchronized (lock) {
11         counter++;
12       }
13   }
14
15   public static void main(String[] args) {
16     SyncTest test = new SyncTest();
17     Thread[] thr = new Thread[4];
18
19     for (int n = 0; n < thr.length; n++) {
20       thr[n] = new Thread(test);
21       thr[n].start();
22     }
23
24     for (int n = 0; n < thr.length; n++) {
25       try {
26         thr[n].join();
27       } catch (InterruptedException ex) {
28       }
29     }
30
31     System.out.println(counter == 1000000 * thr.length ?
32       "ok" : "fail: " + counter);
33   }
34 }