Java詳解使用線程池處理任務(wù)方法
什么是線程池?
線程池就是一個(gè)可以復(fù)用線程的技術(shù)。
不使用線程池的問(wèn)題:
如果用戶每發(fā)起一個(gè)請(qǐng)求,后臺(tái)就創(chuàng)建一個(gè)新線程來(lái)處理,下次新任務(wù)來(lái)了又要?jiǎng)?chuàng)建新線程,而創(chuàng)建新線程的開(kāi)銷是很大的,這樣會(huì)嚴(yán)重影響系統(tǒng)的性能。

線程池常見(jiàn)面試題:
1、臨時(shí)線程什么時(shí)候創(chuàng)建?
新任務(wù)提交時(shí)發(fā)現(xiàn)核心線程都在忙,任務(wù)隊(duì)列也滿了,并且還可以創(chuàng)建臨時(shí)線程,此時(shí)才會(huì)創(chuàng)建臨時(shí)線程。
2、什么時(shí)候會(huì)開(kāi)始拒絕任務(wù)?
核心線程和臨時(shí)線程都在忙,任務(wù)隊(duì)列也滿了,新的任務(wù)過(guò)來(lái)的時(shí)候才會(huì)開(kāi)始任務(wù)拒絕。
1、線程池處理Runnable任務(wù)
import java.util.concurrent.*;
public class 多線程_5線程池處理Runnable任務(wù) {
public static void main(String[] args) {
//線程池處理Runnable任務(wù)
//創(chuàng)建線程池對(duì)象
/*
public ThreadPoolExecutor(int corePoolSize,//核心線程數(shù)量
int maximumPoolSize,//線程池可支持的最大線程數(shù)量
long keepAliveTime,//臨時(shí)線程的最大存活時(shí)間
TimeUnit unit,//指定存活時(shí)間的單位(秒,分等)
BlockingQueue<Runnable> workQueue,//指定任務(wù)隊(duì)列
ThreadFactory threadFactory,//指定用哪個(gè)線程工廠創(chuàng)建線程
RejectedExecutionHandler handler)//指定線程忙,任務(wù)滿了的時(shí)候,新任務(wù)來(lái)了怎么辦
*/
ExecutorService pool=new ThreadPoolExecutor(3,5,
6, TimeUnit.SECONDS,new ArrayBlockingQueue<>(5),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
//給任務(wù)線程池處理
Runnable r=new MyExe();
//三個(gè)核心線程
pool.execute(r);
pool.execute(r);
pool.execute(r);
//五個(gè)任務(wù)隊(duì)列(不創(chuàng)建臨時(shí)線程時(shí),會(huì)發(fā)現(xiàn)只有三個(gè)線程,即核心線程量)
pool.execute(r);
pool.execute(r);
pool.execute(r);
pool.execute(r);
pool.execute(r);
//創(chuàng)建臨時(shí)線程(五個(gè)線程,即最大線程量)
pool.execute(r);
pool.execute(r);
//不創(chuàng)建,拒絕策略被觸發(fā)
// pool.execute(r);
//關(guān)閉線程池(開(kāi)發(fā)中一般不會(huì)使用)
// pool.shutdownNow();//立即關(guān)閉,即使任務(wù)沒(méi)有執(zhí)行完畢。會(huì)丟失任務(wù)的!
// pool.shutdown();//會(huì)等待任務(wù)全部執(zhí)行完畢后再關(guān)閉(建議使用)
}
}
class MyExe implements Runnable{
public void run(){
for (int i = 1; i <=6 ; i++) {
System.out.println(Thread.currentThread().getName()+"正在執(zhí)行:"+i+"次");
}
//因?yàn)楫?dāng)前案例任務(wù)太簡(jiǎn)單,我們需要?jiǎng)?chuàng)建臨時(shí)隊(duì)列需要讓三個(gè)核心線程忙,五個(gè)任務(wù)隊(duì)列排滿,所以讓線程休眠以增加任務(wù)時(shí)間
try {
System.out.println(Thread.currentThread().getName()+"任務(wù)與線程綁定,線程進(jìn)入了休眠");
Thread.sleep(1000000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

2、線程池處理Callable任務(wù)
import java.util.concurrent.*;
public class 多線程_5線程池處理Callable任務(wù) {
public static void main(String[] args) throws Exception {
//線程池處理Callable任務(wù)
//創(chuàng)建線程池對(duì)象
/*
public ThreadPoolExecutor(int corePoolSize,//核心線程數(shù)量
int maximumPoolSize,//線程池可支持的最大線程數(shù)量
long keepAliveTime,//臨時(shí)線程的最大存活時(shí)間
TimeUnit unit,//指定存活時(shí)間的單位(秒,分等)
BlockingQueue<Runnable> workQueue,//指定任務(wù)隊(duì)列
ThreadFactory threadFactory,//指定用哪個(gè)線程工廠創(chuàng)建線程
RejectedExecutionHandler handler)//指定線程忙,任務(wù)滿了的時(shí)候,新任務(wù)來(lái)了怎么辦
*/
ExecutorService pool=new ThreadPoolExecutor(3,5,
6, TimeUnit.SECONDS,new ArrayBlockingQueue<>(5),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
//給任務(wù)線程池處理
// Callable c=new MyCallable2(100);
// pool.submit(c);
Future<String> f1=pool.submit(new MyCallable2(100));
Future<String> f2=pool.submit(new MyCallable2(200));
Future<String> f3=pool.submit(new MyCallable2(300));
Future<String> f4=pool.submit(new MyCallable2(400));
Future<String> f5=pool.submit(new MyCallable2(500));
// String str=f1.get();
// System.out.println(str);
System.out.println(f1.get());
System.out.println(f2.get());
System.out.println(f3.get());
System.out.println(f4.get());
System.out.println(f5.get());
}
}
class MyCallable2 implements Callable<String> {
// v(泛型)
private int n;
public MyCallable2(int n) {
this.n = n;
}
//重寫call方法
//案例:加法
public String call() throws Exception {
int sum = 0;
for (int i = 1; i <=n; i++) {
sum += i;
}
return Thread.currentThread().getName()+"執(zhí)行 1-"+n+"的和,結(jié)果為:" + sum;
}
}
到此這篇關(guān)于Java詳解使用線程池處理任務(wù)方法的文章就介紹到這了,更多相關(guān)Java線程池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析阿里GTS開(kāi)源版本fescar分布式事務(wù)
這篇文章主要為大家介紹解析阿里GTS開(kāi)源版本fescar分布式事務(wù)的原理及使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多進(jìn)步2022-02-02
使用SpringBoot+EasyExcel+Vue實(shí)現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
這篇文章主要介紹了使用SpringBoot+VUE+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的過(guò)程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
詳解Java數(shù)據(jù)庫(kù)連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫(kù):增刪改查)
這篇文章主要介紹了詳解Java數(shù)據(jù)庫(kù)連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫(kù):增刪改查),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
MyBatis動(dòng)態(tài)SQL如何實(shí)現(xiàn)前端指定返回字段
這篇文章主要介紹了MyBatis動(dòng)態(tài)SQL如何實(shí)現(xiàn)前端指定返回字段,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
詳解MybatisPlus3.4版本之后分頁(yè)插件的使用
從Mybatis Plus 3.4.0版本開(kāi)始,不再使用舊版本的PaginationInterceptor ,而是使用MybatisPlusInterceptor。本文就詳細(xì)的介紹一下兩者的區(qū)別,感興趣的可以了解一下2021-11-11
詳解Java并發(fā)編程之內(nèi)置鎖(synchronized)
這篇文章主要介紹了Java并發(fā)編程之內(nèi)置鎖(synchronized)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

