Java詳解使用線程池處理任務(wù)方法
什么是線程池?
線程池就是一個(gè)可以復(fù)用線程的技術(shù)。
不使用線程池的問題:
如果用戶每發(fā)起一個(gè)請(qǐng)求,后臺(tái)就創(chuàng)建一個(gè)新線程來處理,下次新任務(wù)來了又要?jiǎng)?chuàng)建新線程,而創(chuàng)建新線程的開銷是很大的,這樣會(huì)嚴(yán)重影響系統(tǒng)的性能。
線程池常見面試題:
1、臨時(shí)線程什么時(shí)候創(chuàng)建?
新任務(wù)提交時(shí)發(fā)現(xiàn)核心線程都在忙,任務(wù)隊(duì)列也滿了,并且還可以創(chuàng)建臨時(shí)線程,此時(shí)才會(huì)創(chuàng)建臨時(shí)線程。
2、什么時(shí)候會(huì)開始拒絕任務(wù)?
核心線程和臨時(shí)線程都在忙,任務(wù)隊(duì)列也滿了,新的任務(wù)過來的時(shí)候才會(huì)開始任務(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ù)來了怎么辦 */ 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)閉線程池(開發(fā)中一般不會(huì)使用) // pool.shutdownNow();//立即關(guān)閉,即使任務(wù)沒有執(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ǎ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ù)來了怎么辦 */ 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)文章
使用SpringBoot+EasyExcel+Vue實(shí)現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
這篇文章主要介紹了使用SpringBoot+VUE+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的過程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08詳解Java數(shù)據(jù)庫連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫:增刪改查)
這篇文章主要介紹了詳解Java數(shù)據(jù)庫連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫:增刪改查),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01MyBatis動(dòng)態(tài)SQL如何實(shí)現(xiàn)前端指定返回字段
這篇文章主要介紹了MyBatis動(dòng)態(tài)SQL如何實(shí)現(xiàn)前端指定返回字段,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01詳解Java并發(fā)編程之內(nèi)置鎖(synchronized)
這篇文章主要介紹了Java并發(fā)編程之內(nèi)置鎖(synchronized)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03