[JAVA-13960] Clean up (#12994)
* [JAVA-13960] Clean up * [JAVA-13960] Rename classes Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
parent
fc0c16a3ae
commit
ca436ff5e0
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.concurrent.synchronize;
|
package com.baeldung.concurrent.synchronize;
|
||||||
|
|
||||||
public class BaeldungSynchronizedBlocks {
|
public class SynchronizedBlocks {
|
||||||
|
|
||||||
private int count = 0;
|
private int count = 0;
|
||||||
private static int staticCount = 0;
|
private static int staticCount = 0;
|
||||||
|
@ -12,7 +12,7 @@ public class BaeldungSynchronizedBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void performStaticSyncTask() {
|
static void performStaticSyncTask() {
|
||||||
synchronized (BaeldungSynchronizedBlocks.class) {
|
synchronized (SynchronizedBlocks.class) {
|
||||||
setStaticCount(getStaticCount() + 1);
|
setStaticCount(getStaticCount() + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,6 @@ public class BaeldungSynchronizedBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setStaticCount(int staticCount) {
|
private static void setStaticCount(int staticCount) {
|
||||||
BaeldungSynchronizedBlocks.staticCount = staticCount;
|
SynchronizedBlocks.staticCount = staticCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.concurrent.synchronize;
|
package com.baeldung.concurrent.synchronize;
|
||||||
|
|
||||||
public class BaeldungSynchronizedMethods {
|
public class SynchronizedMethods {
|
||||||
|
|
||||||
private int sum = 0;
|
private int sum = 0;
|
||||||
private int syncSum = 0;
|
private int syncSum = 0;
|
|
@ -9,15 +9,15 @@ import java.util.stream.IntStream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class BaeldungSychronizedBlockUnitTest {
|
public class SynchronizedBlocksUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultiThread_whenBlockSync() throws InterruptedException {
|
public void givenMultiThread_whenBlockSync() throws InterruptedException {
|
||||||
ExecutorService service = Executors.newFixedThreadPool(3);
|
ExecutorService service = Executors.newFixedThreadPool(3);
|
||||||
BaeldungSynchronizedBlocks synchronizedBlocks = new BaeldungSynchronizedBlocks();
|
SynchronizedBlocks synchronizedBlocks = new SynchronizedBlocks();
|
||||||
|
|
||||||
IntStream.range(0, 1000)
|
IntStream.range(0, 1000)
|
||||||
.forEach(count -> service.submit(synchronizedBlocks::performSynchronisedTask));
|
.forEach(count -> service.submit(synchronizedBlocks::performSynchronisedTask));
|
||||||
service.awaitTermination(500, TimeUnit.MILLISECONDS);
|
service.awaitTermination(500, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
assertEquals(1000, synchronizedBlocks.getCount());
|
assertEquals(1000, synchronizedBlocks.getCount());
|
||||||
|
@ -28,10 +28,10 @@ public class BaeldungSychronizedBlockUnitTest {
|
||||||
ExecutorService service = Executors.newCachedThreadPool();
|
ExecutorService service = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
IntStream.range(0, 1000)
|
IntStream.range(0, 1000)
|
||||||
.forEach(count -> service.submit(BaeldungSynchronizedBlocks::performStaticSyncTask));
|
.forEach(count -> service.submit(SynchronizedBlocks::performStaticSyncTask));
|
||||||
service.awaitTermination(500, TimeUnit.MILLISECONDS);
|
service.awaitTermination(500, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
assertEquals(1000, BaeldungSynchronizedBlocks.getStaticCount());
|
assertEquals(1000, SynchronizedBlocks.getStaticCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
|
@ -10,16 +10,16 @@ import java.util.stream.IntStream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class BaeldungSynchronizeMethodsUnitTest {
|
public class SynchronizedMethodsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void givenMultiThread_whenNonSyncMethod() throws InterruptedException {
|
public void givenMultiThread_whenNonSyncMethod() throws InterruptedException {
|
||||||
ExecutorService service = Executors.newFixedThreadPool(3);
|
ExecutorService service = Executors.newFixedThreadPool(3);
|
||||||
BaeldungSynchronizedMethods method = new BaeldungSynchronizedMethods();
|
SynchronizedMethods method = new SynchronizedMethods();
|
||||||
|
|
||||||
IntStream.range(0, 1000)
|
IntStream.range(0, 1000)
|
||||||
.forEach(count -> service.submit(method::calculate));
|
.forEach(count -> service.submit(method::calculate));
|
||||||
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
assertEquals(1000, method.getSum());
|
assertEquals(1000, method.getSum());
|
||||||
|
@ -28,10 +28,10 @@ public class BaeldungSynchronizeMethodsUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenMultiThread_whenMethodSync() throws InterruptedException {
|
public void givenMultiThread_whenMethodSync() throws InterruptedException {
|
||||||
ExecutorService service = Executors.newFixedThreadPool(3);
|
ExecutorService service = Executors.newFixedThreadPool(3);
|
||||||
BaeldungSynchronizedMethods method = new BaeldungSynchronizedMethods();
|
SynchronizedMethods method = new SynchronizedMethods();
|
||||||
|
|
||||||
IntStream.range(0, 1000)
|
IntStream.range(0, 1000)
|
||||||
.forEach(count -> service.submit(method::synchronisedCalculate));
|
.forEach(count -> service.submit(method::synchronisedCalculate));
|
||||||
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
assertEquals(1000, method.getSyncSum());
|
assertEquals(1000, method.getSyncSum());
|
||||||
|
@ -42,10 +42,10 @@ public class BaeldungSynchronizeMethodsUnitTest {
|
||||||
ExecutorService service = Executors.newCachedThreadPool();
|
ExecutorService service = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
IntStream.range(0, 1000)
|
IntStream.range(0, 1000)
|
||||||
.forEach(count -> service.submit(BaeldungSynchronizedMethods::syncStaticCalculate));
|
.forEach(count -> service.submit(SynchronizedMethods::syncStaticCalculate));
|
||||||
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
service.awaitTermination(100, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
assertEquals(1000, BaeldungSynchronizedMethods.staticSum);
|
assertEquals(1000, SynchronizedMethods.staticSum);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue