标签:开始 class inter oid ble ide ext 公司 容量
如何去设计一个自己的线程池?方式一:固定线程池,一开始是就申请好线程。
比如:
公司一次性雇佣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("第六个时期");
}
}
});
}
}
标签:开始 class inter oid ble ide ext 公司 容量
原文地址:https://blog.51cto.com/14232658/2468768