如何用java設(shè)計(jì)一個(gè)線程池?相信很多新手小白還沒(méi)學(xué)會(huì)這個(gè)技能,通過(guò)這篇文章的總結(jié),希望你能學(xué)會(huì)用java設(shè)計(jì)線程池。如下資料是用java設(shè)計(jì)線程池的步驟。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供天壇街道網(wǎng)站建設(shè)、天壇街道做網(wǎng)站、天壇街道網(wǎng)站設(shè)計(jì)、天壇街道網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、天壇街道企業(yè)網(wǎng)站模板建站服務(wù),10多年天壇街道做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
思路與生產(chǎn)者與消費(fèi)者模式相同,將任務(wù)放到隊(duì)列中,子線程再?gòu)年?duì)列中取出任務(wù)去執(zhí)行。
方式一:固定線程池,一開(kāi)始是就申請(qǐng)好線程。
比如:
公司一次性雇傭5個(gè)工人,往后在接手任務(wù)還是這5 個(gè)人去做。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ThreadPoolV1{
private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);//隊(duì)列容量為10,大于就阻塞等待。
private Thread[] workers = new Thread[5];//線程的個(gè)數(shù)即一開(kāi)始雇傭的工人個(gè)數(shù)
ThreadPoolV1() {
for (int i = 0; i < 5; i++) {
workers[i] = new Worker(workQueue);
workers[i].start();//5個(gè)線程的啟動(dòng) 去完成業(yè)務(wù)
}
}
public void execute(Runnable cmd) throws InterruptedException {
workQueue.put(cmd);//將任務(wù)放到隊(duì)列中
}
private static class Worker extends Thread {
private BlockingQueue<Runnable> workQueue;
Worker(BlockingQueue<Runnable> queue) {
workQueue = queue;
}
@Override
public void run() {
while (!isInterrupted()) {
try {
Runnable cmd = workQueue.take();//從隊(duì)列中把任務(wù)取出來(lái)
cmd.run();//業(yè)務(wù)
} catch (InterruptedException e) {
break;
}
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadPoolV1 pool = new ThreadPoolV1();
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第一個(gè)事情");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第二個(gè)時(shí)期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第三 個(gè)時(shí)期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true)
System.out.println("第四個(gè)時(shí)期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第五個(gè)時(shí)期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第五個(gè)時(shí)期");
}
}
});
}
```
方式二:當(dāng)有任務(wù)時(shí)雇傭一個(gè)工人,再來(lái)任務(wù)時(shí)再雇傭一個(gè)工人,一直這樣,直到雇傭人數(shù)達(dá)到預(yù)期最大值,再來(lái)任務(wù)就放到隊(duì)列中去。
public class ThreadPoolV2 {
private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);//隊(duì)列上大于10個(gè)任務(wù)時(shí),發(fā)生阻塞等待
private int maxThreads = 5;
private int currentThreads = 0;//一開(kāi)始沒(méi)有線程
private Thread[] works = new Thread[maxThreads];
public void execute(Runnable cmd) throws InterruptedException {
if (currentThreads == maxThreads) {//雇傭人數(shù)達(dá)到最大值
workQueue.put(cmd);//任務(wù)放到隊(duì)列
} else {
Worker worker = new Worker(workQueue);//雇傭一個(gè)工人
works[currentThreads++] = worker;
worker.start();
workQueue.put(cmd);
}
}
private static class Worker extends Thread {
private BlockingQueue<Runnable> workQueue;
Worker(BlockingQueue<Runnable> queue) {
workQueue = queue;
}
@Override
public void run() {
while (!isInterrupted()) {
try {
Runnable cmd = workQueue.take();//從
cmd.run();
} catch (InterruptedException e) {
break;
}
}
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadPoolV1 pool = new ThreadPoolV1();
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第一個(gè)事情");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第二個(gè)時(shí)期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第三 個(gè)時(shí)期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第四個(gè)時(shí)期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第五個(gè)時(shí)期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第六個(gè)時(shí)期");
}
}
});
}
}
綜上所述,用java設(shè)計(jì)線程池有兩種方式,具體使用還要根據(jù)自己情況選擇其中一種方式,如果想了解更多相關(guān)文章內(nèi)容或知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站題目:如何用java設(shè)計(jì)一個(gè)線程池
當(dāng)前路徑:http://sd-ha.com/article26/jochjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、動(dòng)態(tài)網(wǎng)站、網(wǎng)站策劃、企業(yè)建站、標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)