forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BAEL-3351] - Common Concurrency Pitfalls in Java (eugenp#8104)
- Loading branch information
1 parent
009aac4
commit c552aca
Showing
7 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ency-advanced-3/src/main/java/com/baeldung/commonissues/CollectionsConcurrencyIssues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.baeldung.commonissues; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class CollectionsConcurrencyIssues { | ||
|
||
private void putIfAbsentList_NonAtomicOperation_ProneToConcurrencyIssues() { | ||
List<String> list = Collections.synchronizedList(new ArrayList<>()); | ||
if (!list.contains("foo")) { | ||
list.add("foo"); | ||
} | ||
} | ||
|
||
private void putIfAbsentList_AtomicOperation_ThreadSafe() { | ||
List<String> list = Collections.synchronizedList(new ArrayList<>()); | ||
synchronized (list) { | ||
if (!list.contains("foo")) { | ||
list.add("foo"); | ||
} | ||
} | ||
} | ||
|
||
private void putIfAbsentMap_NonAtomicOperation_ProneToConcurrencyIssues() { | ||
Map<String, String> map = new ConcurrentHashMap<>(); | ||
if (!map.containsKey("foo")) { | ||
map.put("foo", "bar"); | ||
} | ||
} | ||
|
||
private void putIfAbsentMap_AtomicOperation_BetterApproach() { | ||
Map<String, String> map = new ConcurrentHashMap<>(); | ||
synchronized (map) { | ||
if (!map.containsKey("foo")) { | ||
map.put("foo", "bar"); | ||
} | ||
} | ||
} | ||
|
||
private void putIfAbsentMap_AtomicOperation_BestApproach() { | ||
Map<String, String> map = new ConcurrentHashMap<>(); | ||
map.putIfAbsent("foo", "bar"); | ||
} | ||
|
||
private void computeIfAbsentMap_AtomicOperation() { | ||
Map<String, String> map = new ConcurrentHashMap<>(); | ||
map.computeIfAbsent("foo", key -> key + "bar"); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...les/core-java-concurrency-advanced-3/src/main/java/com/baeldung/commonissues/Counter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.baeldung.commonissues; | ||
|
||
class Counter { | ||
private int counter = 0; | ||
|
||
public void increment() { | ||
counter++; | ||
} | ||
|
||
public int getValue() { | ||
return counter; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...-java-concurrency-advanced-3/src/main/java/com/baeldung/commonissues/DeadlockExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.baeldung.commonissues; | ||
|
||
public class DeadlockExample { | ||
|
||
public static Object lock1 = new Object(); | ||
public static Object lock2 = new Object(); | ||
|
||
public static void main(String args[]) { | ||
Thread threadA = new Thread(() -> { | ||
synchronized (lock1) { | ||
System.out.println("ThreadA: Holding lock 1..."); | ||
sleep(); | ||
System.out.println("ThreadA: Waiting for lock 2..."); | ||
|
||
synchronized (lock2) { | ||
System.out.println("ThreadA: Holding lock 1 & 2..."); | ||
} | ||
} | ||
}); | ||
Thread threadB = new Thread(() -> { | ||
synchronized (lock2) { | ||
System.out.println("ThreadB: Holding lock 2..."); | ||
sleep(); | ||
System.out.println("ThreadB: Waiting for lock 1..."); | ||
|
||
synchronized (lock1) { | ||
System.out.println("ThreadB: Holding lock 1 & 2..."); | ||
} | ||
} | ||
}); | ||
threadA.start(); | ||
threadB.start(); | ||
} | ||
|
||
private static void sleep() { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...nced-3/src/main/java/com/baeldung/commonissues/SimpleDateFormatThreadUnsafetyExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.baeldung.commonissues; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class SimpleDateFormatThreadUnsafetyExample { | ||
|
||
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | ||
|
||
public static void main(String[] args) { | ||
String dateStr = "2019-10-29T11:12:21"; | ||
|
||
ExecutorService executorService = Executors.newFixedThreadPool(10); | ||
|
||
for (int i = 0; i < 20; i++) { | ||
executorService.submit(() -> parseDate(dateStr)); | ||
} | ||
|
||
executorService.shutdown(); | ||
} | ||
|
||
private static void parseDate(String dateStr) { | ||
try { | ||
Date date = simpleDateFormat.parse(dateStr); | ||
System.out.println("Successfully Parsed Date " + date); | ||
} catch (ParseException e) { | ||
System.out.println("ParseError " + e.getMessage()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...a-concurrency-advanced-3/src/main/java/com/baeldung/commonissues/SynchronizedCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.baeldung.commonissues; | ||
|
||
class SynchronizedCounter { | ||
private int counter = 0; | ||
|
||
public synchronized void increment() { | ||
counter++; | ||
} | ||
|
||
public synchronized int getValue() { | ||
return counter; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...rency-advanced-3/src/main/java/com/baeldung/commonissues/SynchronizedVolatileCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.baeldung.commonissues; | ||
|
||
class SynchronizedVolatileCounter { | ||
private volatile int counter = 0; | ||
|
||
public synchronized void increment() { | ||
counter++; | ||
} | ||
|
||
public int getValue() { | ||
return counter; | ||
} | ||
} |