以執行個體簡介Java中線程池的工作特點_java

來源:互聯網
上載者:User

什麼原因使我們不得不使用線程池?

個人認為主要原因是:短時間內需要處理的任務數量很多

使用線程池的好處:

1.減少在建立和銷毀線程上所花的時間以及系統資源的開銷
2.如不使用線程池,有可能造成系統建立大量線程而導致消耗完系統記憶體

以下是Java內建的幾種線程池:

1、newFixedThreadPool  建立一個指定背景工作執行緒數量的線程池。

每當提交一個任務就建立一個背景工作執行緒,如果背景工作執行緒數量達到線程池初始的最大數,則將提交的任務存入到池隊列中。

2、newCachedThreadPool 建立一個可快取的線程池。

這種類型的線程池特點是:

1).背景工作執行緒的建立數量幾乎沒有限制(其實也有限制的,數目為Interger. MAX_VALUE), 這樣可靈活的往線程池中添加線程。

2).如果長時間沒有往線程池中提交任務,即如果背景工作執行緒空閑了指定的時間(預設為1分鐘),則該背景工作執行緒將自動終止。終止後,如果你又提交了新的任務,則線程池重新建立一個背景工作執行緒。

3、newSingleThreadExecutor 建立一個單線程化的Executor,即只建立唯一的工作者線程來執行任務,如果這個線程異常結束,會有另一個取代它,保證順序執行(我覺得這點是它的特色)。

單背景工作執行緒最大的特點是可保證順序地執行各個任務,並且在任意給定的時間不會有多個線程是活動的 。

4、newScheduleThreadPool  建立一個定長的線程池,而且支援定時的以及周期性的任務執行,類似於Timer。

總結:

一.FixedThreadPool是一個典型且優秀的線程池,它具有線程池提高程式效率和節省建立線程時所耗的開銷的優點。但線上程池空閑時,即線程池中沒有可運行任務時,它不會釋放背景工作執行緒,還會佔用一定的系統資源。

二.CachedThreadPool的特點就是線上程池空閑時,即線程池中沒有可運行任務時,它會釋放背景工作執行緒,從而釋放背景工作執行緒所佔用的資源。但是,但當出現新任務時,又要建立一新的背景工作執行緒,又要一定的系統開銷。並且,在使用CachedThreadPool時,一定要注意控制任務的數量,否則,由於大量線程同時運行,很有會造成系統癱瘓。

Java線程池 ThreadPoolExecutor使用執行個體

package com.sondon.mayi.jpool;  import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;  public class JPoolLearn {   private static int produceTaskSleepTime = 3;  private static int produceTaskMaxNumber = 20;    public void testThreadPoolExecutor(){   /*    * ThreadPoolExecutor(    * int corePoolSize, //線程池維護線程的最少數量    * int maximumPoolSize, //線程池維護線程的最大數量    * long keepAliveTime, //線程池維護線程所允許的空閑時間    * TimeUnit unit, //線程池維護線程所允許的空閑時間的單位    * BlockingQueue<Runnable> workQueue, //線程池所使用的緩衝隊列    * RejectedExecutionHandler handler //線程池對拒絕任務的處理策略 )    */   ThreadPoolExecutor threadPool = new ThreadPoolExecutor(     5,     10,     3,     TimeUnit.SECONDS,     new ArrayBlockingQueue<Runnable>(10),     new ThreadPoolExecutor.DiscardOldestPolicy()     );    for (int i = 1; i <= produceTaskMaxNumber; i++) {    try {     // 產生一個任務,並將其加入到線程池     String task = "task---" + i;     threadPool.execute(new ThreadPoolTask(task));     System.out.println("activeCount :"+ threadPool.getActiveCount());     // 便於觀察,等待一段時間     Thread.sleep(produceTaskSleepTime);    } catch (Exception e) {     e.printStackTrace();    }   }      //查看當前的線程池狀況   while(true){    try {     Thread.sleep(3000);     System.out.println("pool size :"+threadPool.getPoolSize());//線程池中線程數量     System.out.println("active count :"+threadPool.getActiveCount());//線程池中活動的線程數量    } catch (InterruptedException e) {     e.printStackTrace();    }   }  }   /**   *   * @Author 蔡文鋒   * @Data_Time 2015年7月25日 下午4:06:28   * @Description { 測試不同線程池模式 }   */  public void testNewCachedThreadPool(){   ThreadPoolExecutor threadPool=(ThreadPoolExecutor) Executors.newCachedThreadPool(); //  ThreadPoolExecutor threadPool=(ThreadPoolExecutor) Executors.newFixedThreadPool(100); //  ThreadPoolExecutor threadPool=(ThreadPoolExecutor) Executors.newScheduledThreadPool(100); //  ThreadPoolExecutor threadPool=(ThreadPoolExecutor) Executors.newSingleThreadExecutor();   try {   for (int i = 0; i < 100; i++) {    // 產生一個任務,並將其加入到線程池    String task = "task---" + i;    threadPool.execute(new ThreadPoolTask(task));    System.out.println("activeCount :");    // 便於觀察,等待一段時間    Thread.sleep(produceTaskSleepTime);        }   } catch (InterruptedException e) {    e.printStackTrace();   }   //查看當前的線程池狀況   while(true){    try {     Thread.sleep(3000);     System.out.println("pool size :"+threadPool.getPoolSize());//線程池中線程數量     System.out.println("active count :"+threadPool.getActiveCount());//線程池中活動的線程數量    } catch (InterruptedException e) {     e.printStackTrace();    }   }  }    /**   *   * @Author 蔡文鋒   * @Data_Time 2015年7月25日 下午4:06:58   * @Description { 測試callable與runable方法的區別 }   */  public void testNewCachedThreadPool_callable(){   ExecutorService es=Executors.newFixedThreadPool(10);   try {     //   String result=es.submit(new MyCallable<String>()).get(); //   System.out.println("callable result :"+result);        String result=(String) es.submit(new ThreadPoolTask("")).get();    System.out.println("runable result :"+result);       } catch (InterruptedException | ExecutionException e) {    e.printStackTrace();   }  }      public static void main(String[] args) {   new JPoolLearn().testNewCachedThreadPool();  } }    /**  * 線程池執行的任務  */ class ThreadPoolTask implements Runnable {  private static int consumeTaskSleepTime = 2000;  // 儲存任務所需要的資料  private Object threadPoolTaskData;   ThreadPoolTask(Object tasks) {   this.threadPoolTaskData = tasks;  }   public void run() {   System.out.println("start .." + threadPoolTaskData);   try {    // Sleep 2秒 類比耗時操作    Thread.sleep(consumeTaskSleepTime);   } catch (Exception e) {    e.printStackTrace();   }   threadPoolTaskData = null;  }   public Object getTask() {   return this.threadPoolTaskData;  } }  /**  *  * @Project : JPool  * @Package : com.sondon.mayi.jpool  * @Class : MyCallable  * @param <T>  */ class MyCallable<T> implements Callable<T>{    @Override  public T call() throws Exception {    System.out.println("開始執行Callable");    return (T) "測試callable介面";   } } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.