Java詳解使用線程池處理任務(wù)方法
什么是線程池?
線程池就是一個可以復(fù)用線程的技術(shù)。
不使用線程池的問題:
如果用戶每發(fā)起一個請求,后臺就創(chuàng)建一個新線程來處理,下次新任務(wù)來了又要創(chuàng)建新線程,而創(chuàng)建新線程的開銷是很大的,這樣會嚴重影響系統(tǒng)的性能。
線程池常見面試題:
1、臨時線程什么時候創(chuàng)建?
新任務(wù)提交時發(fā)現(xiàn)核心線程都在忙,任務(wù)隊列也滿了,并且還可以創(chuàng)建臨時線程,此時才會創(chuàng)建臨時線程。
2、什么時候會開始拒絕任務(wù)?
核心線程和臨時線程都在忙,任務(wù)隊列也滿了,新的任務(wù)過來的時候才會開始任務(wù)拒絕。
1、線程池處理Runnable任務(wù)
import java.util.concurrent.*; public class 多線程_5線程池處理Runnable任務(wù) { public static void main(String[] args) { //線程池處理Runnable任務(wù) //創(chuàng)建線程池對象 /* public ThreadPoolExecutor(int corePoolSize,//核心線程數(shù)量 int maximumPoolSize,//線程池可支持的最大線程數(shù)量 long keepAliveTime,//臨時線程的最大存活時間 TimeUnit unit,//指定存活時間的單位(秒,分等) BlockingQueue<Runnable> workQueue,//指定任務(wù)隊列 ThreadFactory threadFactory,//指定用哪個線程工廠創(chuàng)建線程 RejectedExecutionHandler handler)//指定線程忙,任務(wù)滿了的時候,新任務(wù)來了怎么辦 */ ExecutorService pool=new ThreadPoolExecutor(3,5, 6, TimeUnit.SECONDS,new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy()); //給任務(wù)線程池處理 Runnable r=new MyExe(); //三個核心線程 pool.execute(r); pool.execute(r); pool.execute(r); //五個任務(wù)隊列(不創(chuàng)建臨時線程時,會發(fā)現(xiàn)只有三個線程,即核心線程量) pool.execute(r); pool.execute(r); pool.execute(r); pool.execute(r); pool.execute(r); //創(chuàng)建臨時線程(五個線程,即最大線程量) pool.execute(r); pool.execute(r); //不創(chuàng)建,拒絕策略被觸發(fā) // pool.execute(r); //關(guān)閉線程池(開發(fā)中一般不會使用) // pool.shutdownNow();//立即關(guān)閉,即使任務(wù)沒有執(zhí)行完畢。會丟失任務(wù)的! // pool.shutdown();//會等待任務(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+"次"); } //因為當(dāng)前案例任務(wù)太簡單,我們需要創(chuàng)建臨時隊列需要讓三個核心線程忙,五個任務(wù)隊列排滿,所以讓線程休眠以增加任務(wù)時間 try { System.out.println(Thread.currentThread().getName()+"任務(wù)與線程綁定,線程進入了休眠"); 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)建線程池對象 /* public ThreadPoolExecutor(int corePoolSize,//核心線程數(shù)量 int maximumPoolSize,//線程池可支持的最大線程數(shù)量 long keepAliveTime,//臨時線程的最大存活時間 TimeUnit unit,//指定存活時間的單位(秒,分等) BlockingQueue<Runnable> workQueue,//指定任務(wù)隊列 ThreadFactory threadFactory,//指定用哪個線程工廠創(chuàng)建線程 RejectedExecutionHandler handler)//指定線程忙,任務(wù)滿了的時候,新任務(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用SpringBoot+EasyExcel+Vue實現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
這篇文章主要介紹了使用SpringBoot+VUE+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的過程詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08詳解Java數(shù)據(jù)庫連接JDBC基礎(chǔ)知識(操作數(shù)據(jù)庫:增刪改查)
這篇文章主要介紹了詳解Java數(shù)據(jù)庫連接JDBC基礎(chǔ)知識(操作數(shù)據(jù)庫:增刪改查),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01MyBatis動態(tài)SQL如何實現(xiàn)前端指定返回字段
這篇文章主要介紹了MyBatis動態(tài)SQL如何實現(xiàn)前端指定返回字段,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01詳解Java并發(fā)編程之內(nèi)置鎖(synchronized)
這篇文章主要介紹了Java并發(fā)編程之內(nèi)置鎖(synchronized)的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03