File tree Expand file tree Collapse file tree
src/com/sri/synchronisedTest Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments