Skip to content

Commit 9d68152

Browse files
Merge pull request eugenp#1854 from dhruba619/master
BAEL-839 added test cases and refactored code
2 parents ee770f3 + fef1395 commit 9d68152

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.concurrent.synchronize;
2+
3+
public class BaeldungSynchronizedBlocks {
4+
5+
private int count = 0;
6+
private static int staticCount = 0;
7+
8+
public void performSynchronisedTask() {
9+
synchronized (this) {
10+
setCount(getCount() + 1);
11+
}
12+
}
13+
14+
public static void performStaticSyncTask() {
15+
synchronized (BaeldungSynchronizedBlocks.class) {
16+
setStaticCount(getStaticCount() + 1);
17+
}
18+
}
19+
20+
public int getCount() {
21+
return count;
22+
}
23+
24+
public void setCount(int count) {
25+
this.count = count;
26+
}
27+
28+
public static int getStaticCount() {
29+
return staticCount;
30+
}
31+
32+
public static void setStaticCount(int staticCount) {
33+
BaeldungSynchronizedBlocks.staticCount = staticCount;
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.concurrent.synchronize;
2+
3+
public class BaeldungSynchronizedMethods {
4+
5+
private int sum = 0;
6+
private int syncSum = 0;
7+
8+
public static int staticSum = 0;
9+
10+
public void calculate() {
11+
setSum(getSum() + 1);
12+
}
13+
14+
public synchronized void synchronisedCalculate() {
15+
setSyncSum(getSyncSum() + 1);
16+
}
17+
18+
public static synchronized void syncStaticCalculate() {
19+
staticSum = staticSum + 1;
20+
}
21+
22+
public int getSum() {
23+
return sum;
24+
}
25+
26+
public void setSum(int sum) {
27+
this.sum = sum;
28+
}
29+
30+
public int getSyncSum() {
31+
return syncSum;
32+
}
33+
34+
public void setSyncSum(int syncSum) {
35+
this.syncSum = syncSum;
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.concurrent.synchronize;
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 BaeldungSychronizedBlockTest {
13+
14+
@Test
15+
public void givenMultiThread_whenBlockSync() throws InterruptedException {
16+
ExecutorService service = Executors.newFixedThreadPool(3);
17+
BaeldungSynchronizedBlocks synchronizedBlocks = new BaeldungSynchronizedBlocks();
18+
19+
IntStream.range(0, 1000)
20+
.forEach(count -> service.submit(synchronizedBlocks::performSynchronisedTask));
21+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
22+
23+
assertEquals(1000, synchronizedBlocks.getCount());
24+
}
25+
26+
@Test
27+
public void givenMultiThread_whenStaticSyncBlock() throws InterruptedException {
28+
ExecutorService service = Executors.newCachedThreadPool();
29+
30+
IntStream.range(0, 1000)
31+
.forEach(count -> service.submit(BaeldungSynchronizedBlocks::performStaticSyncTask));
32+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
33+
34+
assertEquals(1000, BaeldungSynchronizedBlocks.getStaticCount());
35+
}
36+
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.concurrent.synchronize;
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.Ignore;
11+
import org.junit.Test;
12+
13+
public class BaeldungSynchronizeMethodsTest {
14+
15+
@Test
16+
@Ignore
17+
public void givenMultiThread_whenNonSyncMethod() throws InterruptedException {
18+
ExecutorService service = Executors.newFixedThreadPool(3);
19+
BaeldungSynchronizedMethods method = new BaeldungSynchronizedMethods();
20+
21+
IntStream.range(0, 1000)
22+
.forEach(count -> service.submit(method::calculate));
23+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
24+
25+
assertEquals(1000, method.getSum());
26+
}
27+
28+
@Test
29+
public void givenMultiThread_whenMethodSync() throws InterruptedException {
30+
ExecutorService service = Executors.newFixedThreadPool(3);
31+
BaeldungSynchronizedMethods method = new BaeldungSynchronizedMethods();
32+
33+
IntStream.range(0, 1000)
34+
.forEach(count -> service.submit(method::synchronisedCalculate));
35+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
36+
37+
assertEquals(1000, method.getSyncSum());
38+
}
39+
40+
@Test
41+
public void givenMultiThread_whenStaticSyncMethod() throws InterruptedException {
42+
ExecutorService service = Executors.newCachedThreadPool();
43+
44+
IntStream.range(0, 1000)
45+
.forEach(count -> service.submit(BaeldungSynchronizedMethods::syncStaticCalculate));
46+
service.awaitTermination(100, TimeUnit.MILLISECONDS);
47+
48+
assertEquals(1000, BaeldungSynchronizedMethods.staticSum);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)