亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3095 | 回復: 1
打印 上一主題 下一主題

java并發(fā)編程--線程池初步 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-11-06 17:11 |只看該作者 |倒序瀏覽
java并發(fā)編程--線程池初步






[coolxing按: 轉(zhuǎn)載請注明作者和出處, 如有謬誤, 歡迎在評論中指正.]



服務器應用程序經(jīng)常需要處理執(zhí)行時間很短而數(shù)目巨大的請求, 如果為每一個請求創(chuàng)建一個新的線程, 會導致一些問題的出現(xiàn), 如:

1. 性能瓶頸. 線程的創(chuàng)建和銷毀需要執(zhí)行大量的后臺操作, 如果單個請求的執(zhí)行時間很短, 有可能花在創(chuàng)建和銷毀線程上的時間大于真正執(zhí)行請求的時間.

2. 可能會導致資源不足. 大量的并發(fā)請求意味著需要創(chuàng)建大量的線程, 過多的線程存在會吞噬大量的系統(tǒng)資源, 而且CPU需要在這些線程間不斷切換, 這可能引發(fā)"切換過度"的問題.

為了適應上述場合, java在JDK1.5中引入了線程池的概念. 線程池中存放著一定數(shù)量的已創(chuàng)建好的線程, 當一個請求到來時, 只需從線程池中取出一個線程來執(zhí)行請求, 請求完成后再將線程歸還給線程池. 同時, 我們可以為線程池指定最大的線程數(shù)量, 當池中所有線程都處于活動狀態(tài)下, 新的任務會排隊等候, 直到之前的某個任務處理完成后, 新的任務才能得到處理.  



創(chuàng)建線程池. java.util.concurrent.Executors類提供了多個靜態(tài)方法用于創(chuàng)建線程池.

|--public static ExecutorService newFixedThreadPool(int nThreads): 創(chuàng)建一個可重用的固定線程數(shù)的線程池. 如果池中所有的nThreads個線程都處于活動狀態(tài)時提交任務(任務通常是Runnable或Callable對象), 任務將在隊列中等待, 直到池中出現(xiàn)可用線程.

|--public static ExecutorService newCachedThreadPool(): 調(diào)用此方法創(chuàng)建的線程池可根據(jù)需要自動調(diào)整池中線程的數(shù)量. 執(zhí)行任務時將重用存在先前創(chuàng)建的線程(如果池中存在可用線程的話). 如果池中沒有可用線程, 將創(chuàng)建一個新的線程, 并將其添加到池中. 池中的線程超過60秒未被使用就會被銷毀, 因此長時間保持空閑的CachedThreadPool不會消耗額外的資源.

|--public static ExecutorService newSingleThreadExecutor(): 創(chuàng)建一個單線程的Executor. 這個Executor保證按照任務提交的順序依次執(zhí)行任務.

|--public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize): 創(chuàng)建一個可重用的固定線程數(shù)的線程池. ScheduledExecutorService是ExecutorService的子接口, 調(diào)用ScheduledExecutorService的相關(guān)方法, 可以延遲或定期執(zhí)行任務.

以上靜態(tài)方法均使用默認的ThreadFactory(即Executors.defaultThreadFactory()方法的返回值)創(chuàng)建線程, 如果想要指定ThreadFactory, 可調(diào)用他們的重載方法.通過指定ThreadFactory, 可以定制新建線程的名稱, 線程組, 優(yōu)先級, 守護線程狀態(tài)等.

如果Executors提供的創(chuàng)建線程池的方法無法滿足要求, 可以使用ThreadPoolExecutor類創(chuàng)建線程池.



提交任務. 所有的線程池都是ExecutorService及其子類的對象, 因此, 可以調(diào)用ExecutorService的相關(guān)方法提交任務.

|--void execute(Runnable command): 使用池中已存在的線程或新建一個線程執(zhí)行command.



