1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-05 00:45:06 -05:00
filebot/source/net/sourceforge/tuned/PausableThreadPoolExecutor.java
Reinhard Pointner 04c4baf9b9 * support for arguments
* better tempfile management
* lots of refactoring
2008-03-19 22:14:38 +00:00

65 lines
1.2 KiB
Java

package net.sourceforge.tuned;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class PausableThreadPoolExecutor extends ThreadPoolExecutor {
private boolean paused;
private ReentrantLock pauseLock = new ReentrantLock();
private Condition unpaused = pauseLock.newCondition();
@SuppressWarnings("unchecked")
public PausableThreadPoolExecutor(int nThreads, BlockingQueue<? extends Runnable> workQueue) {
super(nThreads, nThreads, 0, TimeUnit.MILLISECONDS, (BlockingQueue<Runnable>) workQueue);
}
@Override
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
pauseLock.lock();
try {
while (paused)
unpaused.await();
} catch (InterruptedException ie) {
t.interrupt();
} finally {
pauseLock.unlock();
}
}
public void pause() {
pauseLock.lock();
try {
paused = true;
} finally {
pauseLock.unlock();
}
}
public void resume() {
pauseLock.lock();
try {
paused = false;
unpaused.signalAll();
} finally {
pauseLock.unlock();
}
}
public boolean isPaused() {
return paused;
}
}