java线程 addcall_【Java】Java线程池ExecutorService中重要的方法

首页

专栏

java

文章详情

0

Java线程池ExecutorService中重要的方法

108547.html入门小站发布于 今天 14:42

ExecutorService 介绍

b906239476b100be4bdb74099a01f8cb.png

b45c1e1a0b17e09798cb022f2ff7bac2.png

1. ThreadPoolExecutor

2. ScheduledThreadPoolExecutor

fdee3474d2cba2b018b391d96a00c7c6.png

ExecutorService的创建

newCachedThreadPool 创建一个可缓存的线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,如果没有可以回收的,则新建线程。

newFixedThreadPool 创建一个定长的线程池,可控制线程最大并发数,超出的线程会在队列中等待。

newScheduledThreadPool 创建一个定长线程池,可以定时周期性执行任务。

newSingleThreadPool 创建一个单线程线程池,它只会用唯一的线程来执行任务,保证所有任务按照指定顺序来执行(FIFO,LIFO)

ExecutorService的使用

ExecutorService executorService = Executors.newFixedThreadPool(10);

executorService.execute(new Runnable() {

public void run() {

System.out.println("入门小站");

}

});

executorService.shutdown();

ExecutorService的执行方法

execute(Runnable)

submit(Runnable)

submit(Callable)

invokeAny(…)

invokeAll(…)

execute(Runnable) 无法获取执行结果

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.execute(new Runnable() {

public void run() {

System.out.println("Asynchronous task");

}

});

executorService.shutdown();

submit(Runnable) 可以判断任务是否完成

Future future = executorService.submit(new Runnable() {

public void run() {

System.out.println("Asynchronous task");

}

});

future.get(); //returns null if the task has finished correctly.

submit(Callable)可以获取返回结果

Future future = executorService.submit(new Callable(){

public Object call() throws Exception {

System.out.println("Asynchronous Callable");

return "Callable Result";

}

});

System.out.println("future.get() = " + future.get());

invokeAny(…)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set> callables = new HashSet>();

callables.add(new Callable() {

public String call() throws Exception {

return "Task 1";

}

});

callables.add(new Callable() {

public String call() throws Exception {

return "Task 2";

}

});

callables.add(new Callable() {

public String call() throws Exception {

return "Task 3";

}

});

String result = executorService.invokeAny(callables);

System.out.println("result = " + result);

executorService.shutdown();

invokeAll(...)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set> callables = new HashSet>();

callables.add(new Callable() {

public String call() throws Exception {

return "Task 1";

}

});

callables.add(new Callable() {

public String call() throws Exception {

return "Task 2";

}

});

callables.add(new Callable() {

public String call() throws Exception {

return "Task 3";

}

});

List> futures = executorService.invokeAll(callables);

for(Future future : futures){

System.out.println("future.get = " + future.get());

}

executorService.shutdown();

线程池ExecutorService的关闭

关注微信公众号:【入门小站】,解锁更多知识点

java多线程

阅读 40发布于 今天 14:42

赞收藏

分享

本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议

108547.html

入门小站

rumenz.com

47声望

3粉丝

关注作者

0 条评论

得票时间

108547.html

提交评论

108547.html

入门小站

rumenz.com

47声望

3粉丝

关注作者

宣传栏

ExecutorService 介绍

b906239476b100be4bdb74099a01f8cb.png

b45c1e1a0b17e09798cb022f2ff7bac2.png

1. ThreadPoolExecutor

2. ScheduledThreadPoolExecutor

fdee3474d2cba2b018b391d96a00c7c6.png

ExecutorService的创建

newCachedThreadPool 创建一个可缓存的线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,如果没有可以回收的,则新建线程。

newFixedThreadPool 创建一个定长的线程池,可控制线程最大并发数,超出的线程会在队列中等待。

newScheduledThreadPool 创建一个定长线程池,可以定时周期性执行任务。

newSingleThreadPool 创建一个单线程线程池,它只会用唯一的线程来执行任务,保证所有任务按照指定顺序来执行(FIFO,LIFO)

ExecutorService的使用

ExecutorService executorService = Executors.newFixedThreadPool(10);

executorService.execute(new Runnable() {

public void run() {

System.out.println("入门小站");

}

});

executorService.shutdown();

ExecutorService的执行方法

execute(Runnable)

submit(Runnable)

submit(Callable)

invokeAny(…)

invokeAll(…)

execute(Runnable) 无法获取执行结果

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.execute(new Runnable() {

public void run() {

System.out.println("Asynchronous task");

}

});

executorService.shutdown();

submit(Runnable) 可以判断任务是否完成

Future future = executorService.submit(new Runnable() {

public void run() {

System.out.println("Asynchronous task");

}

});

future.get(); //returns null if the task has finished correctly.

submit(Callable)可以获取返回结果

Future future = executorService.submit(new Callable(){

public Object call() throws Exception {

System.out.println("Asynchronous Callable");

return "Callable Result";

}

});

System.out.println("future.get() = " + future.get());

invokeAny(…)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set> callables = new HashSet>();

callables.add(new Callable() {

public String call() throws Exception {

return "Task 1";

}

});

callables.add(new Callable() {

public String call() throws Exception {

return "Task 2";

}

});

callables.add(new Callable() {

public String call() throws Exception {

return "Task 3";

}

});

String result = executorService.invokeAny(callables);

System.out.println("result = " + result);

executorService.shutdown();

invokeAll(...)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set> callables = new HashSet>();

callables.add(new Callable() {

public String call() throws Exception {

return "Task 1";

}

});

callables.add(new Callable() {

public String call() throws Exception {

return "Task 2";

}

});

callables.add(new Callable() {

public String call() throws Exception {

return "Task 3";

}

});

List> futures = executorService.invokeAll(callables);

for(Future future : futures){

System.out.println("future.get = " + future.get());

}

executorService.shutdown();

线程池ExecutorService的关闭

关注微信公众号:【入门小站】,解锁更多知识点

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值