Java如何固定大小的線程池
1.固定大小的線程池簡(jiǎn)介
線程池就是在程序啟動(dòng)的時(shí)候先建立幾個(gè)可以使用的線程放在那里,然后等著具體的任務(wù)放進(jìn)去,這個(gè)任務(wù)基本可以說(shuō)都是Runnable的實(shí)現(xiàn)類,因此它減小了系統(tǒng)每次新建和銷毀線程的開(kāi)銷,但同時(shí)增加了維護(hù)這些線程的開(kāi)銷,個(gè)中取舍看具體情況而定。
固定大小的線程池就是在啟動(dòng)的時(shí)候創(chuàng)建了固定個(gè)數(shù)的線程放在那里等待使用。
2.包裝一個(gè)線程池對(duì)象
public class TaskPool{
private final ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(9); // 創(chuàng)建一個(gè)大小為9的固定線程池,可以按照CPU的核數(shù)初步判定,如果CPU密集性任務(wù)則創(chuàng)建N+1個(gè),如果是IO密集型任務(wù)則創(chuàng)建2N+1個(gè),其中N即CPU的核數(shù)
protected void shutdown(){
// do something
// 這個(gè)方法等待線程池中所有已提交任務(wù)執(zhí)行結(jié)束,不接收新任務(wù),然后結(jié)束
executor.shutdown();
// 這個(gè)強(qiáng)制結(jié)束所有任務(wù),然后正在等在的任務(wù)列表
// executor.shutdownNow();
}
protected void execute(Runnable command){
// do something
// 提交任務(wù)
executor.execute(command);
}
public void status(){
StringBuffer sb = new StringBuffer();
// 當(dāng)前正在執(zhí)行任務(wù)的線程數(shù)
sb.append(executor.getActiveCount() + "\n");
// 當(dāng)前正在等待執(zhí)行的線程數(shù)
sb.append(executor.getQueue().size() + "\n");
// 返回已經(jīng)完成的線程數(shù)
sb.append(executor.getCompletedTaskCount() + "\n");
System.out.println(sb.toString());
// 注:以上方法都是返回一個(gè)大概值,因?yàn)榫€程在執(zhí)行中,這些狀態(tài)隨時(shí)都會(huì)改變
}
}
3.使用線程池
public class Launcher{
private TaskPool taskPool = new TaskPool();
public static void main(String[] args){
// 新建100個(gè)任務(wù),Runnable的實(shí)現(xiàn)類Task
Task[] tasks = new Task[100];
for (int i = 0; i < tasks.length; i++){
tasks[i] = new Task("Task " + (i+1));
// 提交到線程池運(yùn)行
taskPool.execute(task[i]);
if ( i % 50 == 0){
taskPool.status();
}
}
private static class Task implements Runnable{
private String name;
public Task(String name){
this.name = name;
}
public void run(){
// do something
System.out.println("我的名字是:" + this.name);
}
}
}
Java線程池小拓展
線程池的介紹
1 常用的 池化技術(shù)
C3P0
DBCP
2 線程池的衍生
頻繁的創(chuàng)建線程對(duì)象和多線程之間進(jìn)行上下文切換,是非常耗時(shí)間和資源的所以JDK1.5中提出了線程池技術(shù)
3 使用線程池
Exector
4 線程池的創(chuàng)建
創(chuàng)建一個(gè)固定大小的線程池 ( 最常用的方法 )
ExecutorService pool = Executors.newFixedThreadPool(2);
Runnable task = new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
};
pool.execute(task);
pool.execute(task);
pool.execute(task);//線程池的帶下只有兩個(gè) 現(xiàn)在這個(gè)任務(wù)在其等待隊(duì)列中排隊(duì)等候
創(chuàng)建可變大小的線程池
ExecutorService pool = Executors.newCachedThreadPool();
Runnable task = new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
};
pool.execute(task);
pool.execute(task);
pool.execute(task);
創(chuàng)建獨(dú)立任務(wù)的線程池
ExecutorService pool = Executors.newSingleThreadExecutor();
Runnable task = new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
};
pool.execute(task);
pool.execute(task);
pool.execute(task);
創(chuàng)建可調(diào)度的線程池
ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
Runnable task = new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
};
threadPool.schedule(task, 2000, TimeUnit.MILLISECONDS);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Eclipse添加xml文件提示及Hibernate配置學(xué)習(xí)
文件提示功能在開(kāi)發(fā)過(guò)程中很實(shí)用的,本文實(shí)現(xiàn)了一個(gè)Eclipse添加xml文件提示,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01
Spring聲明式事務(wù)注解之@EnableTransactionManagement解析
這篇文章主要介紹了Spring聲明式事務(wù)注解之@EnableTransactionManagement解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Mybatis分頁(yè)插件PageHelper手寫(xiě)實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Mybatis分頁(yè)插件PageHelper手寫(xiě)實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
詳解SpringBoot中@ConditionalOnClass注解的使用
這篇文章主要和大家詳細(xì)介紹一下springboot中@ConditionalOnClass注解的用法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-08-08
springboot?serviceImpl初始化注入對(duì)象實(shí)現(xiàn)方式
這篇文章主要介紹了springboot?serviceImpl初始化注入對(duì)象實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Java簡(jiǎn)易抽獎(jiǎng)系統(tǒng)小項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易抽獎(jiǎng)系統(tǒng)小項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
spring使用ehcache實(shí)現(xiàn)頁(yè)面緩存示例
這篇文章主要介紹了spring使用ehcache實(shí)現(xiàn)頁(yè)面緩存示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
在java List中進(jìn)行模糊查詢的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇在java List中進(jìn)行模糊查詢的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11

