semaphores (#2198)

* minor logging fix

* spring security sso

* use basic auth

* use form login

* cleanup

* cleanup

* final cleanup

* second client app for sso

* spring boot bootstrap

* add logic

* cleanup

* add simple controller

* add thymeleaf and security

* minor fix

* minor fix

* add more boot properties

* fix live test

* fix live test

* minor fix

* semaphores
This commit is contained in:
Doha2012
2017-07-03 17:34:41 +02:00
committed by Grzegorz Piwowarek
parent 7c6a08746e
commit 20669dc6a1
4 changed files with 219 additions and 0 deletions
@@ -0,0 +1,31 @@
package com.baeldung.concurrent.semaphores;
import java.util.concurrent.Semaphore;
public class CounterUsingMutex {
private final Semaphore mutex;
private int count;
public CounterUsingMutex() {
mutex = new Semaphore(1);
count = 0;
}
public void increase() throws InterruptedException {
mutex.acquire();
this.count = this.count + 1;
Thread.sleep(1000);
mutex.release();
}
public int getCount() {
return this.count;
}
public boolean hasQueuedThreads() {
return mutex.hasQueuedThreads();
}
}
@@ -0,0 +1,23 @@
package com.baeldung.concurrent.semaphores;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.concurrent.TimedSemaphore;
public class DelayQueueUsingTimedSemaphore {
private final TimedSemaphore semaphore;
public DelayQueueUsingTimedSemaphore(long period, int slotLimit) {
semaphore = new TimedSemaphore(period, TimeUnit.SECONDS, slotLimit);
}
public boolean tryAdd() {
return semaphore.tryAcquire();
}
public int availableSlots() {
return semaphore.getAvailablePermits();
}
}
@@ -0,0 +1,25 @@
package com.baeldung.concurrent.semaphores;
import java.util.concurrent.Semaphore;
public class LoginQueueUsingSemaphore {
private final Semaphore semaphore;
public LoginQueueUsingSemaphore(int slotLimit) {
semaphore = new Semaphore(slotLimit);
}
public boolean tryLogin() {
return semaphore.tryAcquire();
}
public void logout() {
semaphore.release();
}
public int availableSlots() {
return semaphore.availablePermits();
}
}