|
Last change
on this file since 3103 was 2, checked in by bird, 23 years ago |
|
Initial revision
|
-
Property cvs2svn:cvs-rev
set to
1.1
-
Property svn:eol-style
set to
native
-
Property svn:executable
set to
*
|
|
File size:
698 bytes
|
| Line | |
|---|
| 1 | // Test atomic increment via synchronized blocks.
|
|---|
| 2 | public class SyncTest implements Runnable {
|
|---|
| 3 | static int counter;
|
|---|
| 4 |
|
|---|
| 5 | public void run() {
|
|---|
| 6 | for (int n = 0; n < 1000000; n++)
|
|---|
| 7 | synchronized (SyncTest.class) {
|
|---|
| 8 | counter++;
|
|---|
| 9 | }
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | public static void main(String[] args) {
|
|---|
| 13 | SyncTest test = new SyncTest();
|
|---|
| 14 | Thread[] thr = new Thread[4];
|
|---|
| 15 |
|
|---|
| 16 | for (int n = 0; n < thr.length; n++) {
|
|---|
| 17 | thr[n] = new Thread(test);
|
|---|
| 18 | thr[n].start();
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | for (int n = 0; n < thr.length; n++) {
|
|---|
| 22 | try {
|
|---|
| 23 | thr[n].join();
|
|---|
| 24 | } catch (InterruptedException ex) {
|
|---|
| 25 | }
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | System.out.println(counter == 1000000 * thr.length ?
|
|---|
| 29 | "ok" : "fail: " + counter);
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.