Advertisement
Guest User

Untitled

a guest
May 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. package ru.sberbank;
  2.  
  3.  
  4. import java.util.Queue;
  5. import java.util.concurrent.ConcurrentLinkedQueue;
  6.  
  7. public class FixedThreadPool implements ThreadPool {
  8.  
  9.   private final Queue<Runnable> workQueue = new ConcurrentLinkedQueue<>();
  10.   private volatile boolean isRunning = true;
  11.   private final int numberOfThreads;
  12.  
  13.   private int completed = 0;
  14.   private int failed = 0;
  15.  
  16.  
  17.   private synchronized void incCompleted() {
  18.     ++completed;
  19.   }
  20.  
  21.  
  22.   int getCompletedTasks() {
  23.     return completed;
  24.   }
  25.  
  26.  
  27.   private synchronized void incFailed() {
  28.     ++failed;
  29.   }
  30.  
  31.  
  32.   int getFailedTasks() {
  33.     return failed;
  34.   }
  35.  
  36.  
  37.   int getQueueSize() {
  38.     return workQueue.size();
  39.   }
  40.  
  41.  
  42.   public FixedThreadPool(int numberOfThreads) {
  43.     this.numberOfThreads = numberOfThreads;
  44.   }
  45.  
  46.  
  47.   @Override
  48.   public void start() {
  49.     for (int i = 0; i < numberOfThreads; i++) {
  50.       new Thread(new Worker()).start();
  51.     }
  52.   }
  53.  
  54.  
  55.   @Override
  56.   public void execute(Runnable command) {
  57.     if (isRunning) {
  58.       workQueue.offer(command);
  59.     }
  60.   }
  61.  
  62.  
  63.   public void shutdown() {
  64.     isRunning = false;
  65.   }
  66.  
  67.  
  68.   private final class Worker implements Runnable {
  69.  
  70.     @Override
  71.     public void run() {
  72.  
  73.       while (isRunning) {
  74.         Runnable nextTask = workQueue.poll();
  75.         if (nextTask != null) {
  76.           try {
  77.             nextTask.run();
  78.             incCompleted();
  79.           } catch (Exception e) {
  80.             e.printStackTrace();
  81.             incFailed();
  82.           }
  83.         }
  84.       }
  85.     }
  86.  
  87.  
  88.   }
  89.  
  90.  
  91.   public static void main(String[] args) {
  92.     FixedThreadPool threadPool = new FixedThreadPool(5);
  93.     threadPool.start();
  94.  
  95.     for (int j = 0; j < 10; ++j) {
  96.       threadPool.execute(() -> {
  97.         double sum = 0;
  98.         for (int i = 0; i < 10_000_000; ++i) {
  99.           sum += Math.random();
  100.         }
  101.  
  102.         System.out.println(Thread.currentThread().getName());
  103.         System.out.println(sum);
  104.       });
  105.     }
  106.  
  107.     try {
  108.       Thread.sleep(10000);
  109.     } catch (InterruptedException e) {
  110.       e.printStackTrace();
  111.     }
  112.  
  113.     threadPool.shutdown();
  114.  
  115.  
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement