2012-09-02 27 views
6

我想了解ThreadPoolExecutor類。我已閱讀此answer和Javadoc。但我的實驗不匹配這樣的描述:ThreadPoolExecutor的maximumPoolSize如何工作?

我初始化線程池與工廠跟蹤IDS

int tcounter = 0; 
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 1, TimeUnit.MINUTES, 
     new ArrayBlockingQueue<Runnable>(1000), new ThreadFactory() { 

      @Override 
      public Thread newThread(Runnable r) { 
       return new mThread(tcounter++, r); 
      } 
     }); 

public class mThread extends Thread { 
    int id; 

    private mThread(int id, Runnable run) { 
     super(run); 
     GLog.e("created thread " + id); 
     this.id = id; 
    } 

} 

接任務:

public class mRunanble implements Runnable { 
    int value = 0; 

    private mRunanble(int value) { 
     super(); 
     this.value = value; 
    } 

    @Override 
    public void run() { 
     SystemClock.sleep(3000); 
     Thread t = Thread.currentThread(); 
     if (t instanceof mThread) { 

      GLog.e("Say " + (value) + " on thread " + ((mThread) t).id); 
     } 

    } 

} 

,並指定一個按鈕的動作:

executor.execute(new mRunanble(i++)); 

但我垃圾郵件的按鈕和第三個線程永遠不會創建,那麼ThreadPoolExecutor構造函數中的第二個參數是什麼(maximumPoolSize=4)。 我specting 4個線程被創建,其中2 1分鐘後執行

+0

什麼是'Runa'? 'mRunanble'? – Jeffrey

+0

對不起,現在編輯 – Addev

+0

是因爲你的線程在睡覺嗎?也許改變你的runnable做一個忙等待會給你你期待的結果 – happymeal

回答

2

在ThreadPoolExecutor的maximumPoolSize進來畫面年底被殺害時corePoolSize沒有不足以執行你的任務,如果一切沒有都被任務佔用,然後只有一個花樣被創建來執行任務。這個不可以長到maxPoolSize。

編輯您錯過了maxPoolsize的概念。請參閱下面的鏈接。

http://www.bigsoft.co.uk/blog/index.php/2009/11/27/rules-of-a-threadpoolexecutor-pool-size

+0

但是所有OP的核心線程*都被任務佔用。 – Jeffrey

+0

因此,當我垃圾按鈕額外的2個線程,直到maximumPoolSize應創建正確? – Addev

+0

更新了答案plz檢查。以此爲例, – amicngh

4

從API爲ThreadPoolExecutor

,如果多於corePoolSize大於maximumPoolSize以上但 線程中運行,一個新的線程將只如果隊列滿 創建。

由於您的隊列的容量爲1000,所以您的隊列永不填充。如果將容量更改爲1,則會看到Thread正在創建。

Executors類使用SynchronousQueue作爲其newCachedThreadPool方法,因此您可能還想考慮使用它。

+0

如果我減少該值,我會盡快減少數量,然後它會拒絕這些任務,從而得到一個java.util.concurrent.RejectedExecutionException – Addev

+1

。您可以在此異常之前打印池大小,最大限度地減少。 – amicngh

0

爲了使線程池創建新的附加線程(根據maximumPoolSize參數擴展池的大小)試圖執行這個簡單的例子:

public class Main { 

    public static void main(String[] args) throws InterruptedException { 

     ThreadPoolExecutor tpe = new ThreadPoolExecutor(
       1, 2, 500, TimeUnit.MILLISECONDS, 
       new LinkedBlockingQueue<>(1)); 
     System.out.println("init pool size= " + tpe.getPoolSize() + ", queue size=" + tpe.getQueue().size()); 

     tpe.execute(new Task("1st", 10000)); 
     Thread.sleep(1000); 
     print(tpe, "1st"); 

     tpe.execute(new Task("2nd", 0)); 
     Thread.sleep(1000); 
     print(tpe, "2nd"); 

     tpe.execute(new Task("3d", 2000)); 
     Thread.sleep(1000); 
     print(tpe, "3d"); 

     while (tpe.getPoolSize()>1) {   
      Thread.sleep(100); 
     } 
     System.out.println("pool size= " + tpe.getPoolSize() + ", queue size=" + tpe.getQueue().size()); 
     tpe.shutdown(); 
    } 

    private static void print(ThreadPoolExecutor tpe, String name) { 
     System.out.println("After " + name + " execute - pool size= " + tpe.getPoolSize() + ", queue=" + tpe.getQueue()); 
    } 

    private static class Task implements Runnable { 

     private final String name; 
     private final long time; 

     Task(String name, long time) { 
      this.name = name; 
      this.time = time; 
     } 

     @Override 
     public void run() { 
      System.out.println("Run " + Thread.currentThread().getName() + "-" + name); 
      try { 
       Thread.sleep(time); 
      } catch (InterruptedException ex) { 
       Thread.currentThread().interrupt(); 
      } 
      System.out.println("Finish " + Thread.currentThread().getName() + "-" + name); 
     } 

     @Override 
     public String toString() { 
      return name; 
     } 

    } 
} 

你會得到這表明影響maximumPoolSize輸出和keepAliveTime

init pool size= 0, queue size=0 
Run pool-1-thread-1-1st 
After 1st execute - pool size= 1, queue=[] 
After 2nd execute - pool size= 1, queue=[2nd] 
Run pool-1-thread-2-3d 
After 3d execute - pool size= 2, queue=[2nd] 
Finish pool-1-thread-2-3d 
Run pool-1-thread-2-2nd 
Finish pool-1-thread-2-2nd 
pool size= 1, queue size=0 
Finish pool-1-thread-1-1st 
相關問題