File tree Expand file tree Collapse file tree 4 files changed +17
-4
lines changed
java-concurrency-issues-and-synchronization Expand file tree Collapse file tree 4 files changed +17
-4
lines changed Original file line number Diff line number Diff line change 11import java .util .concurrent .ExecutorService ;
22import java .util .concurrent .Executors ;
3+ import java .util .concurrent .TimeUnit ;
34
45/**
56 * Created by rajeevkumarsingh on 11/05/17.
@@ -18,7 +19,7 @@ public int getCount() {
1819
1920public class RaceConditionExample {
2021
21- public static void main (String [] args ) {
22+ public static void main (String [] args ) throws InterruptedException {
2223 ExecutorService executorService = Executors .newFixedThreadPool (10 );
2324
2425 Counter counter = new Counter ();
@@ -27,6 +28,9 @@ public static void main(String[] args) {
2728 executorService .submit (() -> counter .increment ());
2829 }
2930
31+ executorService .shutdown ();
32+ executorService .awaitTermination (60 , TimeUnit .SECONDS );
33+
3034 System .out .println ("Final count is : " + counter .getCount ());
3135 }
3236}
Original file line number Diff line number Diff line change 11import java .util .concurrent .ExecutorService ;
22import java .util .concurrent .Executors ;
3+ import java .util .concurrent .TimeUnit ;
34
45/**
56 * Created by rajeevkumarsingh on 11/05/17.
@@ -20,14 +21,17 @@ public int getCount() {
2021}
2122
2223public class SynchronizedBlockExample {
23- public static void main (String [] args ) {
24+ public static void main (String [] args ) throws InterruptedException {
2425 ExecutorService executorService = Executors .newFixedThreadPool (10 );
2526 FineGrainedSynchronizedCounter counter = new FineGrainedSynchronizedCounter ();
2627
2728 for (int i = 0 ; i < 1000 ; i ++) {
2829 executorService .submit (() -> counter .increment ());
2930 }
3031
32+ executorService .shutdown ();
33+ executorService .awaitTermination (60 , TimeUnit .SECONDS );
34+
3135 System .out .println ("Final count is " + counter .getCount ());
3236 }
3337}
Original file line number Diff line number Diff line change 11import java .util .concurrent .ExecutorService ;
22import java .util .concurrent .Executors ;
3+ import java .util .concurrent .TimeUnit ;
34
45/**
56 * Created by rajeevkumarsingh on 11/05/17.
@@ -9,6 +10,7 @@ class SynchronizedCounter {
910
1011 // Synchronized Method
1112 public synchronized void increment () {
13+ System .out .println (Thread .currentThread ().getName ());
1214 count = count + 1 ;
1315 }
1416
@@ -18,7 +20,7 @@ public int getCount() {
1820}
1921
2022public class SynchronizedMethodExample {
21- public static void main (String [] args ) {
23+ public static void main (String [] args ) throws InterruptedException {
2224 ExecutorService executorService = Executors .newFixedThreadPool (10 );
2325
2426 SynchronizedCounter synchronizedCounter = new SynchronizedCounter ();
@@ -27,6 +29,9 @@ public static void main(String[] args) {
2729 executorService .submit (() -> synchronizedCounter .increment ());
2830 }
2931
32+ executorService .shutdown ();
33+ executorService .awaitTermination (60 , TimeUnit .SECONDS );
34+
3035 System .out .println ("Final count is : " + synchronizedCounter .getCount ());
3136 }
32- }
37+ }
You can’t perform that action at this time.
0 commit comments