File tree Expand file tree Collapse file tree 3 files changed +87
-33
lines changed
src/main/java/org/alxkm/antipatterns/forgottensynchronization Expand file tree Collapse file tree 3 files changed +87
-33
lines changed Original file line number Diff line number Diff line change 11package org .alxkm .antipatterns .forgottensynchronization ;
22
3-
43/***
54 *
65 * Forgotten synchronization is a common concurrency issue in Java where a method or block of code that should be synchronized is not,
1211 *
1312 * */
1413public class CounterExample {
15- private int count = 0 ;
14+ private int counter = 0 ;
1615
1716 /**
1817 * Increments the counter by one.
1918 * This method is not synchronized, leading to potential race conditions.
2019 */
2120 public void increment () {
22- count ++;
21+ counter ++;
2322 }
2423
2524 /**
@@ -28,7 +27,35 @@ public void increment() {
2827 *
2928 * @return the current count value.
3029 */
31- public int getCount () {
32- return count ;
30+ public int getCounter () {
31+ return counter ;
32+ }
33+
34+
35+ public static void main (String [] args ) throws InterruptedException {
36+ CounterExample counter = new CounterExample ();
37+
38+ // Create two threads that both call increment 1000 times
39+ Thread t1 = new Thread (() -> {
40+ for (int i = 0 ; i < 1000 ; i ++) {
41+ counter .increment ();
42+ }
43+ });
44+
45+ Thread t2 = new Thread (() -> {
46+ for (int i = 0 ; i < 1000 ; i ++) {
47+ counter .increment ();
48+ }
49+ });
50+
51+ t1 .start ();
52+ t2 .start ();
53+
54+ // Wait for threads to finish
55+ t1 .join ();
56+ t2 .join ();
57+
58+ // This will likely print a number less than 2000 due to race conditions
59+ System .out .println ("Final Counter Value (without synchronization): " + counter .getCounter ());
3360 }
3461}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ package org .alxkm .antipatterns .forgottensynchronization ;
2+
3+ /***
4+ *
5+ * To resolve this issue, we need to synchronize the methods to ensure that only one thread can access these critical sections at a time.
6+ *
7+ * */
8+ public class CounterSynchronized {
9+ private int counter = 0 ;
10+
11+ /**
12+ * Increments the counter by one.
13+ * This method is synchronized to prevent race conditions.
14+ */
15+ public synchronized void increment () {
16+ counter ++;
17+ }
18+
19+ /**
20+ * Returns the current value of the counter.
21+ * This method is synchronized to ensure consistent read operations.
22+ *
23+ * @return the current count value.
24+ */
25+ public synchronized int getCounter () {
26+ return counter ;
27+ }
28+
29+ public static void main (String [] args ) throws InterruptedException {
30+ CounterSynchronized counter = new CounterSynchronized ();
31+
32+ // Create two threads that both call increment 1000 times
33+ Thread t1 = new Thread (() -> {
34+ for (int i = 0 ; i < 1000 ; i ++) {
35+ counter .increment ();
36+ }
37+ });
38+
39+ Thread t2 = new Thread (() -> {
40+ for (int i = 0 ; i < 1000 ; i ++) {
41+ counter .increment ();
42+ }
43+ });
44+
45+ t1 .start ();
46+ t2 .start ();
47+
48+ // Wait for threads to finish
49+ t1 .join ();
50+ t2 .join ();
51+
52+ // This will always print 2000 because of proper synchronization
53+ System .out .println ("Final Counter Value (with synchronization): " + counter .getCounter ());
54+ }
55+ }
You can’t perform that action at this time.
0 commit comments