分享Java多線程實現(xiàn)的四種方式
以下四種方式:
- 1.繼承Thread類,重寫run方法
- 2.實現(xiàn)Runnable接口,重寫run方法,實現(xiàn)
Runnable接口的實現(xiàn)類的實例對象作為Thread構造函數(shù)的target - 3.通過Callable和FutureTask創(chuàng)建線程
- 4.通過線程池創(chuàng)建線程
后面兩種可以歸結成一類:有返回值,通過Callable接口,就要實現(xiàn)call方法,這個方法的返回值是Object,所以返回的結果可以放在Object對象中。
第一種:繼承Thread類,重寫該類的run()方法。
class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
for (i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 30) {
// 創(chuàng)建一個新的線程 myThread1 此線程進入新建狀態(tài)
Thread myThread1 = new MyThread();
// 創(chuàng)建一個新的線程 myThread2 此線程進入新建狀態(tài)
Thread myThread2 = new MyThread();
// 調用start()方法使得線程進入就緒狀態(tài)
myThread1.start();
// 調用start()方法使得線程進入就緒狀態(tài)
myThread2.start();
}
}
}
}如上所示,繼承Thread類,通過重寫run()方法定義了一個新的線程類MyThread,其中run()方法的方法體代表了線程需要完成的任務,稱之為線程執(zhí)行體。當創(chuàng)建此線程類對象時一個新的線程得以創(chuàng)建,并進入到線程新建狀態(tài)。通過調用線程對象引用的start()方法,使得該線程進入到就緒狀態(tài),此時此線程并不一定會馬上得以執(zhí)行,這取決于CPU調度時機。
第二種:實現(xiàn)Runnable接口,并重寫該接口的run()方法。創(chuàng)建Runnable實現(xiàn)類的實例,并以此實例作為Thread類的target來創(chuàng)建Thread對象,該Thread對象才是真正的線程對象。
class MyRunnable implements Runnable {
private int i = 0;
@Override
public void run() {
for (i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
}
public class ThreadDemo2 {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 30) {
// 創(chuàng)建一個Runnable實現(xiàn)類的對象
Runnable myRunnable = new MyRunnable();
// 將myRunnable作為Thread target創(chuàng)建新的線程
Thread thread1 = new Thread(myRunnable);
Thread thread2 = new Thread(myRunnable);
// 調用start()方法使得線程進入就緒狀態(tài)
thread1.start();
thread2.start();
}
}
}
}第三種:使用Callable和Future接口創(chuàng)建線程。
- 1:創(chuàng)建Callable接口的實現(xiàn)類 ,并實現(xiàn)Call方法
- 2:創(chuàng)建Callable實現(xiàn)類的實現(xiàn),使用FutureTask類包裝Callable對象,該
FutureTask對象封裝了Callable對象的Call方法的返回值 - 3:使用FutureTask對象作為Thread對象的target創(chuàng)建并啟動線程
- 4:調用FutureTask對象的get()來獲取子線程執(zhí)行結束的返回值\
public class ThreadDemo3 {
public static void main(String[] args) {
// 創(chuàng)建MyCallable對象
Callable<Integer> myCallable = new MyCallable();
//使用FutureTask來包裝MyCallable對象
FutureTask<Integer> ft = new FutureTask<Integer>(myCallable);
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 30) {
//FutureTask對象作為Thread對象的target創(chuàng)建新的線程
Thread thread = new Thread(ft);
//線程進入到就緒狀態(tài)
thread.start();
}
}
System.out.println("主線程for循環(huán)執(zhí)行完畢..");
try {
//取得新創(chuàng)建的新線程中的call()方法返回的結果
int sum = ft.get();
System.out.println("sum = " + sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class MyCallable implements Callable<Integer> {
private int i = 0;
// 與run()方法不同的是,call()方法具有返回值
@Override
public Integer call() {
int sum = 0;
for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
sum += i;
}
return sum;
}
}首先,我們發(fā)現(xiàn),在實現(xiàn)Callable接口中,此時不再是run()方法了,而是call()方法,此call()方法作為線程執(zhí)行體,同時還具有返回值!在創(chuàng)建新的線程時,是通過FutureTask來包裝MyCallable對象,同時作為了Thread對象的target。
第四種:通過線程池創(chuàng)建線程。
public class ThreadDemo4{
//線程池數(shù)量
private static int POOL_NUM = 10;
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
ExecutorService executorService = Executors.newFixedThreadPool(5);
for(int i = 0; i<POOL_NUM; i++) {
RunnableThread thread = new RunnableThread();
//Thread.sleep(1000);
executorService.execute(thread);
}
//關閉線程池
executorService.shutdown();
}
}
class RunnableThread implements Runnable {
@Override
public void run() {
System.out.println("通過線程池方式創(chuàng)建的線程:" + Thread.currentThread().getName() + " ");
}
}ExecutorService、Callable都是屬于Executor框架。返回結果的線程是在JDK1.5中引入的新特征,還有Future接口也是屬于這個框架,有了這種特征得到返回值就很方便了。
通過分析可以知道,他同樣也是實現(xiàn)了Callable接口,實現(xiàn)了Call方法,所以有返回值。這也就是正好符合了前面所說的兩種分類
執(zhí)行Callable任務后,可以獲取一個Future的對象,在該對象上調用get就可以獲取到Callable任務返回的Object了。get方法是阻塞的,
即:線程無返回結果,get方法會一直等待。
到此這篇關于分享Java多線程實現(xiàn)的四種方式的文章就介紹到這了,更多相關Java多線程內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring使用RedisTemplate操作Redis數(shù)據(jù)庫
這篇文章主要介紹了spring使用RedisTemplate操作Redis數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
解決springboot配置logback-spring.xml不起作用問題
這篇文章主要介紹了解決springboot配置logback-spring.xml不起作用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

