mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-17 14:59:37 +00:00
JUnit tests
This commit is contained in:
@@ -27,13 +27,29 @@ package com.iluwatar.semaphore;
|
||||
*/
|
||||
public class Semaphore implements Lock {
|
||||
|
||||
private final int licenses;
|
||||
/**
|
||||
* The number of concurrent resource accesses which are allowed.
|
||||
*/
|
||||
private int counter;
|
||||
|
||||
public Semaphore(int counter) {
|
||||
this.counter = counter;
|
||||
public Semaphore(int licenses) {
|
||||
this.licenses = licenses;
|
||||
this.counter = licenses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of licenses managed by the Semaphore
|
||||
*/
|
||||
public int getNumLicenses() {
|
||||
return licenses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of available licenses
|
||||
*/
|
||||
public int getAvailableLicenses() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,8 +68,10 @@ public class Semaphore implements Lock {
|
||||
* Method called by a thread to release the lock.
|
||||
*/
|
||||
public synchronized void release() {
|
||||
counter = counter + 1;
|
||||
notify();
|
||||
if (counter < licenses) {
|
||||
counter = counter + 1;
|
||||
notify();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user