20190619-James-快速鸟瞰并发编程, 呕心沥血整理的架构技术(第3篇)

| newCachedThreadPool | 返回ExecutorService对象,该对象持有不同大小的线程池。 || newSingleThreadScheduledExecutor | 返回ScheduledExecutorService对象,只返回1个线程 。 || newScheduledThreadPool | 返回一个ScheduledExecutorService核心线程集。 || n
摘要由CSDN通过智能技术生成

| newCachedThreadPool | 返回ExecutorService对象,该对象持有不同大小的线程池。 |
| newSingleThreadScheduledExecutor | 返回ScheduledExecutorService对象,只返回1个线程 。 |
| newScheduledThreadPool | 返回一个ScheduledExecutorService核心线程集。 |
| newWorkStealingPool | 返回ExecutorService`对象,拥有多个任务队列(以便减少连接数)的线程池。 |

注意:

调整线程池大小时,大小是根据你的计算机中的逻辑核心数而定的。这个大小可以通过调用Runtime.getRuntime().availableProcessors()方法获得该值。

线程池实现描述
ThreadPoolExecutor线程池大小可调整,ThreadPoolExecutor实现了ExecutorService接口,使用池里的线程来执行你提交的任务,通常使用 Executors 工厂方法来配置。
ScheduledThreadPoolExecutorThreadPoolExecutor的扩展,并提供了执行定期任务的功能。
ForkJoinPoolForkJoinPool实现了ExecutorService接口,ForkJoinPool 采用分治思想将大任务分割成几个小任务,小任务继续分割成更小的任务,直至任务不可分割,然后运行这些任务。

任务随着ExecutorService#submitExecutorService#invokeAll或者提交,ExecutorService#invokeAny对于不同类型的任务具有多个重载。

其实 功能接口如下

接口描述
Runnablerun()方法没有返回值。
Callablecall方法有返回值。

Future

Future是对于具体的Runnable任务或Callable任务的执行结果进行取消、查询是否完成、获取结果。必要时可以通过get方法获取执行结果,该方法会阻塞直到任务返回结果。

ExecutorService使用Future作为返回类型。

ExecutorService executorService = Executors.newSingleThreadExecutor();
Future future = executorService.submit(() -> “结果”);
try {
String result = future.get(1L, TimeUnit.SECONDS);
System.out.println(“结果为 '” + result + “’.”);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e.getCause());
} catch (TimeoutException e) {
throw new RuntimeException(e);
}
assert future.isDone();

ReentrantLock锁

java.util.concurrent.locks软件包括了经常使用到的Lock接口。ReentrantLock类其实也实现了synchronized关键字的功能,还提供了其它功能,例如获取有关锁的状态,非阻塞tryLock()和可中断锁的信息。使用显式ReentrantLock的示例如下:

class JamesCounter {
private final Lock lock = new ReentrantLock();
private int value;

int increment() {
lock.lock();
try {
return ++value;
} finally {
lock.unlock();
}
}
}

ReadWriteLock读写锁

java.util.concurrent.locks还包含一个ReadWriteLock接口(ReentrantReadWriteLock实现),读写锁,通常允许多个并发读取,但只允许一个写入。

class JamesStatistic {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private int value;

void increment() {
lock.writeLock().lock();
try {
value++;
} finally {
lock.writeLock().unlock();
}
}

int current() {
lock.readLock().lock();
try {
return value;
} finally {
lock.readLock().unlock();
}
}
}

CountDownLatch工具

CountDownLatch主要用过计数,比如开项目大会,项目经理在会议室门口,有5个程序员A B C D E(相当于5个线程)分别来会议室开会,项目经理手写拿了一份会议人员名单,程序员A进入了会议室后,项目经理把A名单打个勾表示来了(相当于创建了线程A),B进会议室后,在名单上把B也打勾(相当于创建了线程B),但请注意,人没到齐, A,B程序员只能在座位上等待(线程全在等待阻塞中),还不能开会,等5个程序员都到齐了,才开会(5个线程同时被唤醒,开始工作)。

@SpringBootTest(classes = TripApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class JamesTestInvokeRemote {

private static final int THREADS = 200; //200线程模拟用户提交并发

RestTemplate rest = new RestTemplate();

private final String url = “http://127.0.0.1:8090/buyTicket?idcard=123456”;

private static CountDownLatch cdl = new CountDownLatch(THREADS);//200

@Test
public void TestInvoke() throws InterruptedException {
for(int i = 0; i < THREADS; i++){
new Thread(new TicketRequest()).start();//模拟5个程序员陆陆续续进门
}
}
public class JamesTicketRequest implements Runnable{

@Override
public void run() {

由于篇幅原因,这份面试宝典已经被整理成了PDF文档,有需要Android面试宝典全套完整文档的麻烦点赞+点击GitHub即可获取资料免费领取方式!

本文在开源项目:GitHub中已收录,里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md),里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值