使用Executors建立和管理執行緒
轉自: [url]http://blog.csdn.net/liuzhengkang/archive/2008/10/24/3137340.aspx[/url]
1. 類 Executors
此類中提供的一些方法有:
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 的呼叫了,從而允許應用程式使用特殊的執行緒子類、屬性等等。
此介面最簡單的實現就是:
3. 介面ExecutorService
該介面提供了管理終止的方法。
4.建立標準執行緒池啟動執行緒
4.1 提供一個簡單的實現Runnable介面的執行緒
MyThread.java
這個執行緒會列印出相應的建立和執行資訊。
4.2使用CachedThreadPool啟動執行緒
CachedThreadPool.java
結果:
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
結果:
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
結果:
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
5.2 設定優先順序屬性
最高優先順序MaxPriorityThreadFactory.java
最低優先順序MinPriorityThreadFactory.java
5.3啟動帶有屬性設定的執行緒
ExecFromFactory.java
結果:
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)
1. 類 Executors
此類中提供的一些方法有:
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)
相關文章
- Java:使用Executors建立和管理執行緒薦Java執行緒
- 使用 Executors,ThreadPoolExecutor,建立執行緒池,原始碼分析理解thread執行緒原始碼
- Java Executors(執行緒池)Java執行緒
- 如何建立一個執行緒池,為什麼不推薦使用Executors去建立呢?執行緒
- 阿里不允許使用 Executors 建立執行緒池!那怎麼使用,怎麼監控?阿里執行緒
- java多執行緒系列:Executors框架Java執行緒框架
- MFC多執行緒的建立,包括工作執行緒和使用者介面執行緒執行緒
- 執行緒池的建立和使用,執行緒池原始碼初探(篇一)執行緒原始碼
- Java通過Executors提供四種執行緒池Java執行緒
- 多執行緒------執行緒與程式/執行緒排程/建立執行緒執行緒
- 使用CreateThreadPool建立執行緒池thread執行緒
- Java Executors類的9種建立執行緒池的方法及應用場景分析Java執行緒
- 多執行緒--執行緒管理執行緒
- C#多執行緒之旅(2):建立和開始執行緒C#執行緒
- 執行緒的建立及執行緒池執行緒
- java執行緒之守護執行緒和使用者執行緒Java執行緒
- 多執行緒-執行緒組的概述和使用執行緒
- 多執行緒-執行緒池的概述和使用執行緒
- 【搞定面試官】你還在用Executors來建立執行緒池?會有什麼問題呢?面試執行緒
- 執行緒池建立執行緒的過程執行緒
- 認識執行緒、建立執行緒寫法執行緒
- 多執行緒:執行緒池理解和使用總結執行緒
- 執行緒管理執行緒
- 二. 執行緒管理之執行緒池執行緒
- 執行緒和執行緒池執行緒
- Concurrency(二:建立和啟動執行緒)執行緒
- Java多執行緒的建立和啟動Java執行緒
- 使用Loom建立虛擬執行緒 - davidOOM執行緒
- 使用 ThreadPoolExecutor 建立多執行緒工具類thread執行緒
- 我會手動建立執行緒,為什麼讓我使用執行緒池?執行緒
- 【原創】Java多執行緒初學者指南(3):使用Runnable介面建立執行緒Java執行緒
- 自建執行緒管理執行緒
- 【Java基礎】:執行緒的建立和啟動Java執行緒
- Android執行緒管理之ExecutorService執行緒池Android執行緒
- 執行緒的建立方式以及synchronize的使用執行緒
- Java多執行緒學習(1)建立執行緒與執行緒的生命週期Java執行緒
- 多執行緒和多執行緒同步執行緒
- 執行緒和程式基礎以及多執行緒的基本使用(iOS)執行緒iOS