package ru.job4j.taskBlockCondition;

import com.sun.istack.internal.NotNull;

import java.util.Arrays;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Class for thread pool.
 * @author atrifonov.
 * @since 09.10.2017.
 * @version 1.
 */
public class SThreadPool {
    /**
     * Thread safe queue for adding and getting objects Runnable.
     */
    private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    /**
     * Array threads for execute tasks from queue.
     */
    private final MyThread[] threads;

    /**
     * Constructs object SThreadPool. In this object count of threads in array equal count of CPU on computer.
     */
    public SThreadPool() {
        threads = new MyThread[Runtime.getRuntime().availableProcessors()];
        for(MyThread t : threads) {
            t = new MyThread();
            t.start();
        }
    }

    /**
     * Add not null object Runnable to queue.
     * @param r object for execute.
     */
    public void add(Runnable r) {
        if (r != null) {
            try {
                queue.put(r);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    /**
     * Nested class is descendent of class Thread. Class for array threads.
     */
    private class MyThread extends Thread {
        @Override
        public void run() {
            Runnable r;
            while (!this.isInterrupted()) {
                try {
                    r = queue.take();
                    r.run();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

@@ -35,74 +35,8 @@
 n;%0A%0A
-import com.sun.istack.internal.NotNull;%0A%0Aimport java.util.Arrays;%0A
 impo
@@ -74,16 +74,16 @@
 gQueue;%0A
+
 import j
@@ -408,113 +408,8 @@
 %3E();
-%0A    /**%0A     * Array threads for execute tasks from queue.%0A     */%0A    private final MyThread%5B%5D threads;
 %0A%0A  
@@ -572,31 +572,27 @@
     
-threads = new MyThread%5B
+for(int i = 0; i %3C 
 Runt
@@ -633,43 +633,13 @@
 rs()
-%5D;%0A        for(MyThread t : threads
+; i++
 ) %7B%0A
@@ -653,27 +653,22 @@
     
- t =
  new 
-My
+S
 Thread()
 ;%0A  
@@ -667,23 +667,8 @@
 ad()
-;%0A            t
 .sta
@@ -1139,10 +1139,9 @@
 ass 
-My
+S
 Thre
@@ -1210,32 +1210,8 @@
 ) %7B%0A
-            Runnable r;%0A
     
@@ -1295,12 +1295,8 @@
     
- r =
  que
@@ -1308,31 +1308,8 @@
 ke()
-;%0A                    r
 .run
@@ -1549,17 +1549,16 @@
       %7D%0A
+
     %7D%0A
-%0A
 %7D%0A

ed task 1099