Visitar URL original
test locks (semaphore, timebased semaphore, striped lock) · zacscoding/java_example@0e1879d · GitHub
Skip to content

Commit 0e1879d

Browse files
committed
test locks (semaphore, timebased semaphore, striped lock)
1 parent 902a833 commit 0e1879d

6 files changed

Lines changed: 209 additions & 0 deletions

File tree

‎javademo/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,10 @@
171171
<version>2.23.0</version>
172172
<scope>test</scope>
173173
</dependency>
174+
<dependency>
175+
<groupId>org.apache.commons</groupId>
176+
<artifactId>commons-lang3</artifactId>
177+
<version>3.8.1</version>
178+
</dependency>
174179
</dependencies>
175180
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package thread.lock;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import org.apache.commons.lang3.concurrent.TimedSemaphore;
5+
6+
/**
7+
* https://www.baeldung.com/java-semaphore
8+
*/
9+
public class DelayQueueUsingTimedSemaphore {
10+
11+
private TimedSemaphore semaphore;
12+
13+
public DelayQueueUsingTimedSemaphore(long period, int slotLimit) {
14+
this.semaphore = new TimedSemaphore(period, TimeUnit.SECONDS, slotLimit);
15+
}
16+
17+
public boolean tryAdd() {
18+
return semaphore.tryAcquire();
19+
}
20+
21+
public int availableSlots() {
22+
return semaphore.getAvailablePermits();
23+
}
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package thread.lock;
2+
3+
import java.util.concurrent.Semaphore;
4+
import org.springframework.util.Assert;
5+
6+
/**
7+
* https://www.baeldung.com/java-semaphore
8+
*/
9+
public class LoginQueueUsingSemaphore {
10+
11+
private Semaphore semaphore;
12+
13+
public LoginQueueUsingSemaphore(int slotLimit) {
14+
Assert.isTrue(slotLimit > 0, "slotLimit must be larger than 0");
15+
this.semaphore = new Semaphore(slotLimit);
16+
}
17+
18+
public boolean tryLogin() {
19+
return semaphore.tryAcquire();
20+
}
21+
22+
public void logout() {
23+
semaphore.release();
24+
}
25+
26+
public int availableSlots() {
27+
return semaphore.availablePermits();
28+
}
29+
30+
public boolean hasWaittingUsers() {
31+
return semaphore.hasQueuedThreads();
32+
}
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package thread.lock;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.concurrent.CountDownLatch;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.TimeUnit;
10+
import java.util.stream.IntStream;
11+
import org.checkerframework.checker.units.qual.Acceleration;
12+
import org.checkerframework.checker.units.qual.C;
13+
import org.junit.Test;
14+
15+
/**
16+
* @GitHub : https://github.com/zacscoding
17+
*/
18+
public class DelayQueueUsingTimedSemaphoreTest {
19+
20+
@Test
21+
public void testTimedSemaphore() throws Exception {
22+
final int slots = 10;
23+
final CountDownLatch countDownLatch = new CountDownLatch(slots);
24+
ExecutorService executorService = Executors.newFixedThreadPool(slots);
25+
DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(3, slots);
26+
27+
IntStream.range(0, slots).forEach(i -> {
28+
executorService.execute(() -> {
29+
assertTrue(delayQueue.tryAdd());
30+
countDownLatch.countDown();
31+
});
32+
});
33+
countDownLatch.await();
34+
executorService.shutdown();
35+
36+
System.out.println(delayQueue.availableSlots());
37+
assertTrue(delayQueue.availableSlots() == 0);
38+
assertFalse(delayQueue.tryAdd());
39+
40+
TimeUnit.SECONDS.sleep(4);
41+
assertTrue(delayQueue.availableSlots() > 0);
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package thread.lock;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.stream.IntStream;
9+
import org.junit.Test;
10+
11+
/**
12+
* @GitHub : https://github.com/zacscoding
13+
*/
14+
public class LoginQueueUsingSemaphoreTest {
15+
16+
@Test
17+
public void testSemaphore() {
18+
final int slotLimit = 5;
19+
20+
LoginQueueUsingSemaphore semaphore = new LoginQueueUsingSemaphore(slotLimit);
21+
ExecutorService executorService = Executors.newFixedThreadPool(10);
22+
23+
IntStream.range(0, 5).forEach(i -> {
24+
executorService.execute(() -> {
25+
if (i < slotLimit) {
26+
assertTrue(semaphore.tryLogin());
27+
} else {
28+
assertTrue(semaphore.availableSlots() == 0);
29+
assertFalse(semaphore.tryLogin());
30+
assertTrue(semaphore.hasWaittingUsers());
31+
}
32+
});
33+
});
34+
35+
executorService.shutdown();
36+
}
37+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package thread.lock;
2+
3+
import com.google.common.util.concurrent.Striped;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.concurrent.TimeUnit;
7+
import java.util.concurrent.locks.Lock;
8+
import org.junit.Test;
9+
10+
/**
11+
* Not work.. will try again
12+
*
13+
* @GitHub : https://github.com/zacscoding
14+
*/
15+
public class StripedLockTest {
16+
17+
@Test
18+
public void keyBasedLock() throws Exception {
19+
Striped<Lock> locks = Striped.lock(4);
20+
21+
WriteTask task1 = new WriteTask("key1", "thread01", locks);
22+
WriteTask task2 = new WriteTask("key2", "thread02", locks);
23+
WriteTask task3 = new WriteTask("key3", "thread03", locks);
24+
WriteTask task4 = new WriteTask("key1", "thread04", locks);
25+
26+
List<WriteTask> tasks = Arrays.asList(task1, task2, task3, task4);
27+
28+
for (WriteTask task : tasks) {
29+
task.start();
30+
}
31+
32+
for (WriteTask task : tasks) {
33+
task.join();
34+
}
35+
}
36+
37+
public static class WriteTask extends Thread {
38+
39+
final String taskKey;
40+
final Striped<Lock> locks;
41+
final String threadName;
42+
final int repeat;
43+
44+
public WriteTask(String taskKey, String name, Striped<Lock> locks) {
45+
this.taskKey = taskKey;
46+
this.locks = locks;
47+
this.threadName = name;
48+
//this.repeat = new Random().nextInt(3) + 1;
49+
this.repeat = 3;
50+
}
51+
52+
@Override
53+
public void run() {
54+
Lock lock = locks.get(taskKey);
55+
try {
56+
for (int i = 0; i < repeat; i++) {
57+
System.out.printf("Work %s ... %d\n", threadName, i);
58+
TimeUnit.SECONDS.sleep(1L);
59+
}
60+
} catch (Exception e) {
61+
e.printStackTrace(System.err);
62+
} finally {
63+
lock.unlock();
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)