Java並發編程中應用Executors類創立和治理線程的用法。本站提示廣大學習愛好者:(Java並發編程中應用Executors類創立和治理線程的用法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java並發編程中應用Executors類創立和治理線程的用法正文
1. 類 Executors
Executors類可以看作一個“對象類”。征引JDK1.6 API中的引見:
此包中所界說的 Executor、ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 類的工場和適用辦法。此類支撐以下各類辦法:
(1)創立並前往設置有經常使用設置裝備擺設字符串的 ExecutorService 的辦法。
(2)創立並前往設置有經常使用設置裝備擺設字符串的 ScheduledExecutorService 的辦法。
(3)創立並前往“包裝的”ExecutorService 辦法,它經由過程使特定於完成的辦法弗成拜訪來禁用從新設置裝備擺設。
(4)創立並前往 ThreadFactory 的辦法,它可將新創立的線程設置為已知的狀況。
(5)創立並前往非閉包情勢的 Callable 的辦法,如許可將其用於須要 Callable 的履行辦法中。
經由過程這個類可以或許取得多種線程池的實例,例如可以挪用newSingleThreadExecutor()取得單線程的ExecutorService,調 用newFixedThreadPool()取得固定年夜小線程池的ExecutorService,等等。拿到ExecutorService可以做的工作就比 較多了,最簡略的是用它來履行Runnable對象,也能夠履行一些完成了Callable<T>的對象。用Thread的start()方 法沒有前往值,假如該線程履行的辦法有前往值那用ExecutorService就再好不外了,可以選擇submit()、invokeAll()或許 invokeAny(),依據詳細情形選擇適合的辦法便可。
此類中供給的一些辦法有:
1.1 public static ExecutorService newCachedThreadPool()
創立一個可依據須要創立新線程的線程池,然則在之前結構的線程可用時將重用它們。關於履行許多短時間異步義務的法式而言,這些線程池平日可進步法式機能。
1.2 public static ExecutorService newFixedThreadPool(int nThreads)
創立一個可重用固定線程數的線程池,以同享的無界隊列方法來運轉這些線程。
1.3 public static ExecutorService newSingleThreadExecutor()
創立一個應用單個 worker 線程的 Executor,以無界隊列方法來運轉該線程。
這三個辦法都可以合營接口ThreadFactory的實例一路應用。而且前往一個ExecutorService接口的實例。
2. 接口 ThreadFactory
依據須要創立新線程的對象。應用線程工場就無需再手工編寫對 new Thread 的挪用了,從而許可運用法式應用特別的線程子類、屬性等等。
此接口最簡略的完成就是:
class SimpleThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { return new Thread(r); } }
3. 接口ExecutorService
該接口供給了治理終止的辦法。
4.創立尺度線程池啟動線程
4.1 供給一個簡略的完成Runnable接口的線程
MyThread.java
package com.zj.concurrency.executors; public class MyThread implements Runnable { private int count = 1, number; public MyThread(int num) { number = num; System.out.println("Create Thread-" + number); } public void run() { while (true) { System.out.println("Thread-" + number + " run " + count+" time(s)"); if (++count == 3) return; } } }
這個線程會打印出響應的創立和履行信息。
4.2應用CachedThreadPool啟動線程
CachedThreadPool.java
package com.zj.concurrency.executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CachedThreadPool { public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) exec.execute(new MyThread(i)); exec.shutdown(); } }
成果:
Create Thread-0 Create Thread-1 Create Thread-2 Create Thread-3 Thread-0 run 1 time(s) Thread-0 run 2 time(s) Thread-1 run 1 time(s) Thread-1 run 2 time(s) Thread-2 run 1 time(s) Thread-2 run 2 time(s) Create Thread-4 Thread-4 run 1 time(s) Thread-4 run 2 time(s) Thread-3 run 1 time(s) Thread-3 run 2 time(s)
4.3 應用FixedThreadPool啟動線程
FixedThreadPool.java package com.zj.concurrency.executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class FixedThreadPool { public static void main(String[] args) { ExecutorService exec = Executors.newFixedThreadPool(2); for (int i = 0; i < 5; i++) exec.execute(new MyThread(i)); exec.shutdown(); } }
成果:
Create Thread-0 Create Thread-1 Create Thread-2 Create Thread-3 Create Thread-4 Thread-0 run 1 time(s) Thread-0 run 2 time(s) Thread-2 run 1 time(s) Thread-2 run 2 time(s) Thread-3 run 1 time(s) Thread-3 run 2 time(s) Thread-4 run 1 time(s) Thread-4 run 2 time(s) Thread-1 run 1 time(s) Thread-1 run 2 time(s)
4.4 應用SingleThreadExecutor啟動線程
SingleThreadExecutor.java
package com.zj.concurrency.executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SingleThreadExecutor { public static void main(String[] args) { ExecutorService exec = Executors.newSingleThreadExecutor(); for (int i = 0; i < 5; i++) exec.execute(new MyThread(i)); exec.shutdown(); } }
成果:
Create Thread-0 Create Thread-1 Create Thread-2 Create Thread-3 Create Thread-4 Thread-0 run 1 time(s) Thread-0 run 2 time(s) Thread-1 run 1 time(s) Thread-1 run 2 time(s) Thread-2 run 1 time(s) Thread-2 run 2 time(s) Thread-3 run 1 time(s) Thread-3 run 2 time(s) Thread-4 run 1 time(s) Thread-4 run 2 time(s)
5.合營ThreadFactory接口的應用
我們試圖給線程參加daemon和priority的屬性設置。
5.1設置後台線程屬性
DaemonThreadFactory.java
package com.zj.concurrency.executors.factory; import java.util.concurrent.ThreadFactory; public class DaemonThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setDaemon(true); return t; } }
5.2 設置優先級屬性
最高優先級MaxPriorityThreadFactory.java
package com.zj.concurrency.executors.factory; import java.util.concurrent.ThreadFactory; public class MaxPriorityThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setPriority(Thread.MAX_PRIORITY); return t; } }
最低優先級MinPriorityThreadFactory.java
package com.zj.concurrency.executors.factory; import java.util.concurrent.ThreadFactory; public class MinPriorityThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setPriority(Thread.MIN_PRIORITY); return t; } }
5.3啟動帶有屬性設置的線程
ExecFromFactory.java
package com.zj.concurrency.executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.zj.concurrency.executors.factory.DaemonThreadFactory; import com.zj.concurrency.executors.factory.MaxPriorityThreadFactory; import com.zj.concurrency.executors.factory.MinPriorityThreadFactory; public class ExecFromFactory { public static void main(String[] args) throws Exception { ExecutorService defaultExec = Executors.newCachedThreadPool(); ExecutorService daemonExec = Executors .newCachedThreadPool(new DaemonThreadFactory()); ExecutorService maxPriorityExec = Executors .newCachedThreadPool(new MaxPriorityThreadFactory()); ExecutorService minPriorityExec = Executors .newCachedThreadPool(new MinPriorityThreadFactory()); for (int i = 0; i < 10; i++) daemonExec.execute(new MyThread(i)); for (int i = 10; i < 20; i++) if (i == 10) maxPriorityExec.execute(new MyThread(i)); else if (i == 11) minPriorityExec.execute(new MyThread(i)); else defaultExec.execute(new MyThread(i)); } }
成果:
Create Thread-0 Create Thread-1 Create Thread-2 Create Thread-3 Thread-0 run 1 time(s) Thread-0 run 2 time(s) Thread-1 run 1 time(s) Thread-1 run 2 time(s) Thread-2 run 1 time(s) Thread-2 run 2 time(s) Create Thread-4 Thread-4 run 1 time(s) Thread-4 run 2 time(s) Create Thread-5 Thread-5 run 1 time(s) Thread-5 run 2 time(s) Create Thread-6 Create Thread-7 Thread-7 run 1 time(s) Thread-7 run 2 time(s) Create Thread-8 Thread-8 run 1 time(s) Thread-8 run 2 time(s) Create Thread-9 Create Thread-10 Thread-10 run 1 time(s) Thread-10 run 2 time(s) Create Thread-11 Thread-9 run 1 time(s) Thread-9 run 2 time(s) Thread-6 run 1 time(s) Thread-6 run 2 time(s) Thread-3 run 1 time(s) Thread-3 run 2 time(s) Create Thread-12 Create Thread-13 Create Thread-14 Thread-12 run 1 time(s) Thread-12 run 2 time(s) Thread-13 run 1 time(s) Thread-13 run 2 time(s) Create Thread-15 Thread-15 run 1 time(s) Thread-15 run 2 time(s) Create Thread-16 Thread-16 run 1 time(s) Thread-16 run 2 time(s) Create Thread-17 Create Thread-18 Create Thread-19 Thread-14 run 1 time(s) Thread-14 run 2 time(s) Thread-17 run 1 time(s) Thread-17 run 2 time(s) Thread-18 run 1 time(s) Thread-18 run 2 time(s) Thread-19 run 1 time(s) Thread-19 run 2 time(s) Thread-11 run 1 time(s) Thread-11 run 2 time(s)