Semaphores refactor
This commit is contained in:
parent
20669dc6a1
commit
aecdf6d5c7
|
@ -2,17 +2,17 @@ package com.baeldung.concurrent.semaphores;
|
||||||
|
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
public class CounterUsingMutex {
|
class CounterUsingMutex {
|
||||||
|
|
||||||
private final Semaphore mutex;
|
private final Semaphore mutex;
|
||||||
private int count;
|
private int count;
|
||||||
|
|
||||||
public CounterUsingMutex() {
|
CounterUsingMutex() {
|
||||||
mutex = new Semaphore(1);
|
mutex = new Semaphore(1);
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void increase() throws InterruptedException {
|
void increase() throws InterruptedException {
|
||||||
mutex.acquire();
|
mutex.acquire();
|
||||||
this.count = this.count + 1;
|
this.count = this.count + 1;
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
@ -20,11 +20,11 @@ public class CounterUsingMutex {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCount() {
|
int getCount() {
|
||||||
return this.count;
|
return this.count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasQueuedThreads() {
|
boolean hasQueuedThreads() {
|
||||||
return mutex.hasQueuedThreads();
|
return mutex.hasQueuedThreads();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,19 +4,19 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.apache.commons.lang3.concurrent.TimedSemaphore;
|
import org.apache.commons.lang3.concurrent.TimedSemaphore;
|
||||||
|
|
||||||
public class DelayQueueUsingTimedSemaphore {
|
class DelayQueueUsingTimedSemaphore {
|
||||||
|
|
||||||
private final TimedSemaphore semaphore;
|
private final TimedSemaphore semaphore;
|
||||||
|
|
||||||
public DelayQueueUsingTimedSemaphore(long period, int slotLimit) {
|
DelayQueueUsingTimedSemaphore(long period, int slotLimit) {
|
||||||
semaphore = new TimedSemaphore(period, TimeUnit.SECONDS, slotLimit);
|
semaphore = new TimedSemaphore(period, TimeUnit.SECONDS, slotLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean tryAdd() {
|
boolean tryAdd() {
|
||||||
return semaphore.tryAcquire();
|
return semaphore.tryAcquire();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int availableSlots() {
|
int availableSlots() {
|
||||||
return semaphore.getAvailablePermits();
|
return semaphore.getAvailablePermits();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,23 +2,23 @@ package com.baeldung.concurrent.semaphores;
|
||||||
|
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
public class LoginQueueUsingSemaphore {
|
class LoginQueueUsingSemaphore {
|
||||||
|
|
||||||
private final Semaphore semaphore;
|
private final Semaphore semaphore;
|
||||||
|
|
||||||
public LoginQueueUsingSemaphore(int slotLimit) {
|
LoginQueueUsingSemaphore(int slotLimit) {
|
||||||
semaphore = new Semaphore(slotLimit);
|
semaphore = new Semaphore(slotLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean tryLogin() {
|
boolean tryLogin() {
|
||||||
return semaphore.tryAcquire();
|
return semaphore.tryAcquire();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void logout() {
|
void logout() {
|
||||||
semaphore.release();
|
semaphore.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int availableSlots() {
|
int availableSlots() {
|
||||||
return semaphore.availablePermits();
|
return semaphore.availablePermits();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
package com.baeldung.concurrent.semaphores;
|
package com.baeldung.concurrent.semaphores;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class SemaphoresManualTest {
|
public class SemaphoresManualTest {
|
||||||
|
|
||||||
|
@ -20,12 +20,7 @@ public class SemaphoresManualTest {
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
||||||
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
|
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
|
||||||
IntStream.range(0, slots)
|
IntStream.range(0, slots)
|
||||||
.forEach(user -> executorService.execute(new Runnable() {
|
.forEach(user -> executorService.execute(loginQueue::tryLogin));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
loginQueue.tryLogin();
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
|
||||||
assertEquals(0, loginQueue.availableSlots());
|
assertEquals(0, loginQueue.availableSlots());
|
||||||
|
@ -38,12 +33,7 @@ public class SemaphoresManualTest {
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
||||||
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
|
final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots);
|
||||||
IntStream.range(0, slots)
|
IntStream.range(0, slots)
|
||||||
.forEach(user -> executorService.execute(new Runnable() {
|
.forEach(user -> executorService.execute(loginQueue::tryLogin));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
loginQueue.tryLogin();
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
|
||||||
assertEquals(0, loginQueue.availableSlots());
|
assertEquals(0, loginQueue.availableSlots());
|
||||||
|
@ -60,12 +50,7 @@ public class SemaphoresManualTest {
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
||||||
final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots);
|
final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots);
|
||||||
IntStream.range(0, slots)
|
IntStream.range(0, slots)
|
||||||
.forEach(user -> executorService.execute(new Runnable() {
|
.forEach(user -> executorService.execute(delayQueue::tryAdd));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
delayQueue.tryAdd();
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
|
||||||
assertEquals(0, delayQueue.availableSlots());
|
assertEquals(0, delayQueue.availableSlots());
|
||||||
|
@ -78,12 +63,7 @@ public class SemaphoresManualTest {
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
final ExecutorService executorService = Executors.newFixedThreadPool(slots);
|
||||||
final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots);
|
final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots);
|
||||||
IntStream.range(0, slots)
|
IntStream.range(0, slots)
|
||||||
.forEach(user -> executorService.execute(new Runnable() {
|
.forEach(user -> executorService.execute(delayQueue::tryAdd));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
delayQueue.tryAdd();
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
|
||||||
assertEquals(0, delayQueue.availableSlots());
|
assertEquals(0, delayQueue.availableSlots());
|
||||||
|
@ -100,16 +80,13 @@ public class SemaphoresManualTest {
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(count);
|
final ExecutorService executorService = Executors.newFixedThreadPool(count);
|
||||||
final CounterUsingMutex counter = new CounterUsingMutex();
|
final CounterUsingMutex counter = new CounterUsingMutex();
|
||||||
IntStream.range(0, count)
|
IntStream.range(0, count)
|
||||||
.forEach(user -> executorService.execute(new Runnable() {
|
.forEach(user -> executorService.execute(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
counter.increase();
|
||||||
try {
|
} catch (final InterruptedException e) {
|
||||||
counter.increase();
|
e.printStackTrace();
|
||||||
} catch (final InterruptedException e) {
|
}
|
||||||
e.printStackTrace();
|
}));
|
||||||
}
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
|
||||||
assertTrue(counter.hasQueuedThreads());
|
assertTrue(counter.hasQueuedThreads());
|
||||||
|
@ -121,16 +98,13 @@ public class SemaphoresManualTest {
|
||||||
final ExecutorService executorService = Executors.newFixedThreadPool(count);
|
final ExecutorService executorService = Executors.newFixedThreadPool(count);
|
||||||
final CounterUsingMutex counter = new CounterUsingMutex();
|
final CounterUsingMutex counter = new CounterUsingMutex();
|
||||||
IntStream.range(0, count)
|
IntStream.range(0, count)
|
||||||
.forEach(user -> executorService.execute(new Runnable() {
|
.forEach(user -> executorService.execute(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
counter.increase();
|
||||||
try {
|
} catch (final InterruptedException e) {
|
||||||
counter.increase();
|
e.printStackTrace();
|
||||||
} catch (final InterruptedException e) {
|
}
|
||||||
e.printStackTrace();
|
}));
|
||||||
}
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
assertTrue(counter.hasQueuedThreads());
|
assertTrue(counter.hasQueuedThreads());
|
||||||
Thread.sleep(5000);
|
Thread.sleep(5000);
|
||||||
|
|
Loading…
Reference in New Issue