Java多線程——之一創(chuàng)建線程的四種方法
1.實(shí)現(xiàn)Runnable接口,重載run(),無返回值
package thread;
public class ThreadRunnable implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
package thread;
public class ThreadMain {
public static void main(String[] args) throws Exception {
ThreadRunnable threadRunnable1 = new ThreadRunnable();
ThreadRunnable threadRunnable2 = new ThreadRunnable();
ThreadRunnable threadRunnable3 = new ThreadRunnable();
ThreadRunnable threadRunnable4 = new ThreadRunnable();
Thread thread1 = new Thread(threadRunnable1);
Thread thread2 = new Thread(threadRunnable2);
Thread thread3 = new Thread(threadRunnable3);
Thread thread4 = new Thread(threadRunnable4);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
2.繼承Thread類,復(fù)寫run()
使用時(shí)通過調(diào)用Thread的start()(該方法是native),再調(diào)用創(chuàng)建線程的run(),不同線程的run方法里面的代碼交替執(zhí)行。
不足:由于java為單繼承,若使用線程類已經(jīng)有個(gè)父類,則不能使用該方式創(chuàng)建線程。
public class ThreadEx extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread() + ":" + i);
}
}
}
public class ThreadMain {
public static void main(String[] args)
{
ThreadEx threadEx = new ThreadEx();
threadEx.start();
}
}
3.實(shí)現(xiàn)Callable接口,通過FutureTask/Future來創(chuàng)建有返回值的Thread線程,通過Executor執(zhí)行
補(bǔ)充:與實(shí)現(xiàn)Runnable接口類似,都是實(shí)現(xiàn)接口,不同的是該方式有返回值,可以獲得異步執(zhí)行的結(jié)果。
延伸:FutureTask是類,F(xiàn)uture是接口。
package thread;
import java.util.concurrent.*;
public class ThreadCallable {
public static void main(String[] args) throws Exception {
FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() {
public Integer call() throws Exception {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
return 1;
}
});
Executor executor = Executors.newFixedThreadPool(1);
((ExecutorService) executor).submit(futureTask);
//獲得線程執(zhí)行狀態(tài)
System.out.println(Thread.currentThread().getName() + ":" + futureTask.get());
}
}
4.使用Executors創(chuàng)建ExecutorService,入?yún)allable或Future
補(bǔ)充:適用于線程池和并發(fā)
package thread;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import static java.lang.Thread.sleep;
public class ThreadExecutors {
private final String threadName;
public ThreadExecutors(String threadName) {
this.threadName = threadName;
}
private ThreadFactory createThread() {
ThreadFactory tf = new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread thread = new Thread();
thread.setName(threadName);
thread.setDaemon(true);
try {
sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
return thread;
}
};
return tf;
}
public Object runCallable(Callable callable) {
return Executors.newSingleThreadExecutor(createThread()).submit(callable);
}
public Object runFunture(Runnable runnable) {
return Executors.newSingleThreadExecutor(createThread()).submit(runnable);
}
}
package thread;
import java.util.concurrent.*;
public class ThreadMain {
public static void main(String[] args) throws Exception {
ThreadExecutors threadExecutors = new ThreadExecutors("callableThread");
threadExecutors.runCallable(new Callable() {
public String call() throws Exception {
return "success";
}
});
threadExecutors.runFunture(new Runnable() {
public void run() {
System.out.println("execute runnable thread.");
}
});
}
}
5 Runnable接口和Callable接口區(qū)別
- 1)兩個(gè)接口需要實(shí)現(xiàn)的方法名不一樣,Runnable需要實(shí)現(xiàn)的方法為run(),Callable需要實(shí)現(xiàn)的方法為call()。
- 2)實(shí)現(xiàn)的方法返回值不一樣,Runnable任務(wù)執(zhí)行后無返回值,Callable任務(wù)執(zhí)行后可以得到異步計(jì)算的結(jié)果。
- 3)拋出異常不一樣,Runnable不可以拋出異常,Callable可以拋出異常。
6 Callable返回值意義在哪兒,不要返回值可以嗎,什么時(shí)候需要用到返回值?
首先Callable是線程異步執(zhí)行的結(jié)果狀態(tài),如果有兩個(gè)線程A和B,B中的某個(gè)業(yè)務(wù)邏輯中需要確定A結(jié)束后才能進(jìn)行,那么就需要獲得線程A的執(zhí)行結(jié)果。
設(shè)計(jì)背景:一個(gè)任務(wù)需要進(jìn)行一系列操作,比如拷貝大量的基礎(chǔ)數(shù)據(jù),以及解析數(shù)據(jù),并入庫(kù),由于數(shù)量大,整個(gè)過程需要持續(xù)十秒左右,用戶體驗(yàn)差,需要降低到2~5s。
設(shè)計(jì)思路:經(jīng)過分解過程,將拷貝數(shù)據(jù)分為一個(gè)過程,同時(shí)涵蓋部分解析數(shù)據(jù)功能,剩下解析數(shù)據(jù)劃為一個(gè)過程,兩個(gè)過程異步執(zhí)行,其中最后一個(gè)任務(wù)狀態(tài)入庫(kù)時(shí)需要將所有業(yè)務(wù)操作都執(zhí)行完成后更新,此時(shí)就需要用到線程中的返回值。
以上所述是小編給大家介紹的Java創(chuàng)建線程方法詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring事務(wù)失效的一種原因關(guān)于this調(diào)用的問題
這篇文章主要介紹了Spring事務(wù)失效的一種原因關(guān)于this調(diào)用的問題,本文給大家分享問題原因及解決辦法,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
java通過cglib動(dòng)態(tài)生成實(shí)體bean的操作
這篇文章主要介紹了java通過cglib動(dòng)態(tài)生成實(shí)體bean的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
java導(dǎo)出到excel常用的幾種方式總結(jié)
導(dǎo)出excel是咱Java開發(fā)的必備技能啦,之前項(xiàng)目有這個(gè)功能,現(xiàn)在將其獨(dú)立出來,分享一下,下面這篇文章主要給大家介紹了關(guān)于java導(dǎo)出到excel常用的幾種方式,需要的朋友可以參考下2023-05-05
Linux 下通過 java 命令啟動(dòng) jar 包常見方式小結(jié)
這篇文章主要介紹了Linux 下通過 java 命令啟動(dòng) jar 包常見方式小結(jié),后臺(tái)啟動(dòng)jar包命令大致有五種,每種方式結(jié)合代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2023-12-12
Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)
這篇文章主要介紹了Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Dubbo?retries?超時(shí)重試機(jī)制的問題原因分析及解決方案
這篇文章主要介紹了Dubbo?retries?超時(shí)重試機(jī)制的問題,解決方案是通過修改dubbo服務(wù)提供方,將timeout超時(shí)設(shè)為20000ms或者設(shè)置retries=“0”,禁用超時(shí)重試機(jī)制,感興趣的朋友跟隨小編一起看看吧2022-04-04
java根據(jù)模板動(dòng)態(tài)生成PDF實(shí)例
本篇文章主要介紹了java根據(jù)模板動(dòng)態(tài)生成PDF實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

