Skip to content

Commit c7c3e14

Browse files
committed
Verifying synchronized keyword
1 parent d998aad commit c7c3e14

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
*
3+
*/
4+
package com.sri.synchronisedTest;
5+
6+
/**
7+
* @author Sri
8+
*
9+
* Created on Apr 9, 2020
10+
*/
11+
public class TestSynchronised {
12+
13+
private static int counter = 0;
14+
15+
public static void main(String[] args) {
16+
process();
17+
System.out.println(counter);
18+
}
19+
20+
public static synchronized void increment() {
21+
++counter;
22+
23+
}
24+
25+
public static void process() {
26+
27+
Thread thread1 = new Thread(new Runnable() {
28+
29+
@Override
30+
public void run() {
31+
for (int i = 0; i < 100; i++) {
32+
increment();
33+
}
34+
}
35+
});
36+
37+
Thread thread2 = new Thread(new Runnable() {
38+
39+
@Override
40+
public void run() {
41+
for (int i = 0; i < 100; i++) {
42+
increment();
43+
}
44+
}
45+
});
46+
47+
thread1.start();
48+
thread2.start();
49+
50+
try {
51+
thread1.join();
52+
thread2.join();
53+
} catch (InterruptedException e) {
54+
// TODO Auto-generated catch block
55+
e.printStackTrace();
56+
}
57+
58+
}
59+
}

0 commit comments

Comments
 (0)