Java代碼
  1. 1.public class ExecutorsDemo {   
  2. 2.      
  3. 3.    public static void main(String[] args) throws Exception {   
  4. 4.        System.out.println("----------------FixedThreadPool---------------------");   
  5. 5.        ExecutorService fixedPool = getFixedThreadPool();   
  6. 6.        executeThread(fixedPool);   
  7. 7.           
  8. 8.        // 為了避免混淆, 需要等待executeThread(fixedPool)執(zhí)行完成   
  9. 9.        Thread.sleep(3000);   
  10. 10.           
  11. 11.        System.out.println("----------------CashedThreadPool---------------------");   
  12. 12.        ExecutorService cashedPool = getCashedThreadPool();   
  13. 13.        executeThread(cashedPool);   
  14. 14.           
  15. 15.        // 為了避免混淆, 需要等待executeThread(cashedPool)執(zhí)行完成   
  16. 16.        Thread.sleep(3000);   
  17. 17.           
  18. 18.        System.out.println("----------------SingleThreadExecutor---------------------");   
  19. 19.        ExecutorService singleExecutor = getSingleThreadExecutor();   
  20. 20.        executeThread(singleExecutor);   
  21. 21.           
  22. 22.    }   
  23. 23.      
  24. 24.    // 只存在一個線程的線程池   
  25. 25.    private static ExecutorService getSingleThreadExecutor() {   
  26. 26.        return Executors.newSingleThreadExecutor();   
  27. 27.    }   
  28. 28.  
  29. 29.    // 創(chuàng)建一個根據(jù)需要自動調(diào)整大小的線程池   
  30. 30.    private static ExecutorService getCashedThreadPool() {   
  31. 31.        return Executors.newCachedThreadPool();   
  32. 32.    }   
  33. 33.  
  34. 34.    // 創(chuàng)建一個可重用的固定線程數(shù)的線程池   
  35. 35.    private static ExecutorService getFixedThreadPool() {   
  36. 36.        return Executors.newFixedThreadPool(2);   
  37. 37.    }   
  38. 38.      
  39. 39.    private static void executeThread(ExecutorService pool) {   
  40. 40.        Thread t1 = new MyThread();   
  41. 41.        Thread t2 = new MyThread();   
  42. 42.        Thread t3 = new MyThread();   
  43. 43.        Thread t4 = new MyThread();   
  44. 44.        // 將Tread放入線程池中執(zhí)行   
  45. 45.        // MyThread類繼承自Thread, 而Thread類實現(xiàn)了Runnable接口   
  46. 46.        pool.execute(t1);   
  47. 47.        pool.execute(t2);   
  48. 48.        pool.execute(t3);   
  49. 49.        pool.execute(t4);   
  50. 50.        // 關(guān)閉線程池   
  51. 51.        pool.shutdown();   
  52. 52.    }   
  53. 53.      
  54. 54.    private static final class MyThread extends Thread {   
  55. 55.        @Override  
  56. 56.        public void run() {   
  57. 57.            super.run();   
  58. 58.            System.out.println(Thread.currentThread().getName() + ": is running!");   
  59. 59.        }   
  60. 60.    }   
  61. 61.}  
  62. public class ExecutorsDemo {
  63.        
  64.         public static void main(String[] args) throws Exception {
  65.                 System.out.println("----------------FixedThreadPool---------------------");
  66.                 ExecutorService fixedPool = getFixedThreadPool();
  67.                 executeThread(fixedPool);
  68.                
  69.                 // 為了避免混淆, 需要等待executeThread(fixedPool)執(zhí)行完成
  70.                 Thread.sleep(3000);
  71.                
  72.                 System.out.println("----------------CashedThreadPool---------------------");
  73.                 ExecutorService cashedPool = getCashedThreadPool();
  74.                 executeThread(cashedPool);
  75.                
  76.                 // 為了避免混淆, 需要等待executeThread(cashedPool)執(zhí)行完成
  77.                 Thread.sleep(3000);
  78.                
  79.                 System.out.println("----------------SingleThreadExecutor---------------------");
  80.                 ExecutorService singleExecutor = getSingleThreadExecutor();
  81.                 executeThread(singleExecutor);
  82.                
  83.         }
  84.        
  85.         // 只存在一個線程的線程池
  86.         private static ExecutorService getSingleThreadExecutor() {
  87.                 return Executors.newSingleThreadExecutor();
  88.         }

  89.         // 創(chuàng)建一個根據(jù)需要自動調(diào)整大小的線程池
  90.         private static ExecutorService getCashedThreadPool() {
  91.                 return Executors.newCachedThreadPool();
  92.         }

  93.         // 創(chuàng)建一個可重用的固定線程數(shù)的線程池
  94.         private static ExecutorService getFixedThreadPool() {
  95.                 return Executors.newFixedThreadPool(2);
  96.         }
  97.        
  98.         private static void executeThread(ExecutorService pool) {
  99.                 Thread t1 = new MyThread();
  100.                 Thread t2 = new MyThread();
  101.                 Thread t3 = new MyThread();
  102.                 Thread t4 = new MyThread();
  103.                 // 將Tread放入線程池中執(zhí)行
  104.                 // MyThread類繼承自Thread, 而Thread類實現(xiàn)了Runnable接口
  105.                 pool.execute(t1);
  106.                 pool.execute(t2);
  107.                 pool.execute(t3);
  108.                 pool.execute(t4);
  109.                 // 關(guān)閉線程池
  110.                 pool.shutdown();
  111.         }
  112.        
  113.         private static final class MyThread extends Thread {
  114.                 @Override
  115.                 public void run() {
  116.                         super.run();
  117.                         System.out.println(Thread.currentThread().getName() + ": is running!");
  118.                 }
  119.         }
  120. }
復制代碼
程序的輸出為:

  1. ----------------FixedThreadPool---------------------

  2. pool-1-thread-1: is running!

  3. pool-1-thread-2: is running!

  4. pool-1-thread-2: is running!

  5. pool-1-thread-1: is running!

  6. ----------------CashedThreadPool---------------------

  7. pool-2-thread-1: is running!

  8. pool-2-thread-2: is running!

  9. pool-2-thread-4: is running!

  10. pool-2-thread-3: is running!

  11. ----------------SingleThreadExecutor---------------------

  12. pool-3-thread-1: is running!

  13. pool-3-thread-1: is running!

  14. pool-3-thread-1: is running!

  15. pool-3-thread-1: is running!
