Skip to content

Instantly share code, notes, and snippets.

@franz1981
Last active June 20, 2018 09:14
Show Gist options
  • Save franz1981/69848c9d59377d0cc29e353dc6141d78 to your computer and use it in GitHub Desktop.
Save franz1981/69848c9d59377d0cc29e353dc6141d78 to your computer and use it in GitHub Desktop.
private final Queue<Runnable> tasks = new MpscArrayQueue<>(64);
private final ReentrantLock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();
private volatile boolean running = true;
private final Thread executorThread = new Thread(() -> {
do {
running = true;
Runnable task;
while ((task = tasks.poll()) != null) {
try {
task.run();
} catch (Throwable t) {
t.printStackTrace();
}
}
running = false;
if (!tasks.isEmpty()) {
continue;
}
lock.lock();
try {
try {
condition.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
//NOOP
}
} finally {
lock.unlock();
}
} while (!Thread.interrupted());
//simple eh? :P
tasks.clear();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment