paranid5

PCSafeDeque

Jun 17th, 2021 (edited)
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.util.Queue;
  4. import java.util.concurrent.LinkedBlockingQueue;
  5. import java.util.function.Function;
  6.  
  7. final class Main {
  8.  
  9.     private static final class ThreadPool<T> implements Closeable {
  10.         private final Thread[] actionThreads;
  11.         private final Queue<T> taskQueue = new LinkedBlockingQueue<>();
  12.         private volatile boolean working = false;
  13.  
  14.         public ThreadPool(
  15.                 final int threadsAmount,
  16.                 final Function<T, Void> action
  17.         ) {
  18.             actionThreads = new Thread[threadsAmount];
  19.  
  20.             for (int i = 0; i < threadsAmount; i++)
  21.                 actionThreads[i] = new Thread(() -> {
  22.                     while (working || !taskQueue.isEmpty()) {
  23.                         final var v = taskQueue.poll();
  24.                         if (v != null) action.apply(v);
  25.                     }
  26.                 });
  27.         }
  28.  
  29.         public final void start() {
  30.             working = true;
  31.             Arrays.stream(actionThreads).forEach(Thread::start);
  32.         }
  33.  
  34.         public final void push(final T elem) {
  35.             taskQueue.offer(elem);
  36.         }
  37.  
  38.         @Override
  39.         public void close() {
  40.             working = false;
  41.         }
  42.     }
  43.  
  44.     private static final int fib(final int number) {
  45.         if (number < 3)
  46.             return 1;
  47.  
  48.         var f = 1;
  49.         var s = 1;
  50.  
  51.         for (int i = 3; i <= number; i++) {
  52.             final var mem = s;
  53.             s += f;
  54.             f = mem;
  55.         }
  56.  
  57.         return s;
  58.     }
  59.  
  60.     public static final void main(final String[] args) throws IOException {
  61.         final var out = System.out;
  62.  
  63.         try (final var reader = new BufferedReader(new InputStreamReader(System.in))) {
  64.             out.print("Threads amount: ");
  65.  
  66.             try  (final var pool = new ThreadPool<Integer>(
  67.                     Integer.parseInt(reader.readLine()),
  68.                     (num) -> {
  69.                         out.println(fib(num));
  70.                         return null;
  71.                     }
  72.             )) {
  73.                 try (final var fileReader = new BufferedReader(new FileReader("file.txt"))) {
  74.                     pool.start();
  75.                     fileReader.lines().forEach((x) -> pool.push(Integer.parseInt(x)));
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }
Add Comment
Please, Sign In to add comment