博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java concurrency 线程_JAVA CONCURRENCY EXECUTORS 介绍Java并发处理线程池
阅读量:6486 次
发布时间:2019-06-23

本文共 3476 字,大约阅读时间需要 11 分钟。

I would make a fool out of myself if I tell you that util.concurrent APIs kicks cheetah's ass when the classes are available since 2004. However, there are some cool features which I would like to revisit. Concurrency experts, now is the time for you to close this window. All others, stay tight for the fun ride.

Thou shall not forget your roots

2e18c8808b6b125cfcfcd765b84a3985.png

Executor is the root interface with a single execute method. Anything that implements a Runnable interface can passed as a parameter. Silly Executor, however, has no support for Callable though.

cb724dab452cd8d74f456f5f013a32a3.png

Good news : ExecutorService interface, which extends Executor, adds support for Callable. Its implementation class is ThreadPoolExecutor.

I am going to pretend that the ScheduledExecutorService interface and its implementation class ScheduledThreadPoolExecutor does not exist as they just add scheduling capabilities on top of the ExecutorService and ThreadPoolExecutor. But remember this class when the powerful but boring java.util.Timer is not enough and a full blown external scheduler is just too much.

If you are new to concurrency or forgot the difference between Callable and Runnable, you might want to read up a little before reading further. A dummy guide is here

The ExecutorService.submit Facts :

The three submit variants :

Future submit(Callable task)

Future submit(Runnable task)

Future submit(Runnable task, T result)

The submit method of the ExecutorService is overloaded and accepts either a Callable or Runnable.

Since the run method of the Runnable returns void, it is no surprise that Future.get always returns a null when the task is complete.

Future> submit(Runnable task)

The other overloaded submit method that accepts a Runnable and a generic returns whatever you passed in as the second parameter as the result.

Future submit(Runnable task, T result)

In fact, opening up the code (FutureTask), you'll notice that the RunnableAdapter top level nested class of Executors simply holds the result and returns the same result after the run method is complete.

###为何用ThreadPoolExecutor 的submit(runnable,T result)却是可以返回结果的呢??

static final class RunnableAdapter implements Callable {

final Runnable task;

final T result;

RunnableAdapter(Runnable task, T result) {

this.task = task;

this.result = result;

}

public T [More ...] call() {

task.run();

return result;

}

}

In both the cases, if you would like to (you should !) terminate the program instead of your executor thread blocking the program and entering a busy loop, you should call the shutdown method as in

executorService.shutdown()

shutDown facts

You could imagine shutdown as a half-closed door of a mall. No new customers will be let in but the existing customers can leave the mall once they are done.

To reiterate,

shutdown is a polite method. It does not actually shut down the tasks in the pool immediately. It just says that no new tasks will be accepted.

Unless you are executing your tasks using invokeAll, you would need to wait for all tasks in progress to complete. This is achieved by calling the awaitTermination method.

(invokeAll and submit examples at the bottom of the post)

Once all the current tasks are done, the executor service shuts down.

If you are in need of an impolite, intruding method which doesn't care whether the current threads are done with their tasks, then shutdownNow is your guy. However, there's no guarantee that the method will shutdown the service on the dot but it is the closest you have to immediate shutdown.

转载地址:http://qlnuo.baihongyu.com/

你可能感兴趣的文章
二分法求平方根(Python实现)
查看>>
使用startActivityForResult方法(转)
查看>>
so在genymotation中错误问题
查看>>
Visual Studio 原生开发的10个调试技巧(二)
查看>>
Windows内核再次出现0Day漏洞 影响win2000到win10所有版本 反病毒软件恐成瞎子
查看>>
H3C品牌刀片系统强势首发
查看>>
【CSS系列】图像映射
查看>>
First blood
查看>>
java 冒泡排序和快速排序 实现
查看>>
SQL存储过程中的几个常见设定SET QUOTED_IDENTIFIER/NOCOUNT/XACT_ABORT ON/OFF
查看>>
Silverlight与Flash区别之一
查看>>
删除恢复Hadoop集群中的DataNode
查看>>
Silverlight 2动态创建矩形对象(附完整源代码)
查看>>
从京东技术演进看互联网企业的成长历程
查看>>
MFC ado+mysql+odbc技术分享
查看>>
js中让字符串中特定字符红色显示
查看>>
HttpClient4.5教程-第二章-连接管理
查看>>
redhat Nginx 安装
查看>>
oracle 配置监听
查看>>
moosefs即将发布新版
查看>>