Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- });
Add Comment
Please, Sign In to add comment