生活随笔
收集整理的這篇文章主要介紹了
测试并发应用(三)监控Executor框架
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
聲明:本文是《 Java 7 Concurrency Cookbook 》的第八章, 作者: Javier Fernández González 譯者:鄭玉婷
監(jiān)控Executor框架
Executor 框架提供從線程的創(chuàng)建和管理來分別實(shí)現(xiàn)任務(wù)來執(zhí)行這些任務(wù)的機(jī)制。如果你使用一個執(zhí)行者,你只需要實(shí)現(xiàn) Runnable 對象并把他們發(fā)送給執(zhí)行者。 執(zhí)行者的責(zé)任是管理線程。當(dāng)你發(fā)一個任務(wù)給執(zhí)行者,它會嘗試使用pooled線程來執(zhí)行這個任務(wù),來避免創(chuàng)建新的任務(wù)。此機(jī)制由 Executor 接口提供,它是以 ThreadPoolExecutor 類來實(shí)現(xiàn)的。
在這個指南,你將學(xué)習(xí)從ThreadPoolExecutor執(zhí)行者可以獲取的信息和如何獲取這些信息。
準(zhǔn)備
指南中的例子是使用Eclipse IDE 來實(shí)現(xiàn)的。如果你使用Eclipse 或者其他的IDE,例如NetBeans, 打開并創(chuàng)建一個新的java項(xiàng)目。
怎么做呢…
按照這些步驟來實(shí)現(xiàn)下面的例子::
| 01 | //1. 創(chuàng)建一個類,名為 Task,并實(shí)現(xiàn) Runnable 接口. |
| 02 | public class Task implements Runnable { |
| 04 | //2. 聲明一個私有 long 屬性,名為 milliseconds. |
| 05 | private long milliseconds; |
| 07 | //3. 實(shí)現(xiàn)類的構(gòu)造函數(shù),初始化它的屬性。 |
| 08 | public Task (long milliseconds) { |
| 09 | this.milliseconds=milliseconds; |
| 12 | //4. 實(shí)現(xiàn) run() 方法。通過 milliseconds 屬性讓線程進(jìn)入一段時間休眠。 |
| 15 | System.out.printf("%s: Begin\n",Thread.currentThread(). getName()); |
| 17 | TimeUnit.MILLISECONDS.sleep(milliseconds); |
| 18 | } catch (InterruptedException e) { |
| 21 | System.out.printf("%s: End\n",Thread.currentThread(). getName()); |
| 24 | //5. 創(chuàng)建例子的主類通過創(chuàng)建一個類,名為 Main 并添加 main()方法。 |
| 27 | public static void main(String[] args) throws Exception { |
| 29 | //6. 使用Executors類的newCachedThreadPool()方法創(chuàng)建新的 Executor 對象。 |
| 30 | ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors. newCachedThreadPool(); |
| 32 | //7. 創(chuàng)建并提交10個 Task 對象給執(zhí)行者。用隨機(jī)數(shù)字初始化任務(wù)。 |
| 33 | Random random=new Random(); |
| 34 | for (int i=0; i<10; i++) { |
| 35 | Task task=new Task(random.nextInt(10000)); |
| 39 | //8. 創(chuàng)建迭代為5的for循環(huán)。在每步,傳遞執(zhí)行者調(diào)用 showLog() 方法寫相關(guān)信息,并讓線程休眠1秒。 |
| 40 | for (int i=0; i<5; i++){ |
| 42 | TimeUnit.SECONDS.sleep(1); |
| 45 | //9. 使用 shutdown() 方法關(guān)閉執(zhí)行者。 |
| 48 | //10. 另創(chuàng)建迭代為5的for循環(huán)。在每步,傳遞執(zhí)行者調(diào)用 showLog() 方法寫相關(guān)信息,并讓線程休眠1秒。 |
| 49 | for (int i=0; i<5; i++){ |
| 51 | TimeUnit.SECONDS.sleep(1); |
| 54 | //11. 使用 awaitTermination() 方法等待執(zhí)行者的終結(jié)。 |
| 55 | executor.awaitTermination(1, TimeUnit.DAYS); |
| 57 | //12. 顯示一條結(jié)束程序的信息。 |
| 58 | System.out.printf("Main: End of the program.\n"); |
| 61 | //13. 實(shí)現(xiàn) showLog() 方法,接收 Executor 作為參數(shù)。寫關(guān)于pool的大小,任務(wù)的數(shù)量,和執(zhí)行者狀態(tài)的信息。 |
| 62 | private static void showLog(ThreadPoolExecutor executor) { |
| 63 | System.out.printf("*********************"); |
| 64 | System.out.printf("Main: Executor Log"); |
| 65 | System.out.printf("Main: Executor: Core Pool Size:%d\n",executor.getCorePoolSize()); |
| 66 | System.out.printf("Main: Executor: Pool Size: %d\n",executor. getPoolSize()); |
| 67 | System.out.printf("Main: Executor: Active Count:%d\n",executor.getActiveCount()); |
| 68 | System.out.printf("Main: Executor: Task Count: %d\n",executor. getTaskCount()); |
| 69 | System.out.printf("Main: Executor: Completed Task Count:%d\n",executor.getCompletedTaskCount()); |
| 70 | System.out.printf("Main: Executor: Shutdown: %s\n",executor. isShutdown()); |
| 71 | System.out.printf("Main: Executor: Terminating:%s\n",executor.isTerminating()); |
| 72 | System.out.printf("Main: Executor: Terminated: %s\n",executor. isTerminated()); |
| 73 | System.out.printf("*********************\n"); |
它是如何工作的…
在這個指南里,你實(shí)現(xiàn)了一個任務(wù),它對它的執(zhí)行線程進(jìn)行了一段隨機(jī)毫秒數(shù)的阻塞。然后,你發(fā)送10個任務(wù)給執(zhí)行者,并且當(dāng)你正在等待它們的終結(jié)的同時,你已經(jīng)把關(guān)于執(zhí)行者的狀態(tài)的信息寫入到操控臺。你使用了以下的方法來獲取 Executor 對象的狀態(tài):
- getCorePoolSize(): 此方法返回一個int數(shù),表示線程的核心數(shù)。它是當(dāng)執(zhí)行者沒有執(zhí)行任何任務(wù)時,在內(nèi)部線程池的線程數(shù)。
- getPoolSize(): 此方法返回一個int數(shù),表示內(nèi)部線程池的真實(shí)大小。
- getActiveCount(): 此方法返回一個int數(shù),表示當(dāng)前執(zhí)行任務(wù)的線程數(shù)。
- getTaskCount(): 此方法返回一個long數(shù),表示已經(jīng)分配執(zhí)行的任務(wù)數(shù)。
- getCompletedTaskCount(): 此方法返回一個long數(shù),表示已經(jīng)被這個執(zhí)行者執(zhí)行并結(jié)束執(zhí)行的任務(wù)數(shù)。
- isShutdown(): 當(dāng) 執(zhí)行的 shutdown() 方法被調(diào)用來結(jié)束執(zhí)行時,此方法返回 Boolean 值。
- isTerminating(): 當(dāng)執(zhí)行者正在操作shutdown(),但是還沒結(jié)束時,此方法返回 Boolean 值。
- isTerminated(): 當(dāng)這個執(zhí)行者結(jié)束執(zhí)行時,此方法返回 Boolean 值。
總結(jié)
以上是生活随笔為你收集整理的测试并发应用(三)监控Executor框架的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。