Last active
June 20, 2018 09:14
-
-
Save franz1981/69848c9d59377d0cc29e353dc6141d78 to your computer and use it in GitHub Desktop.
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
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