Skip to content

Commit a390a28

Browse files
author
Rajeev Kumar Singh
committed
Await Termination
1 parent fefb52a commit a390a28

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

java-concurrency-issues-and-synchronization/Readme.md

Whitespace-only changes.

java-concurrency-issues-and-synchronization/src/RaceConditionExample.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.util.concurrent.ExecutorService;
22
import 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

1920
public 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
}

java-concurrency-issues-and-synchronization/src/SynchronizedBlockExample.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.util.concurrent.ExecutorService;
22
import 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

2223
public 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
}

java-concurrency-issues-and-synchronization/src/SynchronizedMethodExample.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.util.concurrent.ExecutorService;
22
import 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

2022
public 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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy