Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. private final Queue<Runnable> tasks = new MpscArrayQueue<>(64);
  2. private final ReentrantLock lock = new ReentrantLock();
  3. private final Condition condition = lock.newCondition();
  4. private volatile boolean running = true;
  5. private final Thread executorThread = new Thread(() -> {
  6. do {
  7. running = true;
  8. Runnable task;
  9. while ((task = tasks.poll()) != null) {
  10. try {
  11. task.run();
  12. } catch (Throwable t) {
  13. t.printStackTrace();
  14. }
  15. }
  16. running = false;
  17. if (!tasks.isEmpty()) {
  18. continue;
  19. }
  20. lock.lock();
  21. try {
  22. try {
  23. condition.await();
  24. } catch (InterruptedException e) {
  25. Thread.currentThread().interrupt();
  26. //NOOP
  27. }
  28. } finally {
  29. lock.unlock();
  30. }
  31. } while (!Thread.interrupted());
  32. //simple eh? :P
  33. tasks.clear();
  34. });
Add Comment
Please, Sign In to add comment