最近刚刚写完一个多线程的模块,感叹java concurrent强大的同时,整理了下近一周学习,打算大概记录下,
主要是读了部分concurrent包的代码. 有了java concurrent,客户端写多线程果然简单多了,首先需要用到的类是java.util.concurrent.Executors, 由它来生成线程执行器java.util.concurrent.ExecutorService,然后根据需要
可以用不同的方式运行线程,例如:单个任务可以用executorService.submit(task)或者execute(task);前者是
ExecutorService接口就有的方法,任务执行完以后可以返回值,后者是没有返回值。当然这两者区别还是蛮大的,
前者一定会将Runnable任务包装成FutureTask,后者就不一定了。多个任务可以用invokeAll/invokeAny提交执行。
简单示例:
ExecutorService es = Executors.newSingleThreadExecutor();
Future<String> r = es.submit(new Task());
es.shutdown();
try {
r.get();//get result
} catch (InterruptedException e) {
//
} catch (ExecutionException e) {
//
}
貌似重要学习入口在java.util.concurrent.Executors了,查看该类的说明,主要提供了java concurrent 包中Executor
、ExecutorService
、ScheduledExecutorService
、ThreadFactory
和 Callable
类的工厂和实用方法。阅读
其代码也不多,发现他创建线程执行器大致分为两种,一种是ThreadPoolExecutor,另一种
ThreadPoolExecutor/ScheduledThreadPoolExecutor包装以后的线程执行器;创建Callable主要有两种,一是将Runnable
适配一下返回RunnableAdapter,另一是包装了Callable的PrivilegedCallable;创建ThreadFactory也主要是两种,都是
其内部类,一是DefaultThreadFactory,另一是PrivilegedThreadFactory;总之,他可以看作工厂方法类,其他创建的
ThreadFactory、Callable可以先放放。
看完了Executors,也没看出什么。看来重要的学习入口在Executors创建的ExecutorService了。产看该接口,该接口
集成自父接口:Executor。Executor接口只定义了void execute(Runnable command)方法,用来执行提交的任务。而
ExecutorService除了execute方法之外,还定义了submit,invorkAll,invokeAny,shutdwon等等方法,主要用来执行并跟踪
多个异步任务,终止管理等。
continue,AbstractExecutorService类提供了ExecutorService接口除了execute之外其他方法的默认实现。产看其代
码,发现真正执行任务的却是他没有实现的execute方法,其他执行线程的方法,如sumbit和invokeAll内部都会调用execute(Runnable)方法,而invokeAny方法在其调用的doInvokeAny方法中还是会在ExecutorCompletionService.submit
方法中调用execute(Runnable)方法。具体代码就不贴了,JDK中都有。这里顺便对submit,invokeAll,invokeAny方法的
区别不提了,JDK方法都有注释,比较简单。要提下,执行的任务。从void execute(Runnable command)的签名可以看出
他执行的任务是Runnable,众所周知Runnable任务是没有返回值的,若需要任务的返回值则需要Callable来作为任务,
这里Concurrent提供了Runnable和Callable的适配器。invokeAll,submit,invokeAny方法中会将Runnable包装成callable
来执行。具体:
今天就写这些,下班了,留给下一次写ThreadPoolExecutor吧。
- 大小: 47.8 KB
分享到:
相关推荐
If you are installing Webglimpse via ssh, you will need to install Glimpse first. (The FTP and cPanel installers automatically retrieve and install the appropriate Glimpse binary). The most recent ...
资源分类:Python库 所属语言:Python 资源全名:glimpse-0.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Give the right first impression with just a glimpse! Instead of center cropping images blindly, leverage Glimpse's eye to catch the right spot. Setup Add to top level gradle.build file allprojects { ...
This book draws on their considerable experience, taking you from the very first steps of installing Node.js on your computer all the way to creating, debugging, and deploying production applications....
so that you can easily have a glimpse at your appointments. Want to contribute to the project? Start by translating the widget to another language. Visit the project at crowdin.net and be part of ...
添加上下文菜单项以将图像发布到Glimpse服务。 此扩展程序允许您轻松地从任何网站(如Facebook)发布任何图像到Glimpse(测试版)。只需右键点击图片,然后选择“Post to Glimpse”,然后按照说明操作即可。简单! ...
From Google search to self-driving cars to human longevity, is Alphabet creating a neoteric Garden of Eden or Bentham's Panopticon? Will King Solomon's challenge supersede the Turing test for ...
The field has the potential to transform a range of industries, from self-driving cars to intelligent business applications. Yet machine learning is so complex and wide-ranging that even its ...
角度一瞥在您的 AngularJS 单页应用程序上有 Glimpse Heads-Up Display。安装此项目在任何包管理器上均不可用。 直接从 GitHub 使用。配置 var app = angular . module ( 'myApp' , [ 'wt.glimpse' ] ) ;...app . ...
明确通过 Glimpse.RavenDb.Profiler.AttachTo() Web.Config 通过 Glimpse.RavenDb.DocumentStoreApplicationKey AppSetting。 您还可以通过以下任一方式从文档中过滤敏感数据 调用 Glimpse.RavenDb.Profiler....
Glimpse及web.condig默认设置 于2012/5/22更新于Nuget
From idea to prototype, a learner's guide for web development with the Django application framework About This Book Build two real-life based projects, one based on SQL and other based on NoSQL Best ...
编程教程 Topics in This Chapter • “Why JavaServer Faces?...• “A First Glimpse of Ajax” on page 21 • “JSF Framework Services” on page 24 • “Behind the Scenes” on page 26 From
nutritious yogurt for malnourished children in Bangladesh to building eyecare hospitals that will save thousands of poor people from blindness, Creating a World Without Poverty offers a glimpse of ...
Glimpse Extension,用于ASP.NET应用程序的服务器端调试和诊断信息
glimpse_android 该应用程序实现了基于动态编程的帧追赶方案。 专为Android设备和Google Glass设计
This book aims to bring newcomers to natural language processing (NLP) and deep learning to a tasting table covering important topics in both areas. Both of these subject areas are growing ...
You will then learn to build your first Twitter-like app. Later on, you will be introduced to Hashtags, AJAX to enhance the user interface, and tweets. You will then move on to create an ...
based aspects for language processing rather than aiming at exhaustively treating the numerous tasks that benefit from graph-based methods, it cannot replace a general introduction to NLP or IR:...
Glimpse.EventStore Event Store ( ) 的 Glimpse ( ) 插件。 插件向 Glimpse 客户端添加了一个“Event Store”选项卡,显示 EventStore 连接上的所有活动,包括持续时间、参数和结果。 用法 将 Glimpse.EventStore...