This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
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-10837] - Splitted core-java-concurrency module
- Loading branch information
Showing
127 changed files
with
256 additions
and
136 deletions.
There are no files selected for viewing
File renamed without changes.
14 changes: 1 addition & 13 deletions
14
core-java-concurrency/README.md → core-java-concurrency-advanced/README.md
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
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
26 changes: 13 additions & 13 deletions
26
...oncurrent/atomic/SafeCounterWithLock.java → ...oncurrent/atomic/SafeCounterWithLock.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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package com.baeldung.concurrent.atomic; | ||
|
||
public class SafeCounterWithLock { | ||
private volatile int counter; | ||
|
||
int getValue() { | ||
return counter; | ||
} | ||
|
||
synchronized void increment() { | ||
counter++; | ||
} | ||
} | ||
package com.baeldung.concurrent.atomic; | ||
|
||
public class SafeCounterWithLock { | ||
private volatile int counter; | ||
|
||
int getValue() { | ||
return counter; | ||
} | ||
|
||
synchronized void increment() { | ||
counter++; | ||
} | ||
} |
42 changes: 21 additions & 21 deletions
42
...urrent/atomic/SafeCounterWithoutLock.java → ...urrent/atomic/SafeCounterWithoutLock.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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
package com.baeldung.concurrent.atomic; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class SafeCounterWithoutLock { | ||
private final AtomicInteger counter = new AtomicInteger(0); | ||
|
||
int getValue() { | ||
return counter.get(); | ||
} | ||
|
||
void increment() { | ||
while(true) { | ||
int existingValue = getValue(); | ||
int newValue = existingValue + 1; | ||
if(counter.compareAndSet(existingValue, newValue)) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
package com.baeldung.concurrent.atomic; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class SafeCounterWithoutLock { | ||
private final AtomicInteger counter = new AtomicInteger(0); | ||
|
||
int getValue() { | ||
return counter.get(); | ||
} | ||
|
||
void increment() { | ||
while(true) { | ||
int existingValue = getValue(); | ||
int newValue = existingValue + 1; | ||
if(counter.compareAndSet(existingValue, newValue)) { | ||
return; | ||
} | ||
} | ||
} | ||
} |
26 changes: 13 additions & 13 deletions
26
...dung/concurrent/atomic/UnsafeCounter.java → ...dung/concurrent/atomic/UnsafeCounter.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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package com.baeldung.concurrent.atomic; | ||
|
||
public class UnsafeCounter { | ||
private int counter; | ||
|
||
int getValue() { | ||
return counter; | ||
} | ||
|
||
void increment() { | ||
counter++; | ||
} | ||
} | ||
package com.baeldung.concurrent.atomic; | ||
|
||
public class UnsafeCounter { | ||
private int counter; | ||
|
||
int getValue() { | ||
return counter; | ||
} | ||
|
||
void increment() { | ||
counter++; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
76 changes: 38 additions & 38 deletions
76
...mic/ThreadSafeCounterIntegrationTest.java → ...mic/ThreadSafeCounterIntegrationTest.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 |
---|---|---|
@@ -1,38 +1,38 @@ | ||
package com.baeldung.concurrent.atomic; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.Test; | ||
|
||
public class ThreadSafeCounterIntegrationTest { | ||
|
||
@Test | ||
public void givenMultiThread_whenSafeCounterWithLockIncrement() throws InterruptedException { | ||
ExecutorService service = Executors.newFixedThreadPool(3); | ||
SafeCounterWithLock safeCounter = new SafeCounterWithLock(); | ||
|
||
IntStream.range(0, 1000) | ||
.forEach(count -> service.submit(safeCounter::increment)); | ||
service.awaitTermination(100, TimeUnit.MILLISECONDS); | ||
|
||
assertEquals(1000, safeCounter.getValue()); | ||
} | ||
|
||
@Test | ||
public void givenMultiThread_whenSafeCounterWithoutLockIncrement() throws InterruptedException { | ||
ExecutorService service = Executors.newFixedThreadPool(3); | ||
SafeCounterWithoutLock safeCounter = new SafeCounterWithoutLock(); | ||
|
||
IntStream.range(0, 1000) | ||
.forEach(count -> service.submit(safeCounter::increment)); | ||
service.awaitTermination(100, TimeUnit.MILLISECONDS); | ||
|
||
assertEquals(1000, safeCounter.getValue()); | ||
} | ||
|
||
} | ||
package com.baeldung.concurrent.atomic; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.Test; | ||
|
||
public class ThreadSafeCounterIntegrationTest { | ||
|
||
@Test | ||
public void givenMultiThread_whenSafeCounterWithLockIncrement() throws InterruptedException { | ||
ExecutorService service = Executors.newFixedThreadPool(3); | ||
SafeCounterWithLock safeCounter = new SafeCounterWithLock(); | ||
|
||
IntStream.range(0, 1000) | ||
.forEach(count -> service.submit(safeCounter::increment)); | ||
service.awaitTermination(100, TimeUnit.MILLISECONDS); | ||
|
||
assertEquals(1000, safeCounter.getValue()); | ||
} | ||
|
||
@Test | ||
public void givenMultiThread_whenSafeCounterWithoutLockIncrement() throws InterruptedException { | ||
ExecutorService service = Executors.newFixedThreadPool(3); | ||
SafeCounterWithoutLock safeCounter = new SafeCounterWithoutLock(); | ||
|
||
IntStream.range(0, 1000) | ||
.forEach(count -> service.submit(safeCounter::increment)); | ||
service.awaitTermination(100, TimeUnit.MILLISECONDS); | ||
|
||
assertEquals(1000, safeCounter.getValue()); | ||
} | ||
|
||
} |
66 changes: 33 additions & 33 deletions
66
...atomic/ThreadUnsafeCounterManualTest.java → ...atomic/ThreadUnsafeCounterManualTest.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 |
---|---|---|
@@ -1,33 +1,33 @@ | ||
package com.baeldung.concurrent.atomic; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* This test shows the behaviour of a thread-unsafe class in a multithreaded scenario. We are calling | ||
* the increment methods 1000 times from a pool of 3 threads. In most of the cases, the counter will | ||
* less than 1000, because of lost updates, however, occasionally it may reach 1000, when no threads | ||
* called the method simultaneously. This may cause the build to fail occasionally. Hence excluding this | ||
* test from build by adding this in manual test | ||
*/ | ||
public class ThreadUnsafeCounterManualTest { | ||
|
||
@Test | ||
public void givenMultiThread_whenUnsafeCounterIncrement() throws InterruptedException { | ||
ExecutorService service = Executors.newFixedThreadPool(3); | ||
UnsafeCounter unsafeCounter = new UnsafeCounter(); | ||
|
||
IntStream.range(0, 1000) | ||
.forEach(count -> service.submit(unsafeCounter::increment)); | ||
service.awaitTermination(100, TimeUnit.MILLISECONDS); | ||
|
||
assertEquals(1000, unsafeCounter.getValue()); | ||
} | ||
|
||
} | ||
package com.baeldung.concurrent.atomic; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* This test shows the behaviour of a thread-unsafe class in a multithreaded scenario. We are calling | ||
* the increment methods 1000 times from a pool of 3 threads. In most of the cases, the counter will | ||
* less than 1000, because of lost updates, however, occasionally it may reach 1000, when no threads | ||
* called the method simultaneously. This may cause the build to fail occasionally. Hence excluding this | ||
* test from build by adding this in manual test | ||
*/ | ||
public class ThreadUnsafeCounterManualTest { | ||
|
||
@Test | ||
public void givenMultiThread_whenUnsafeCounterIncrement() throws InterruptedException { | ||
ExecutorService service = Executors.newFixedThreadPool(3); | ||
UnsafeCounter unsafeCounter = new UnsafeCounter(); | ||
|
||
IntStream.range(0, 1000) | ||
.forEach(count -> service.submit(unsafeCounter::increment)); | ||
service.awaitTermination(100, TimeUnit.MILLISECONDS); | ||
|
||
assertEquals(1000, unsafeCounter.getValue()); | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,26 @@ | ||
*.class | ||
|
||
0.* | ||
|
||
#folders# | ||
/target | ||
/neoDb* | ||
/data | ||
/src/main/webapp/WEB-INF/classes | ||
*/META-INF/* | ||
.resourceCache | ||
|
||
# Packaged files # | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# Files generated by integration tests | ||
*.txt | ||
backup-pom.xml | ||
/bin/ | ||
/temp | ||
|
||
#IntelliJ specific | ||
.idea/ | ||
*.iml |
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,17 @@ | ||
========= | ||
|
||
## Core Java Concurrency Basic Examples | ||
|
||
### Relevant Articles: | ||
- [Guide To CompletableFuture](http://www.baeldung.com/java-completablefuture) | ||
- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial) | ||
- [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future) | ||
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep) | ||
- [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized) | ||
- [Overview of the java.util.concurrent](http://www.baeldung.com/java-util-concurrent) | ||
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread) | ||
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop) | ||
- [ExecutorService - Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads) | ||
- [wait and notify() Methods in Java](http://www.baeldung.com/java-wait-notify) | ||
- [Life Cycle of a Thread in Java](http://www.baeldung.com/java-thread-lifecycle) | ||
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable) |
Oops, something went wrong.