Java Executors(線程池)

來源:互聯網
上載者:User

        Sun在Java5中,對Java線程的類庫做了大量的擴充,其中線程池就是Java5的新特徵之一,除了線程池之外,還有很多多線程相關的內容,為多線程的編程帶來了極大便利。為了編寫高效穩定可靠的多線程程式,線程部分的新增內容顯得尤為重要。

  有關Java5線程新特徵的內容全部在java.util.concurrent下面,裡麵包含數目眾多的介面和類,熟悉這部分API特徵是一項艱難的學習過程。目前有關這方面的資料和書籍都少之又少,大所屬介紹線程方面書籍還停留在java5之前的知識層面上。

  當然新特徵對做多線程程式沒有必須的關係,在java5之前通用可以寫出很優秀的多線程程式。只是代價不一樣而已。

  線程池的基本思想還是一種對象池的思想,開闢一塊記憶體空間,裡面存放了眾多(未死亡)的線程,池中線程執行調度由池管理器來處理。當有線程任務時,從池中取一個,執行完成後線程對象歸池,這樣可以避免反覆建立線程對象所帶來的效能開銷,節省了系統的資源。

  在Java5之前,要實現一個線程池是相當有難度的,現在Java5為我們做好了一切,我們只需要按照提供的API來使用,即可享受線程池帶來的極大便利。

  Java5的線程池分好多種:具體的可以分為兩類,固定尺寸的線程池、可變尺寸串連池。

  在使用線程池之前,必須知道如何去建立一個線程池,在Java5中,需要瞭解的是java.util.concurrent.Executors類的API,這個類提供大量建立串連池的靜態方法,是必須掌握的。

一、固定大小的線程池,newFixedThreadPool:

package app.executors;import java.util.concurrent.Executors;import java.util.concurrent.ExecutorService;/** * Java線程:線程池 *  * @author 馮小衛 */public class Test {public static void main(String[] args) {// 建立一個可重用固定線程數的線程池ExecutorService pool = Executors.newFixedThreadPool(5);// 建立線程Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();Thread t4 = new MyThread();Thread t5 = new MyThread();// 將線程放入池中進行執行pool.execute(t1);pool.execute(t2);pool.execute(t3);pool.execute(t4);pool.execute(t5);// 關閉線程池pool.shutdown();}}class MyThread extends Thread {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + "正在執行。。。");}}

輸出結果:

pool-1-thread-1正在執行。。。pool-1-thread-3正在執行。。。pool-1-thread-4正在執行。。。pool-1-thread-2正在執行。。。pool-1-thread-5正在執行。。。

改變ExecutorService pool = Executors.newFixedThreadPool(5)中的參數:ExecutorService pool = Executors.newFixedThreadPool(2),輸出結果是:

pool-1-thread-1正在執行。。。pool-1-thread-1正在執行。。。pool-1-thread-2正在執行。。。pool-1-thread-1正在執行。。。pool-1-thread-2正在執行。。。

從以上結果可以看出,newFixedThreadPool的參數指定了可以啟動並執行線程的最大數目,超過這個數目的線程加進去以後,不會運行。其次,加入線程池的線程屬於託管狀態,線程的運行不受加入順序的影響。

 

二、單任務線程池,newSingleThreadExecutor:

僅僅是把上述代碼中的ExecutorService pool = Executors.newFixedThreadPool(2)改為ExecutorService pool = Executors.newSingleThreadExecutor();

輸出結果:

pool-1-thread-1正在執行。。。pool-1-thread-1正在執行。。。pool-1-thread-1正在執行。。。pool-1-thread-1正在執行。。。pool-1-thread-1正在執行。。。

可以看出,每次調用execute方法,其實最後都是調用了thread-1的run方法。

 

三、可變尺寸的線程池,newCachedThreadPool:

與上面的類似,只是改動下pool的建立方式:ExecutorService pool = Executors.newCachedThreadPool();

輸出:

pool-1-thread-1正在執行。。。pool-1-thread-2正在執行。。。pool-1-thread-4正在執行。。。pool-1-thread-3正在執行。。。pool-1-thread-5正在執行。。。

這種方式的特點是:可根據需要建立新線程的線程池,但是在以前構造的線程可用時將重用它們。

四、延遲串連池,newScheduledThreadPool:

package app.executors;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;/** * Java線程:線程池 *  * @author 馮小衛 */public class Test {public static void main(String[] args) {// 建立一個線程池,它可安排在給定延遲後運行命令或者定期地執行。ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);// 建立實現了Runnable介面對象,Thread對象當然也實現了Runnable介面Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();// 將線程放入池中進行執行pool.execute(t1);// 使用順延強制風格的方法pool.schedule(t2, 1000, TimeUnit.MILLISECONDS);pool.schedule(t3, 10, TimeUnit.MILLISECONDS);// 關閉線程池pool.shutdown();}}class MyThread extends Thread {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + "正在執行。。。");}}

讀者可以嘗試改變Executors.newScheduledThreadPool(2)的參數來得到更多的體驗,當然,讓

@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + "正在執行。。。");}

 

變成一個無限迴圈,你可以得到更多的關於pool.shutdown()的用法。

 

五:單任務延遲串連池(和上面類似,就不寫了)。當然我們也可以自訂線程池,這裡就不寫了,累啊……

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.