Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eda030f

Browse files
author
someone-1
committed
refactored tests in countdown latch
1 parent 94ce568 commit eda030f

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

‎src/main/java/barrier/FakeCountDownLatch.java‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
package barrier;
22

3-
import java.util.concurrent.locks.Lock;
4-
import java.util.concurrent.locks.ReadWriteLock;
5-
import java.util.concurrent.locks.ReentrantReadWriteLock;
6-
7-
public class CountDownLatch {
8-
ReadWriteLock lock = new ReentrantReadWriteLock();
9-
private Lock rLock = lock.readLock();
10-
private Lock wLock = lock.writeLock();
3+
public class FakeCountDownLatch {
114
private int count;
125

13-
14-
public CountDownLatch(int count){
6+
public FakeCountDownLatch(int count){
157
this.count = count;
168
}
179

1810

1911
public synchronized void countDown(){
20-
System.out.println(count + " -count in " + Thread.currentThread().getName());
21-
count--;
12+
count--;
13+
if (count == 0) {
2214
this.notifyAll();
15+
}
2316
}
2417

2518

@@ -28,7 +21,7 @@ public int getCount(){
2821
}
2922

3023

31-
public void await() throws InterruptedException {
24+
public synchronizedvoid await() throws InterruptedException {
3225
while (count > 0) {
3326
wait();
3427
}

‎src/test/java/barrier/TestFakeCountDownLatch.java‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,55 @@
33
import org.junit.Test;
44
import org.junit.jupiter.api.Assertions;
55

6+
import java.util.Random;
7+
import java.util.concurrent.CountDownLatch;
68
import java.util.concurrent.ExecutorService;
79
import java.util.concurrent.Executors;
810

9-
public class TestCountDownLatch {
11+
public class TestFakeCountDownLatch {
1012

1113
@Test
12-
public void testCountDown() throwsInterruptedException{
13-
int threadCount = 10, diff = 4;
14-
CountDownLatch latchSingleThread = new CountDownLatch(threadCount);
14+
public void singleThreadTest(){
15+
int threadCount = 10;
16+
FakeCountDownLatch latchSingleThread = new FakeCountDownLatch(threadCount);
1517
Assertions.assertEquals(threadCount, latchSingleThread.getCount());
1618
latchSingleThread.countDown();
1719
latchSingleThread.countDown();
1820
Assertions.assertEquals(threadCount-2, latchSingleThread.getCount());
21+
}
1922

23+
@Test
24+
public void testCountDown() throws InterruptedException {
25+
int threadCount = 10, diff = 4;
26+
27+
FakeCountDownLatch latch = new FakeCountDownLatch(threadCount);
28+
CountDownLatch realLatch = new CountDownLatch(threadCount);
2029

21-
CountDownLatch latch = new CountDownLatch(threadCount);
22-
java.util.concurrent.CountDownLatch realLatch = new
23-
java.util.concurrent.CountDownLatch(threadCount - diff);
2430
ExecutorService service = Executors.newFixedThreadPool(threadCount);
2531
for (int i = 0; i < threadCount; i++) {
2632
service.submit(() -> {
2733
try {
28-
Thread.sleep(1000);
34+
int rand = new Random().nextInt();
35+
if(rand < 0){
36+
rand = rand*-1;
37+
}
38+
rand = rand%1000;
39+
Thread.sleep(rand);
2940
} catch (InterruptedException e) {
3041
e.printStackTrace();
3142
}
3243
latch.countDown();
3344
realLatch.countDown();
34-
System.out.println(latch.getCount() + " - count " + threadCount);
3545
});
3646
}
3747
realLatch.await();
38-
latch.await();
39-
Assertions.assertEquals(diff, latch.getCount());
48+
Assertions.assertEquals(0, latch.getCount());
4049
}
4150

4251

4352
@Test
4453
public void testAwait() throws InterruptedException {
45-
CountDownLatch latch = new CountDownLatch(3);
54+
FakeCountDownLatch latch = new FakeCountDownLatch(3);
4655
Thread t1 = new Thread(() -> {
4756
try {
4857
Thread.sleep(1000);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /