简介
让有限的工作线程(Worker Thread)来轮流异步处理无限多的任务。也可以将其归类为分工模式,它的典型实现就是线程池,也体现了经典设计模式中的享元模式。
注意事项
不过利用线程池处理任务时,需要注意一下,就我这里写了个生产者消费者,因为在生产者,消费者中,如果消费速度跟不上生产速度,那么很可能生产者将线程池中的所有资源都占有,并且wait等待,就没有办法继续进行工作了。
package com.bo.threadstudy.eight;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 工作线程模式,其实是享元模式的一种体现方式,用有限的线程数去执行无限的线程
*/
@Slf4j
public class WorkThreadModelTest {
//模拟一下线程饥饿池现象
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(2);
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
threadPoolWaitedTest(executorService, lock, condition);
// ExecutorService prdService = Executors.newFixedThreadPool(1);
// ExecutorService cusService = Executors.newFixedThreadPool(1);
// //需要状态来维护,true代表饭没做好,false代表饭做好了
// manyThreadPoolTest(lock, condition, prdService, cusService);
}
/**
* 生产者和消费者通过不同的线程池来区分
* @param lock
* @param condition
* @param prdService
* @param cusService
* @throws InterruptedException
*/
private static void manyThreadPoolTest(ReentrantLock lock, Condition condition, ExecutorService prdService, ExecutorService cusService) throws InterruptedException {
cusService.submit(() -> {
lock.lock();
try{
log.debug("要饭");
condition.await();
log.debug("我在吃饭");
return 0;
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
lock.unlock();
}
return 0;
});
cusService.submit(() -> {
lock.lock();
try{
log.debug("要饭");
condition.await();
log.debug("我在吃饭");
return 0;
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
lock.unlock();
}
return 0;
});
Thread.sleep(1000);
prdService.submit(() -> {
lock.lock();
try{
log.debug("我在做饭");
condition.signal();
return 0;
} finally{
lock.unlock();
}
});
prdService.submit(() -> {
lock.lock();
try{
log.debug("我在做饭");
condition.signal();
return 0;
} finally{
lock.unlock();
}
});
}
/**
* 在线程池中使用生产者和消费者的时候,很可能某一方将全部的资源都占据完全,然后导致所有线程等待卡死(这个不是死锁),而是线程池资源都被占了,没有办法
* @param executorService
* @param lock
* @param condition
* @throws InterruptedException
*/
private static void threadPoolWaitedTest(ExecutorService executorService, ReentrantLock lock, Condition condition) throws InterruptedException {
executorService.submit(() -> {
lock.lock();
try{
log.debug("要饭");
condition.await();
log.debug("我在吃饭");
return 0;
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
lock.unlock();
}
return 0;
});
executorService.submit(() -> {
lock.lock();
try{
log.debug("要饭");
condition.await();
log.debug("我在吃饭");
return 0;
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
lock.unlock();
}
return 0;
});
Thread.sleep(1000);
executorService.submit(() -> {
lock.lock();
try{
log.debug("我在做饭");
condition.signal();
return 0;
} finally{
lock.unlock();
}
});
executorService.submit(() -> {
lock.lock();
try{
log.debug("我在做饭");
condition.signal();
return 0;
} finally{
lock.unlock();
}
});
}
}
创建多少线程池合适
CPU密集型运算
通常采用 cpu 核数 + 1 能够实现最优的 CPU 利用率,+1 是保证当线程由于页缺失故障(操作系统)或其它原因导致暂停时,额外的这个线程就能顶上去,保证 CPU 时钟周期不被浪费
IO密集型运算
CPU 不总是处于繁忙状态,例如,当你执行业务计算时,这时候会使用 CPU 资源,但当你执行 I/O 操作时、远程
RPC 调用时,包括进行数据库操作时,这时候 CPU 就闲下来了,你可以利用多线程提高它的利用率。
经验公式如下
线程数 = 核数 * 期望 CPU 利用率 * 总时间(CPU计算时间+等待时间) / CPU 计算时间
例如 4 核 CPU 计算时间是 50% ,其它等待时间是 50%,期望 cpu 被 100% 利用,套用公式
4 * 100% * 100% / 50% = 8
例如 4 核 CPU 计算时间是 10% ,其它等待时间是 90%,期望 cpu 被 100% 利用,套用公式
4 * 100% * 100% / 10% = 40
这个IP密集型运算,这个公式得记一下,万一面试用呢,CPU核数*CPU期望利用率*全部执行时间/CPU执行时间。
本文探讨了工作线程模式如何通过线程池高效处理无限任务,介绍了享元模式的应用。重点提示了生产者消费者模型中避免线程饥饿的问题。适合理解CPU密集和I/O密集场景下线程数量的选择。
568

被折叠的 条评论
为什么被折叠?



