當前位置:編程學習大全網 - 編程語言 - Java線程:新特征-線程池

Java線程:新特征-線程池

Sun在Java 中 對Java線程的類庫做了大量的擴展 其中線程池就是Java 的新特征之壹 除了線程池之外 還有很多多線程相關的內容 為多線程的編程帶來了極大便利 為了編寫高效穩定可靠的多線程程序 線程部分的新增內容顯得尤為重要 ? 有關Java 線程新特征的內容全部在ncurrent下面 裏面包含數目眾多的接口和類 熟悉這部分API特征是壹項艱難的學習過程 目前有關這方面的資料和書籍都少之又少 大所屬介紹線程方面書籍還停留在java 之前的知識層面上 ? 當然新特征對做多線程程序沒有必須的關系 在java 之前通用可以寫出很優秀的多線程程序 只是代價不壹樣而已 ? 線程池的基本思想還是壹種對象池的思想 開辟壹塊內存空間 裏面存放了眾多(未死亡)的線程 池中線程執行調度由池管理器來處理 當有線程任務時 從池中取壹個 執行完成後線程對象歸池 這樣可以避免反復創建線程對象所帶來的性能開銷 節省了系統的資源 ? 在Java 之前 要實現壹個線程池是相當有難度的 現在Java 為我們做好了壹切 我們只需要按照提供的API來使用 即可享受線程池帶來的極大便利 ? Java 的線程池分好多種 固定尺寸的線程池 可變尺寸連接池 ? 在使用線程池之前 必須知道如何去創建壹個線程池 在Java 中 需要了解的是ncurrent Executors類的API 這個類提供大量創建連接池的靜態方法 是必須掌握的 ? 壹 固定大小的線程池 ? import ncurrent Executors; import ncurrent ExecutorService; /** * Java線程 線程池 * * @author Administrator : : */ public class Test { public static void main(String[] args) { //創建壹個可重用固定線程數的線程池 ExecutorService pool = Executors newFixedThreadPool( ); //創建實現了Runnable接口對象 Thread對象當然也實現了Runnable接口 Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); //將線程放入池中進行執行 pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); //關閉線程池 pool shutdown(); } } class MyThread extends Thread{ @Override public void run() { System out println(Thread currentThread() getName()+ 正在執行 ); } } ? pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 Process finished with exit code ? 二 單任務線程池 ? 在上例的基礎上改壹行創建pool對象的代碼為 //創建壹個使用單個 worker 線程的 Executor 以無界隊列方式來運行該線程 ExecutorService pool = Executors newSingleThreadExecutor(); ? 輸出結果為 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 Process finished with exit code ? 對於以上兩種連接池 大小都是固定的 當要加入的池的線程(或者任務)超過池最大尺寸時候 則入此線程池需要排隊等待 壹旦池中有線程完畢 則排隊等待的某個線程會入池執行 三 可變尺寸的線程池 ? 與上面的類似 只是改動下pool的創建方式 //創建壹個可根據需要創建新線程的線程池 但是在以前構造的線程可用時將重用它們 ExecutorService pool = Executors newCachedThreadPool(); ? pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 Process finished with exit code ? 四 延遲連接池 ? import ncurrent Executors; import ncurrent ScheduledExecutorService; import ncurrent TimeUnit; /** * Java線程 線程池 * * @author Administrator : : */ public class Test { public static void main(String[] args) { //創建壹個線程池 它可安排在給定延遲後運行命令或者定期地執行 ScheduledExecutorService pool = Executors newScheduledThreadPool( ); //創建實現了Runnable接口對象 Thread對象當然也實現了Runnable接口 Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); //將線程放入池中進行執行 pool execute(t ); pool execute(t ); pool execute(t ); //使用延遲執行風格的方法 pool schedule(t TimeUnit MILLISECONDS); pool schedule(t TimeUnit MILLISECONDS); //關閉線程池 pool shutdown(); } } class MyThread extends Thread { @Override public void run() { System out println(Thread currentThread() getName() + 正在執行 ); } } ? pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 Process finished with exit code 五 單任務延遲連接池 ? 在四代碼基礎上 做改動 //創建壹個單線程執行程序 它可安排在給定延遲後運行命令或者定期地執行 ScheduledExecutorService pool = Executors newSingleThreadScheduledExecutor(); ? pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 Process finished with exit code ? 六 自定義線程池 ? import ncurrent ArrayBlockingQueue; import ncurrent BlockingQueue; import ncurrent ThreadPoolExecutor; import ncurrent TimeUnit; /** * Java線程 線程池 自定義線程池 * * @author Administrator : : */ public class Test { public static void main(String[] args) { //創建等待隊列 BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>( ); //創建壹個單線程執行程序 它可安排在給定延遲後運行命令或者定期地執行 ThreadPoolExecutor pool = new ThreadPoolExecutor( TimeUnit MILLISECONDS bqueue); //創建實現了Runnable接口對象 Thread對象當然也實現了Runnable接口 Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); Thread t = new MyThread(); //將線程放入池中進行執行 pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); pool execute(t ); //關閉線程池 pool shutdown(); } } class MyThread extends Thread { @Override public void run() { System out println(Thread currentThread() getName() + 正在執行 ); try { Thread sleep( L); } catch (InterruptedException e) { e printStackTrace(); } } } pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 pool thread 正在執行 Process finished with exit code ? 創建自定義線程池的構造方法很多 本例中參數的含義如下 ThreadPoolExecutor 用給定的初始參數和默認的線程工廠及處理程序創建新的 ThreadPoolExecutor 使用 Executors 工廠方法之壹比使用此通用構造方法方便得多 參數 corePoolSize 池中所保存的線程數 包括空閑線程 maximumPoolSize 池中允許的最大線程數 keepAliveTime 當線程數大於核心時 此為終止前多余的空閑線程等待新任務的最長時間 unit keepAliveTime 參數的時間單位 workQueue 執行前用於保持任務的隊列 此隊列僅保持由 execute 方法提交的 Runnable 任務 拋出 IllegalArgumentException 如果 corePoolSize 或 keepAliveTime 小於零 或者 maximumPoolSize 小於或等於零 或者 corePoolSize 大於 maximumPoolSize NullPointerException 如果 workQueue 為 null ? 自定義連接池稍微麻煩些 不過通過創建的ThreadPoolExecutor線程池對象 可以獲取到當前線程池的尺寸 正在執行任務的線程數 工作隊列等等 ? 有關Java 線程池的內容到此就沒有了 更多的內容還需要研讀API來獲取 lishixinzhi/Article/program/Java/hx/201311/26769

  • 上一篇:IT編程類的 大連最好的公司有哪些
  • 下一篇:Android插件化突破應用市場無法上廣告的問題
  • copyright 2024編程學習大全網