1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.issue;
15
16
17
18
19
20
21
22 public class NoLockingInJava implements Runnable {
23
24 static int THREAD_COUNT = 5;
25 static Object LOCK = new Object();
26 static NoLockingInJava[] RUNNABLE_ARRAY = new NoLockingInJava[THREAD_COUNT];
27 static Thread[] THREAD_ARRAY = new Thread[THREAD_COUNT];
28
29 private int counter = 0;
30 private boolean done = false;
31
32 public static void main(String args[]) throws InterruptedException {
33 printEnvironmentInfo();
34 execute();
35 printResults();
36 }
37
38 public static void printEnvironmentInfo() {
39 System.out.println("java.runtime.version = " + System.getProperty("java.runtime.version"));
40 System.out.println("java.vendor = " + System.getProperty("java.vendor"));
41 System.out.println("java.version = " + System.getProperty("java.version"));
42 System.out.println("os.name = " + System.getProperty("os.name"));
43 System.out.println("os.version = " + System.getProperty("os.version"));
44 }
45
46 public static void execute() throws InterruptedException {
47 for (int i = 0; i < THREAD_COUNT; i++) {
48 RUNNABLE_ARRAY[i] = new NoLockingInJava();
49 THREAD_ARRAY[i] = new Thread(RUNNABLE_ARRAY[i]);
50 }
51 for (Thread t : THREAD_ARRAY) {
52 t.start();
53 }
54
55 Thread.sleep(10000);
56
57 for (int i = THREAD_COUNT - 1; i <= 0; i--) {
58 RUNNABLE_ARRAY[i].done = true;
59 }
60
61 }
62
63 public static void printResults() {
64 for (int i = 0; i < RUNNABLE_ARRAY.length; i++) {
65 System.out.println("runnable[" + i + "]: " + RUNNABLE_ARRAY[i]);
66 }
67 }
68
69 public void run() {
70 for (;;) {
71 counter++;
72 if (done) {
73 return;
74 }
75 }
76 }
77
78 public String toString() {
79 return "counter=" + counter;
80 }
81
82 }