package com.wizzardo.epoll.threadpool;

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

/**
 * @author: moxa
 * Date: 4/13/13
 */
public class ThreadPool {

    private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();

    public ThreadPool(int threads) {
        this("ThreadPoolWorker", threads);
    }

    public ThreadPool(String name, int threads) {
        for (int i = 0; i < threads; i++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            queue.take().run();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            t.setDaemon(true);
            t.setName(name + "-" + i);
            t.start();
        }
    }

    public void add(Runnable r) {
        queue.add(r);
    }

}

@@ -281,16 +281,83 @@
 able%3E();
+%0A    private int threadsCount;%0A    private String threadNamePrefix;
 %0A%0A    pu
@@ -479,32 +479,193 @@
  int threads) %7B%0A
+        threadsCount = threads;%0A        threadNamePrefix = name;%0A        startThreads(threads, 0);%0A    %7D%0A%0A    private void startThreads(int count, int offset) %7B%0A
         for (int
@@ -676,23 +676,21 @@
  0; i %3C 
-threads
+count
 ; i++) %7B
@@ -1152,22 +1152,45 @@
 ame(
-name + %22-%22 + i
+threadNamePrefix + %22-%22 + (i + offset)
 );%0A 
@@ -1258,24 +1258,24 @@
 nnable r) %7B%0A
-
         queu
@@ -1291,10 +1291,376 @@
 %0A    %7D%0A%0A
+    public void increaseThreadsCountTo(int newThreadsCount) %7B%0A        if (newThreadsCount %3C threadsCount)%0A            throw new IllegalArgumentException(%22new threads count cannot be smaller than old value: %22 + newThreadsCount + %22 %3C %22 + threadsCount);%0A        startThreads(newThreadsCount - threadsCount, threadsCount);%0A        threadsCount = newThreadsCount;%0A    %7D%0A%0A
 %7D%0A

improve thread pool