Java多線程三種主要實(shí)現(xiàn)方式解析
多線程三種主要實(shí)現(xiàn)方式:繼承Thread類,實(shí)現(xiàn)Runnable接口、Callable和Futrue。
一、簡(jiǎn)單實(shí)現(xiàn)
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
public class T02_HowToCreateThread {
//1.繼承Thread類
static class MyThread extends Thread{
@Override
public void run() {
System.out.println("MyThread-->");
}
}
//3.實(shí)現(xiàn)Runnable接口
static class MyRun implements Runnable{
@Override
public void run() {
System.out.println("MyRunable-->");
}
}
//4.實(shí)現(xiàn)Callable接口
static class MyCall implements Callable{
@Override
public Object call() throws Exception {
System.out.println("myCallable-->");
return 1;
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
//1.繼承Thread類
new MyThread().start();
//2.lambda與繼承Thread類類//1.繼承Thread類似,最簡(jiǎn)單
new Thread(()->{
System.out.println("lambda-->");
}).start();
//3.實(shí)現(xiàn)Runnable接口
new Thread(new MyRun()).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("simple->Runnable");
}
}).start();
//4.實(shí)現(xiàn)Callable接口,并用包裝器FutureTask來(lái)同時(shí)實(shí)現(xiàn)Runable、Callable兩個(gè)接口,可帶返回結(jié)果
MyCall mycall = new MyCall();
FutureTask futureTask = new FutureTask(mycall);
new Thread(futureTask).start();
System.out.println(futureTask.get());
}
}
二、使用ExecutorService、Callable和Future一起實(shí)現(xiàn)帶返回值
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
/**
* 使用ExecutorsService、Callable、Future來(lái)實(shí)現(xiàn)多個(gè)帶返回值的線程
*/
public class T02_HowToCreateThread02 {
static class MyCallable implements Callable{
private int taskNum;
public MyCallable(int taskNum){
this.taskNum = taskNum;
}
@Override
public Object call() throws Exception {
System.out.println("任務(wù)"+taskNum);
return "MyCallable.call()-->task"+taskNum;
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
int num = 5;
//創(chuàng)建一個(gè)線程池
ExecutorService pool = Executors.newFixedThreadPool(num);
List<Future> futureList = new ArrayList<Future>();
for (int i = 0; i < num; i++){
MyCallable mc = new MyCallable(i);
//執(zhí)行任務(wù),并返回值
Future future = pool.submit(mc);
futureList.add(future);
}
pool.shutdown();
for (Future f: futureList){
System.out.println(f.get());
}
}
}
結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
集合嵌套之ArrayList嵌套ArrayList實(shí)例
下面小編就為大家?guī)?lái)一篇集合嵌套之ArrayList嵌套ArrayList實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Spring Boot配置Thymeleaf(gradle)的簡(jiǎn)單使用
今天小編就為大家分享一篇關(guān)于Spring Boot配置Thymeleaf(gradle)的簡(jiǎn)單使用,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
springboot啟動(dòng)時(shí)如何獲取端口和項(xiàng)目名
這篇文章主要介紹了springboot啟動(dòng)時(shí)如何獲取端口和項(xiàng)目名,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Intellij IDEA中啟動(dòng)多個(gè)微服務(wù)(開(kāi)啟Run Dashboard管理)
這篇文章主要介紹了Intellij IDEA中啟動(dòng)多個(gè)微服務(wù)(開(kāi)啟Run Dashboard管理),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java數(shù)組的動(dòng)態(tài)初始化和常見(jiàn)問(wèn)題解析
本文介紹了數(shù)組動(dòng)態(tài)初始化的概念,即在初始化時(shí)僅指定數(shù)組長(zhǎng)度,系統(tǒng)會(huì)為數(shù)組分配初始值,而靜態(tài)初始化則手動(dòng)指定數(shù)組元素,系統(tǒng)根據(jù)元素個(gè)數(shù)計(jì)算數(shù)組長(zhǎng)度,這兩種初始化方式應(yīng)用場(chǎng)景不同,另外,還講述了數(shù)組默認(rèn)初始化值的規(guī)律及數(shù)組常見(jiàn)問(wèn)題,如越界問(wèn)題等2024-10-10

