Java併發編程中使用Executors類建立和管理線程的用法html
1.類 Executors
Executors類能夠看作一個「工具類」。援引JDK1.6 API中的介紹:java
此包中所定義的 Executor、ExecutorService、ScheduledExecutorService、ThreadFactory 和 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,以無界隊列方式來運行該線程。閉包
2. 接口 ThreadFactory 併發
根據須要建立新線程的對象。使用線程工廠就無需再手工編寫對 new Thread 的調用了,從而容許應用程序使用特殊的線程子類、屬性等等。此接口最簡單的實現就是:異步
1
2
3
4
5
|
class
SimpleThreadFactory implements ThreadFactory {
public
Thread newThread(Runnable r) {
return
new
Thread(r);
}
}
|
3. 接口ExecutorService工具
該接口提供了管理終止的方法。性能
4.建立標準線程池啓動線程spa
4.1 提供一個簡單的實現Runnable接口的線程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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啓動線程
1
2
3
4
5
6
7
8
9
10
11
|
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();
}
}
|
4.3 使用FixedThreadPool啓動線程
1
2
3
4
5
6
7
8
9
10
11
|
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();
}
}
|
4.4 使用SingleThreadExecutor啓動線程
1
2
3
4
5
6
7
8
9
10
11
|
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();
}
}
|
5.配合ThreadFactory接口的使用
給線程加入daemon和priority的屬性設置 設置後臺線程屬性,設置優先級屬性
1
2
3
4
5
6
7
8
9
10
|
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;
}
}
|
1
2
3
4
5
6
7
8
9
|
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;
}
}
|
1
2
3
4
5
6
7
8
9
10
|
//最低優先級
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啓動帶有屬性設置的線程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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));
}
}
|
參考文章:
1. Java多線程與併發
2. Java 線程池的使用