mirror of
https://github.com/tonikelope/megabasterd.git
synced 2025-05-23 18:16:18 +02:00
52 lines
1.4 KiB
Java
52 lines
1.4 KiB
Java
/*
|
|
__ __ _ _ _
|
|
| \/ | ___ __ _ __ _| |__ __ _ ___| |_ ___ _ __ __| |
|
|
| |\/| |/ _ \/ _` |/ _` | '_ \ / _` / __| __/ _ \ '__/ _` |
|
|
| | | | __/ (_| | (_| | |_) | (_| \__ \ || __/ | | (_| |
|
|
|_| |_|\___|\__, |\__,_|_.__/ \__,_|___/\__\___|_| \__,_|
|
|
|___/
|
|
© Perpetrated by tonikelope since 2016
|
|
*/
|
|
package com.tonikelope.megabasterd;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.RejectedExecutionException;
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
/**
|
|
*
|
|
* @author tonikelope
|
|
*/
|
|
public class BoundedExecutor {
|
|
|
|
private final ExecutorService exec;
|
|
private final Semaphore semaphore;
|
|
|
|
public BoundedExecutor(ExecutorService exec, int bound) {
|
|
this.exec = exec;
|
|
this.semaphore = new Semaphore(bound);
|
|
}
|
|
|
|
public void submitTask(final Runnable command)
|
|
throws InterruptedException {
|
|
|
|
semaphore.acquire();
|
|
try {
|
|
exec.execute(new Runnable() {
|
|
public void run() {
|
|
try {
|
|
command.run();
|
|
} finally {
|
|
semaphore.release();
|
|
}
|
|
}
|
|
});
|
|
} catch (RejectedExecutionException e) {
|
|
|
|
semaphore.release();
|
|
|
|
}
|
|
|
|
}
|
|
}
|