Skip to content

Commit 45ba2a9

Browse files
committed
[BAEL-10837] - Splitted core-java-concurrency module
1 parent 8fbed2e commit 45ba2a9

File tree

127 files changed

+256
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+256
-136
lines changed

core-java-concurrency/README.md renamed to core-java-concurrency-advanced/README.md

+1-13

core-java-concurrency/pom.xml renamed to core-java-concurrency-advanced/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.baeldung</groupId>
5-
<artifactId>core-java-concurrency</artifactId>
5+
<artifactId>core-java-concurrency-advanced</artifactId>
66
<version>0.1.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
8-
<name>core-java-concurrency</name>
8+
<name>core-java-concurrency-advanced</name>
99

1010
<parent>
1111
<groupId>com.baeldung</groupId>
@@ -60,7 +60,7 @@
6060
</dependencies>
6161

6262
<build>
63-
<finalName>core-java-concurrency</finalName>
63+
<finalName>core-java-concurrency-advanced</finalName>
6464
<resources>
6565
<resource>
6666
<directory>src/main/resources</directory>
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package com.baeldung.concurrent.atomic;
2-
3-
public class SafeCounterWithLock {
4-
private volatile int counter;
5-
6-
int getValue() {
7-
return counter;
8-
}
9-
10-
synchronized void increment() {
11-
counter++;
12-
}
13-
}
1+
package com.baeldung.concurrent.atomic;
2+
3+
public class SafeCounterWithLock {
4+
private volatile int counter;
5+
6+
int getValue() {
7+
return counter;
8+
}
9+
10+
synchronized void increment() {
11+
counter++;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
package com.baeldung.concurrent.atomic;
2-
3-
import java.util.concurrent.atomic.AtomicInteger;
4-
5-
public class SafeCounterWithoutLock {
6-
private final AtomicInteger counter = new AtomicInteger(0);
7-
8-
int getValue() {
9-
return counter.get();
10-
}
11-
12-
void increment() {
13-
while(true) {
14-
int existingValue = getValue();
15-
int newValue = existingValue + 1;
16-
if(counter.compareAndSet(existingValue, newValue)) {
17-
return;
18-
}
19-
}
20-
}
21-
}
1+
package com.baeldung.concurrent.atomic;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
5+
public class SafeCounterWithoutLock {
6+
private final AtomicInteger counter = new AtomicInteger(0);
7+
8+
int getValue() {
9+
return counter.get();
10+
}
11+
12+
void increment() {
13+
while(true) {
14+
int existingValue = getValue();
15+
int newValue = existingValue + 1;
16+
if(counter.compareAndSet(existingValue, newValue)) {
17+
return;
18+
}
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package com.baeldung.concurrent.atomic;
2-
3-
public class UnsafeCounter {
4-
private int counter;
5-
6-
int getValue() {
7-
return counter;
8-
}
9-
10-
void increment() {
11-
counter++;
12-
}
13-
}
1+
package com.baeldung.concurrent.atomic;
2+
3+
public class UnsafeCounter {
4+
private int counter;
5+
6+
int getValue() {
7+
return counter;
8+
}
9+
10+
void increment() {
11+
counter++;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
package com.baeldung.concurrent.atomic;
2-
3-
import static org.junit.Assert.assertEquals;
4-
5-
import java.util.concurrent.ExecutorService;
6-
import java.util.concurrent.Executors;
7-
import java.util.concurrent.TimeUnit;
8-
import java.util.stream.IntStream;
9-
10-
import org.junit.Test;
11-
12-
public class ThreadSafeCounterIntegrationTest {
13-
14-
@Test
15-
public void givenMultiThread_whenSafeCounterWithLockIncrement() throws InterruptedException {
16-
ExecutorService service = Executors.newFixedThreadPool(3);
17-
SafeCounterWithLock safeCounter = new SafeCounterWithLock();
18-
19-
IntStream.range(0, 1000)
20-
.forEach(count -> service.submit(safeCounter::increment));
21-
service.awaitTermination(100, TimeUnit.MILLISECONDS);
22-
23-
assertEquals(1000, safeCounter.getValue());
24-
}
25-
26-
@Test
27-
public void givenMultiThread_whenSafeCounterWithoutLockIncrement() throws InterruptedException {
28-
ExecutorService service = Executors.newFixedThreadPool(3);
29-
SafeCounterWithoutLock safeCounter = new SafeCounterWithoutLock();
30-
31-
IntStream.range(0, 1000)
32-
.forEach(count -> service.submit(safeCounter::increment));
33-
service.awaitTermination(100, TimeUnit.MILLISECONDS);
34-
35-
assertEquals(1000, safeCounter.getValue());
36-
}
37-
38-
}
1+
package com.baeldung.concurrent.atomic;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.stream.IntStream;
9+
10+
import org.junit.Test;
11+
12+
public class ThreadSafeCounterIntegrationTest {
13+
14+
@Test
15+
public void givenMultiThread_whenSafeCounterWithLockIncrement() throws InterruptedException {
16+
ExecutorService service = Executors.newFixedThreadPool(3);
17+
SafeCounterWithLock safeCounter = new SafeCounterWithLock();
18+
19+
IntStream.range(0, 1000)
20+
.forEach(count -> service.submit(safeCounter::increment));
21+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
22+
23+
assertEquals(1000, safeCounter.getValue());
24+
}
25+
26+
@Test
27+
public void givenMultiThread_whenSafeCounterWithoutLockIncrement() throws InterruptedException {
28+
ExecutorService service = Executors.newFixedThreadPool(3);
29+
SafeCounterWithoutLock safeCounter = new SafeCounterWithoutLock();
30+
31+
IntStream.range(0, 1000)
32+
.forEach(count -> service.submit(safeCounter::increment));
33+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
34+
35+
assertEquals(1000, safeCounter.getValue());
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
package com.baeldung.concurrent.atomic;
2-
3-
import static org.junit.Assert.assertEquals;
4-
5-
import java.util.concurrent.ExecutorService;
6-
import java.util.concurrent.Executors;
7-
import java.util.concurrent.TimeUnit;
8-
import java.util.stream.IntStream;
9-
10-
import org.junit.Test;
11-
12-
/**
13-
* This test shows the behaviour of a thread-unsafe class in a multithreaded scenario. We are calling
14-
* the increment methods 1000 times from a pool of 3 threads. In most of the cases, the counter will
15-
* less than 1000, because of lost updates, however, occasionally it may reach 1000, when no threads
16-
* called the method simultaneously. This may cause the build to fail occasionally. Hence excluding this
17-
* test from build by adding this in manual test
18-
*/
19-
public class ThreadUnsafeCounterManualTest {
20-
21-
@Test
22-
public void givenMultiThread_whenUnsafeCounterIncrement() throws InterruptedException {
23-
ExecutorService service = Executors.newFixedThreadPool(3);
24-
UnsafeCounter unsafeCounter = new UnsafeCounter();
25-
26-
IntStream.range(0, 1000)
27-
.forEach(count -> service.submit(unsafeCounter::increment));
28-
service.awaitTermination(100, TimeUnit.MILLISECONDS);
29-
30-
assertEquals(1000, unsafeCounter.getValue());
31-
}
32-
33-
}
1+
package com.baeldung.concurrent.atomic;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.stream.IntStream;
9+
10+
import org.junit.Test;
11+
12+
/**
13+
* This test shows the behaviour of a thread-unsafe class in a multithreaded scenario. We are calling
14+
* the increment methods 1000 times from a pool of 3 threads. In most of the cases, the counter will
15+
* less than 1000, because of lost updates, however, occasionally it may reach 1000, when no threads
16+
* called the method simultaneously. This may cause the build to fail occasionally. Hence excluding this
17+
* test from build by adding this in manual test
18+
*/
19+
public class ThreadUnsafeCounterManualTest {
20+
21+
@Test
22+
public void givenMultiThread_whenUnsafeCounterIncrement() throws InterruptedException {
23+
ExecutorService service = Executors.newFixedThreadPool(3);
24+
UnsafeCounter unsafeCounter = new UnsafeCounter();
25+
26+
IntStream.range(0, 1000)
27+
.forEach(count -> service.submit(unsafeCounter::increment));
28+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
29+
30+
assertEquals(1000, unsafeCounter.getValue());
31+
}
32+
33+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
*.class
2+
3+
0.*
4+
5+
#folders#
6+
/target
7+
/neoDb*
8+
/data
9+
/src/main/webapp/WEB-INF/classes
10+
*/META-INF/*
11+
.resourceCache
12+
13+
# Packaged files #
14+
*.jar
15+
*.war
16+
*.ear
17+
18+
# Files generated by integration tests
19+
*.txt
20+
backup-pom.xml
21+
/bin/
22+
/temp
23+
24+
#IntelliJ specific
25+
.idea/
26+
*.iml

core-java-concurrency-basic/README.md

+17

0 commit comments

Comments
 (0)