您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
如何用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。