-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #343 from gama-platform/Addresses-Issues-339-341-a…
…nd-monitor-order-issue
- Loading branch information
Showing
20 changed files
with
263 additions
and
139 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
gama.core/src/gama/core/common/interfaces/GeneralSynchronizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package gama.core.common.interfaces; | ||
|
||
import java.util.concurrent.Semaphore; | ||
|
||
/** | ||
* Encapsulates a general purpose {@link Semaphore}, with optional control over the maximum amount of permits allowed | ||
*/ | ||
public class GeneralSynchronizer { | ||
|
||
/** | ||
* Returns a synchronizer with {@code init} initial permits and no maximum amount | ||
* | ||
* @param init | ||
* @return | ||
*/ | ||
public static GeneralSynchronizer withInitialPermits(final int init) { | ||
return withInitialAndMaxPermits(init, Integer.MAX_VALUE); | ||
} | ||
|
||
/** | ||
* Returns a synchronizer with {@code init} initial permits and a maximum amount of permits of {@code max} | ||
* | ||
* @param init | ||
* @param max | ||
* @return | ||
*/ | ||
public static GeneralSynchronizer withInitialAndMaxPermits(final int init, final int max) { | ||
return new GeneralSynchronizer(init, max); | ||
} | ||
|
||
private GeneralSynchronizer(final int n, final int max) { | ||
semaphore = new Semaphore(n); | ||
this.max = max; | ||
} | ||
|
||
final Semaphore semaphore; | ||
int max; | ||
|
||
public void release() { | ||
if (semaphore.availablePermits() >= max) return; | ||
semaphore.release(); | ||
} | ||
|
||
public void acquire() { | ||
try { | ||
semaphore.acquire(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void release(final int nb) { | ||
int already = semaphore.availablePermits(); | ||
if (already >= max) return; | ||
semaphore.release(Math.min(max - already, nb)); | ||
} | ||
|
||
public void acquire(final int n) { | ||
try { | ||
semaphore.acquire(n); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.