您好,登錄后才能下訂單哦!
如何用java設計一個線程池?相信很多新手小白還沒學會這個技能,通過這篇文章的總結,希望你能學會用java設計線程池。如下資料是用java設計線程池的步驟。
思路與生產者與消費者模式相同,將任務放到隊列中,子線程再從隊列中取出任務去執行。
方式一:固定線程池,一開始是就申請好線程。
比如:
公司一次性雇傭5個工人,往后在接手任務還是這5 個人去做。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ThreadPoolV1{
private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);//隊列容量為10,大于就阻塞等待。
private Thread[] workers = new Thread[5];//線程的個數即一開始雇傭的工人個數
ThreadPoolV1() {
for (int i = 0; i < 5; i++) {
workers[i] = new Worker(workQueue);
workers[i].start();//5個線程的啟動 去完成業務
}
}
public void execute(Runnable cmd) throws InterruptedException {
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("第一個事情");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第二個時期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第三 個時期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true)
System.out.println("第四個時期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第五個時期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第五個時期");
}
}
});
}
```
方式二:當有任務時雇傭一個工人,再來任務時再雇傭一個工人,一直這樣,直到雇傭人數達到預期最大值,再來任務就放到隊列中去。
public class ThreadPoolV2 {
private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);//隊列上大于10個任務時,發生阻塞等待
private int maxThreads = 5;
private int currentThreads = 0;//一開始沒有線程
private Thread[] works = new Thread[maxThreads];
public void execute(Runnable cmd) throws InterruptedException {
if (currentThreads == maxThreads) {//雇傭人數達到最大值
workQueue.put(cmd);//任務放到隊列
} else {
Worker worker = new Worker(workQueue);//雇傭一個工人
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("第一個事情");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第二個時期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第三 個時期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第四個時期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第五個時期");
}
}
});
pool.execute(new Runnable() {
@Override
public void run() {
while (true) {
System.out.println("第六個時期");
}
}
});
}
}
綜上所述,用java設計線程池有兩種方式,具體使用還要根據自己情況選擇其中一種方式,如果想了解更多相關文章內容或知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。