復制代碼
|--Future<T> submit(Callable<T> task): 使用池中已存在的線程或新建一個線程執(zhí)行task, 與execute()方法不同的是, 該方法會返回線程的執(zhí)行結(jié)果. submit方法接受一個Callable<T>對象, Callable<T>接口是一個泛型接口, 實現(xiàn)Callable<T>接口需要重寫其中的call()方法, call()方法將返回一個T對象. submit方法的返回值是Future<T>對象, 調(diào)用該對象的get()可以獲得call()方法的返回值.



Java代碼
  1. 1.public class FutureDemo {   
  2. 2.    public static void main(String[] args) throws Exception {   
  3. 3.        ExecutorService pool = Executors.newFixedThreadPool(2);   
  4. 4.           
  5. 5.        Future<Integer> intFuture = pool.submit(new IntegerCallable());   
  6. 6.                // get()方法將阻塞主線程, 直到IntegerCallable線程的call()運行結(jié)束并返回結(jié)果時為止.   
  7. 7.        Integer returnInt = intFuture.get();   
  8. 8.        System.out.println("返回值為" + returnInt);   
  9. 9.           
  10. 10.        Future<Boolean> boolFuture = pool.submit(new BooleanCallable());   
  11. 11.        Boolean returnBool = boolFuture.get();   
  12. 12.        System.out.println("返回值為" + returnBool);   
  13. 13.           
  14. 14.        pool.shutdown();   
  15. 15.    }   
  16. 16.      
  17. 17.    private final static class IntegerCallable implements Callable<Integer> {   
  18. 18.        // call()方法的返回值類型由泛型決定     
  19. 19.        @Override  
  20. 20.        public Integer call() throws Exception {   
  21. 21.            return 2;   
  22. 22.        }   
  23. 23.    }   
  24. 24.      
  25. 25.    private final static class BooleanCallable implements Callable<Boolean> {   
  26. 26.        @Override  
  27. 27.        public Boolean call() throws Exception {   
  28. 28.            return true;   
  29. 29.        }   
  30. 30.    }   
  31. 31.}  
  32. public class FutureDemo {
  33.         public static void main(String[] args) throws Exception {
  34.                 ExecutorService pool = Executors.newFixedThreadPool(2);
  35.                
  36.                 Future<Integer> intFuture = pool.submit(new IntegerCallable());
  37.                 // get()方法將阻塞主線程, 直到IntegerCallable線程的call()運行結(jié)束并返回結(jié)果時為止.
  38.                 Integer returnInt = intFuture.get();
  39.                 System.out.println("返回值為" + returnInt);
  40.                
  41.                 Future<Boolean> boolFuture = pool.submit(new BooleanCallable());
  42.                 Boolean returnBool = boolFuture.get();
  43.                 System.out.println("返回值為" + returnBool);
  44.                
  45.                 pool.shutdown();
  46.         }
  47.        
  48.         private final static class IntegerCallable implements Callable<Integer> {
  49.                 // call()方法的返回值類型由泛型決定  
  50.                 @Override
  51.                 public Integer call() throws Exception {
  52.                         return 2;
  53.                 }
  54.         }
  55.        
  56.         private final static class BooleanCallable implements Callable<Boolean> {
  57.                 @Override
  58.                 public Boolean call() throws Exception {
  59.                         return true;
  60.                 }
  61.         }
  62. }  
復制代碼
程序的輸出結(jié)果為:

返回值為2

返回值為true

|--List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks): 批量執(zhí)行多個任務.



Future類. 如果需要獲取線程的執(zhí)行結(jié)果, 那么就會使用到Future. Future對象是一個指向異步執(zhí)行結(jié)果的引用, 由于線程的異步特性, Future對象在其創(chuàng)建之初可能并不可用, 比如線程的call()方法尚未完成時. 可以調(diào)用Future對象的isDone()方法判斷線程結(jié)果是否已經(jīng)可用, 在線程結(jié)果返回之前調(diào)用Future對象的get()方法, 將導致阻塞.



關(guān)閉線程池. 使用完線程池后需要關(guān)閉它, 否則程序可能一直處于運行狀態(tài). ExecutorService提供了2個方法用于關(guān)閉線程池:

|--void shutdown(): 關(guān)閉線程池, 不再接受新任務. 如果存在正在執(zhí)行的任務, 則等待任務執(zhí)行完成.

|--List<Runnable> shutdownNow(): 關(guān)閉線程池, 不再接受新任務. 盡力嘗試停止正在執(zhí)行的任務, 并返回正在等待的任務列表.

|--boolean isShutdown(): 判斷線程池是否已經(jīng)關(guān)閉.

論壇徽章:
0
2 [報告]
發(fā)表于 2011-11-19 11:12 |只看該作者
IT168文庫精選文檔推薦
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP