轉自:https://www.e-learn.cn/content/java/653552
一、線程創建
JAVA創建線程的方式有三種,分別是:
- 繼承Thread
- 實現Runnable
- 實現Callable
1、繼承Thread
通過繼承抽象類Thread,創建MyThreadExtends對象,調用其start方法。
package Thread;
import java.util.concurrent.*;
public class TestThread {
public static void main(String[] args) throws Exception {
testExtends();
}
public static void testExtends() throws Exception {
Thread t1 = new MyThreadExtends();
Thread t2 = new MyThreadExtends();
t1.start();
t2.start();
}
}
class MyThreadExtends extends Thread {
@Override
public void run() {
System.out.println("通過繼承Thread,線程號:" + currentThread().getName());
}
}
2、實現Runnable
通過實現接口Runnable,創建Runnable對象r,然後將r作爲參數創建Thread對象t,最後調用t的start方法。
package Thread;
import java.util.concurrent.*;
public class TestThread {
public static void main(String[] args) throws Exception {
testImplents();
}
public static void testImplents() throws Exception {
MyThreadImplements myThreadImplements = new MyThreadImplements();
Thread t1 = new Thread(myThreadImplements);
Thread t2 = new Thread(myThreadImplements, "my thread -2");
t1.start();
t2.start();
}
}
class MyThreadImplements implements Runnable {
@Override
public void run() {
System.out.println("通過實現Runable,線程號:" + Thread.currentThread().getName());
}
}
3、實現Callable
通過實現接口Callable ,創建Callable 對象c,然後以c爲參數創建FutureTask 對象f,再以f爲參數創建Thread對象t,調用t的start方法。此方法能通過FutureTask 對象f的get方法接收返回值。
package Thread;
import java.util.concurrent.*;
public class TestThread {
public static void main(String[] args) throws Exception {
testCallable();
}
public static void testCallable() throws Exception {
Callable callable = new MyThreadCallable();
FutureTask task = new FutureTask(callable);
new Thread(task).start();
System.out.println(task.get());
Thread.sleep(10);//等待線程執行結束
//task.get() 獲取call()的返回值。若調用時call()方法未返回,則阻塞線程等待返回值
//get的傳入參數爲等待時間,超時拋出超時異常;傳入參數爲空時,則不設超時,一直等待
System.out.println(task.get(100L, TimeUnit.MILLISECONDS));
}
}
class MyThreadCallable implements Callable {
@Override
public Object call() throws Exception {
System.out.println("通過實現Callable,線程號:" + Thread.currentThread().getName());
return 10;
}
}
4、三者對比
- 繼承Thread使用繼承方式,由於JAVA中使用單繼承方式,故對編碼侷限性較高;其餘兩種方式其實最後都是創建了Thread對象。
- Runable使用實現接口方式,屬於常用方式。
- Callable能接收返回值,不過實現相比Runable較爲繁瑣,再不關注返回值的情況下不使用。
二、線程池
在實際項目使用中,多線程都與線程池同時出現,故在此說明線程池的創建(有很多博文認爲線程池是實現多線程的一種方式,我並不認可。我認爲線程池只是創建線程的一種方式,並不是實現)。
在這裏介紹的是使用java.util.concurrent包下的Executors創建線程池。
1、使用代碼示例
public static void testExecutor() throws Exception {
//創建單個線程的線程池(核心線程數與最大線程數都爲1)
ExecutorService executor1 = Executors.newSingleThreadExecutor();
//創建定長線程池(核心線程數與最大線程數一樣)
ExecutorService executor2 = Executors.newFixedThreadPool(4);
//創建定長線程池(定核心線程數,最大線程數爲int的最大值)
ExecutorService executor3 = Executors.newScheduledThreadPool(4);
//創建無限長線程池(核心線程數爲0,最大線程數爲int最大值)
ExecutorService executor4 = Executors.newCachedThreadPool();
//創建線程池(所有參數自定義)
ExecutorService executor5 = new ThreadPoolExecutor(4, 20, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
MyThreadExecutor myThreadImplements = new MyThreadExecutor();
MyThreadCallable myThreadCallable = new MyThreadCallable();
for (int i = 0; i < 4; i++) {
executor2.execute(myThreadImplements);
Future futureTask = executor1.submit(myThreadCallable);
}
}
2、創建線程池方法詳解
根據上述得知,Executors提供了五種實現方式,其作用如上,我們接下來看其源碼。
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
//ScheduledThreadPoolExecutor繼承ThreadPoolExecutor
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,new DelayedWorkQueue());
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,
TimeUnit unit,BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,
TimeUnit unit,BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||maximumPoolSize <= 0 ||maximumPoolSize < corePoolSize ||keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?null :AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
通過關注以上源碼,最終發現最後都是調用的ThreadPoolExecutor的 public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize, long keepAliveTime, TimeUnit unit,BlockingQueue workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler)構造器,故重點關注此構造器。此構造器有7個參數,每個參數含義如下:
- corePoolSize 線程池核心線程數量。當新任務在方法 execute(java.lang.Runnable) 中提交時,如果運行的線程少於 corePoolSize,則創建新線程來處理請求,即使其他輔助線程是空閒的。
- maximumPoolSize 線程池最大線程數量。如果運行的線程多於 corePoolSize 而少於 maximumPoolSize,則僅當隊列滿時才創建新線程。
- keepAliveTime 當線程數大於corePoolSize 時,則這些多出的線程在空閒時間超過 keepAliveTime 時將會終止。
- unit 參數keepAliveTime 的時間單位。
- workQueue 執行前用於保持任務的隊列。此隊列僅保持由 execute 方法提交的 Runnable 任務。
A.隊列保存策略:若運行的線程少於 corePoolSize,則 Executor 始終首選添加新的線程,而不進行排隊;若運行的線程等於或多於 corePoolSize,則 Executor 始終首選將請求加入隊列,而不添加新的線程;若無法將請求加入隊列,則創建新的線程,除非創建此線程超出 maximumPoolSize,在這種情況下,任務將被拒絕。
B.隊列選取通常策略:
a.直接提交:直接提交隊列(如SynchronousQueue),此種隊列將任務直接提交給線程而不保存他們,如果不存在可用於立即運行任務的線程,則試圖把任務加入隊列將失敗,因此會構造一個新的線程。此策略可以避免在處理可能具有內部依賴性的請求集時出現鎖。直接提交通常要求無界 maximumPoolSizes 以避免拒絕新提交的任務。當命令以超過隊列所能處理的平均數連續到達時,此策略允許無界線程具有增長的可能性。如newCachedThreadPool
b.無界隊列。使用無界隊列(如 LinkedBlockingQueue)將導致在所有 corePoolSize 線程都忙時新任務在隊列中等待。這樣,創建的線程就不會超過 corePoolSize(因此,maximumPoolSize 的值也就無效了)。當每個任務完全獨立於其他任務,即任務執行互不影響時,適合於使用無界隊列;例如,在 Web 頁服務器中。這種排隊可用於處理瞬態突發請求,當命令以超過隊列所能處理的平均數連續到達時,此策略允許無界線程具有增長的可能性。如newFixedThreadPool和newSingleThreadExecutor
c.有界隊列。當使用有限的 maximumPoolSizes 時,有界隊列(如 ArrayBlockingQueue)有助於防止資源耗盡,但是可能較難調整和控制。隊列大小和最大池大小可能需要相互折衷:使用大型隊列和小型池可以最大限度地降低 CPU 使用率、操作系統資源和上下文切換開銷,但是可能導致人工降低吞吐量。如果任務頻繁阻塞(例如,如果它們是 I/O 邊界),則系統可能爲超過您許可的更多線程安排時間。使用小型隊列通常要求較大的池大小,CPU 使用率較高,但是可能遇到不可接受的調度開銷,這樣也會降低吞吐量。 - threadFactory 執行程序創建新線程時使用的工廠。若參數爲空,則在同一個 ThreadGroup 中使用 Executors.defaultThreadFactory() 創建線程,並且這些線程具有相同的 NORM_PRIORITY 優先級和非守護進程狀態。
- handler 由於超出線程範圍和隊列容量而使執行被阻塞時所使用的處理程序。一般不會自定義,而使用默認。
3、創建線程池方法對比
通過上述描述,對創建線程池方法進行對比分析:
- 若自身對性能有很大需求,且對於機器性能、代碼能力等有足夠自信,使用ThreadPoolExecutor的構造方法是最合適的。
- newSingleThreadExecutor()是構造只有一個線程的線程池,保存任務的隊列是無界的,可接收所有任務,但是同時只有一個線程執行任務
- newFixedThreadPool()是構造可重用固定線程數的線程池,以共享的無界隊列方式來運行這些線程,在需要時使用提供的 ThreadFactory 創建新線程。
- newScheduledThreadPool()創建一個可重用線程池(最大線程數爲int最大值),它可安排在給定延遲後運行命令或者定期地執行(因爲使用DelayedWorkQueue()隊列)。
- newCachedThreadPool()是構造一個可根據需要創建新線程的線程池(最大線程數爲int最大值),但是在以前構造的線程可用時將重用它們。調用 execute 將重用以前構造的線程(如果線程可用)。如果現有線程沒有可用的,則創建一個新線程並添加到池中。
4、線程池提交線程方法
executor2.execute(myThreadImplements);
Future futureTask = executor1.submit(myThreadCallable);
ScheduledFuture scheduledFuture = executor3.schedule(myThreadImplements, 100L, TimeUnit.SECONDS);
如上,提交方式有execute,submit,schedule三種(不是所有線程都可用此三種):
- execute屬於ExecutorService的父接口Executor的方法,所有線程池都具有此方法。
- submit屬於ExecutorService的方法。具有返回值Future ,當內部線程使用實現Callable方式是現實時(具有返回值),可以接收返回值。上述所有線程池都具有此方法。
- schedule屬於ExecutorService的子孫類ScheduledThreadPoolExecutor方法。具有返回值Future ,當內部線程使用實現Callable方式是現實時(具有返回值),可以接收返回值。能實現定時任務和延遲任務。上述線程池中只有newScheduledThreadPool創建的線程池具有此方法。