Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. package io.github.staakk;
  2.  
  3. import java.util.Optional;
  4. import java.util.concurrent.ConcurrentLinkedQueue;
  5. import java.util.function.Consumer;
  6. import java.util.function.Supplier;
  7.  
  8. public class Main {
  9.  
  10.     private Worker worker = new Worker();
  11.  
  12.     public static void main(String[] args) {
  13.         Worker w = new Main().worker;
  14.  
  15.         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  16.             System.out.println("Shutting down gracefully");
  17.             w.halt();
  18.         }));
  19.  
  20.         w.start();
  21.  
  22.         Task t1 = Task.from(() -> {
  23.  
  24.         });
  25.  
  26.         w.schedule(t1);
  27.  
  28.         trySleep(2000);
  29.  
  30.         w.schedule(t1);
  31.  
  32.         while (true) {
  33.         }
  34.     }
  35.  
  36.     private static void trySleep(long ms) {
  37.         try {
  38.             Thread.sleep(ms);
  39.         } catch (InterruptedException e) {
  40.             // ignore
  41.         }
  42.     }
  43.  
  44. }
  45.  
  46. class Worker extends Thread {
  47.  
  48.     private volatile boolean running = true;
  49.  
  50.     private final ConcurrentLinkedQueue<Task> tasks = new ConcurrentLinkedQueue<>();
  51.  
  52.     @Override
  53.     public void run() {
  54.         System.out.println("Worker started");
  55.         while (running) {
  56.             Optional.ofNullable(tasks.poll())
  57.                     .ifPresent(Task::execute);
  58.  
  59.             if (tasks.isEmpty()) {
  60.                 try {
  61.                     System.out.println("Worker waiting");
  62.                     synchronized (this) {
  63.                         wait();
  64.                     }
  65.                 } catch (InterruptedException e) {
  66.                     System.out.println("Worker notified");
  67.                 }
  68.             }
  69.         }
  70.         System.out.println("Worker stopped");
  71.     }
  72.  
  73.     public void schedule(Task t) {
  74.         tasks.add(t);
  75.         synchronized (this) {
  76.             notify();
  77.         }
  78.     }
  79.  
  80.     public void halt() {
  81.         running = false;
  82.         synchronized (this) {
  83.             notify();
  84.             try {
  85.                 join();
  86.             } catch (InterruptedException e) {
  87.                 // ignored
  88.             }
  89.         }
  90.     }
  91. }
  92.  
  93. class Task<R> {
  94.  
  95.     private final Supplier<R> f;
  96.     private final Consumer<R> r;
  97.     private final Consumer<Exception> e;
  98.  
  99.     public static <R> Task<R> from(Supplier<R> f, Consumer<R> r, Consumer<Exception> e) {
  100.         return new Task<>(f, r, e);
  101.     }
  102.  
  103.     public static <R> Task<R> from(Supplier<R> f, Consumer<R> r) {
  104.         return from(f, r, Exception::printStackTrace);
  105.     }
  106.  
  107.     public static Task<Void> from(Runnable f) {
  108.         return from(
  109.                 () -> {
  110.                     f.run();
  111.                     return null;
  112.                 },
  113.                 v -> {
  114.                 },
  115.                 Exception::printStackTrace);
  116.     }
  117.  
  118.     private Task(Supplier<R> f, Consumer<R> r, Consumer<Exception> e) {
  119.         this.f = f;
  120.         this.r = r;
  121.         this.e = e;
  122.     }
  123.  
  124.     public void execute() {
  125.         try {
  126.             r.accept(f.get());
  127.         } catch (Exception e) {
  128.             this.e.accept(e);
  129.         }
  130.     }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement