Created
June 20, 2018 07:58
-
-
Save franz1981/8ace3a81a47514380a0f66b8e4e077ce 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; | |
lock.lock(); | |
try { | |
if (tasks.isEmpty()) { | |
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