- 浏览: 1090465 次
文章分类
- 全部博客 (379)
- S2SH (16)
- stuts2 (0)
- java语言 (81)
- JSP (17)
- <html>元素 (11)
- javaweb (4)
- web容器 (3)
- ext (23)
- javaScript (48)
- ant (1)
- liferay (1)
- sql (9)
- css (42)
- 浏览器设置 (3)
- office_world (1)
- eclipse (4)
- 其它 (28)
- 操作系统 (5)
- android (6)
- Struts2 (11)
- RegEx (3)
- mysql (5)
- BigDATA (1)
- Node.js (1)
- Algorithm (10)
- Apache Spark (1)
- 数据库 (5)
- linux (2)
- git (1)
- Adobe (3)
- java语言,WebSocket (1)
- Maven (3)
- SHELL (1)
- XML (2)
- 数学 (2)
- Python (2)
- Java_mysql (1)
- ReactJS (6)
- 养生 (4)
- Docker (1)
- Protocols (3)
- java8 (2)
- 书籍 (1)
- Gradle (2)
- AngularJS (5)
- SpringMVC (2)
- SOAP (1)
- BootstrapCSS (1)
- HTTP协议 (1)
- OAuth2 (1)
最新评论
-
Lixh1986:
Java并发编程:自己动手写一把可重入锁https://blo ...
Java之多线程之Lock与Condition -
Lixh1986:
http://win.51apps.com.cn/https: ...
temp -
ztwsl:
不错,支持很好
HttpServletRequest和ServletRequest的区别 -
guodongkai:
谢谢您能将知识精华汇编总结,让初学者们从原理中学会和提高。
javaScript之function定义 -
kangwen23:
谢谢了,顶顶
struts2中的ValueStack学习
Java之 java.util.concurrent 包之ExecutorService之submit () 之 Future
- 博客分类:
- java语言
一、如何使用 ExecutorService.submit() ?
submit()
可以接受 Callable 或 Runnable 对象。
返回值是 Future 对象(调用 Future 对象的 get() 方法会导致主线程阻塞)。
二、程序
java.util.concurrent包之Execuotor系列文章
00_Java之 java.util.concurrent 包之概述
01_Java之java.util.concurrent包之Executor与ExecutorService
02_Java之 java.util.concurrent 包之ExecutorService之submit () 之 Future
03_Java之多线程之Callable与Future
04_Java之多线程之Lock
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2360306
-
submit()
可以接受 Callable 或 Runnable 对象。
返回值是 Future 对象(调用 Future 对象的 get() 方法会导致主线程阻塞)。
二、程序
import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import org.junit.Test; public class TestExecutorService { class Runnabled implements Runnable{ @Override public void run() { System.out.println("[Runnable-child] running..."); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } class Callabled implements Callable<String>{ @Override public String call() throws Exception { System.out.println("[Callable-child] calling..."); Thread.sleep(100); return "finished"; } } //========================================================== @Test public void test() throws Exception{ testExecutorService_Runnable(); testExecutorService_Callable(); testExecutorService_shutdown(); } //========================================================== public void testExecutorService_Runnable() throws Exception { Runnabled runnable = new Runnabled(); ExecutorService es1 = Executors.newCachedThreadPool(); for(int i = 0; i < 10; i++){ System.out.println("[Runnable] main thread : " + i + "[Blocked in Main]"); es1.submit(runnable).get(); } for(int i = 0; i < 10; i++){ System.out.println("[Runnable] main thread : " + i); es1.execute(runnable); //es1.submit(runnable) // same as, if you don't expect a result. } } public void testExecutorService_Callable() throws Exception{ ExecutorService es2 = Executors.newScheduledThreadPool(10); Callabled callable = new Callabled(); for(int i = 0; i < 10; i++){ System.out.println("[Callable] main thread : " + i + "[Blocked in Main]"); es2.submit(callable).get(); } for(int i = 0; i < 10; i++){ System.out.println("[Callable] main thread : " + i); es2.submit(callable); } } public void testExecutorService_shutdown() throws Exception{ ExecutorService es2 = Executors.newScheduledThreadPool(10); /* Does not wait for previously submitted tasks to complete execution. Start an immediately shutdown and no new tasks will be accepted. The invocation has no additional effect if already shut down. */ es2.shutdown(); /* Blocks, until all tasks have completed execution after 1) a shutdown request, 2) or the timeout occurs, 3) or the current thread is interrupted, whichever happens first. */ es2.awaitTermination(10000, TimeUnit.SECONDS); System.out.println("----------------------------"); System.out.println("print me immediately after all task completed."); System.out.println("no need to wait for 10000 seconds."); } /** NOTE: ExecutorService.shutdown() VS ExecutorService.awaitTermination() 1. awaitTermination() will wait for all task to complete. shutdown() method does not wait. if shutdown() method comes before awaitTermination(), Then when all task complete in or less than timeout, thread will shut down immediately. 2. If you want to get the task result immediately, you should use: result = exec.submit(Callable/Runnable).get(); NOTE: If you call get(), it will be blocked. If you just call submit(), it will not be blocked. 3. If you want to get the task result after all task completed, you can store the Future object in to a collection. use: Future f = exec.submit(Callable/Runnable); arrayList.add(f); then after all task completed, you can retrieve the Future objects stored in arrayList. */ /** * NOTE: Callable VS Runnable * * Callable and Runnable are almost same when using * ExecutorService.submit() method. * * They can be both blocked when using Future.get() * method. * * while: * 1. Callable's call() method can return a result. * Runnable's run() method is void. * * 2. Callable's call() method throws exception. * Runnable's run() method cannot throw exception. */ }
/* Result: =============================================================== [Runnable] main thread : 0 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 1 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 2 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 3 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 4 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 5 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 6 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 7 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 8 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 9 [Blocked in Main] [Runnable-child] running... [Runnable] main thread : 0 [Runnable] main thread : 1 [Runnable-child] running... [Runnable-child] running... [Runnable] main thread : 2 [Runnable] main thread : 3 [Runnable-child] running... [Runnable] main thread : 4 [Runnable-child] running... [Runnable] main thread : 5 [Runnable-child] running... [Runnable] main thread : 6 [Runnable-child] running... [Runnable] main thread : 7 [Runnable-child] running... [Runnable] main thread : 8 [Runnable-child] running... [Runnable] main thread : 9 [Runnable-child] running... [Runnable-child] running... [Callable] main thread : 0 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 1 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 2 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 3 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 4 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 5 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 6 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 7 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 8 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 9 [Blocked in Main] [Callable-child] calling... [Callable] main thread : 0 [Callable] main thread : 1 [Callable] main thread : 2 [Callable-child] calling... [Callable] main thread : 3 [Callable-child] calling... [Callable-child] calling... [Callable-child] calling... [Callable] main thread : 4 [Callable] main thread : 5 [Callable-child] calling... [Callable-child] calling... [Callable] main thread : 6 [Callable] main thread : 7 [Callable-child] calling... [Callable-child] calling... [Callable] main thread : 8 [Callable] main thread : 9 [Callable-child] calling... [Callable-child] calling... ---------------------------- print me immediately after all task completed. no need to wait for 10000 seconds. */
java.util.concurrent包之Execuotor系列文章
00_Java之 java.util.concurrent 包之概述
01_Java之java.util.concurrent包之Executor与ExecutorService
02_Java之 java.util.concurrent 包之ExecutorService之submit () 之 Future
03_Java之多线程之Callable与Future
04_Java之多线程之Lock
转载请注明,
原文出处:http://lixh1986.iteye.com/blog/2360306
-
发表评论
-
java 将文件夹所有的文件合并到指定的文件夹下
2020-06-30 19:17 974场景:将文件夹所有的文件合并到指定的文件夹下 另外:如果想效 ... -
多线程-线程池的四种创建方式
2020-04-01 18:38 406多线程-线程池的四种创建方式 https://blog.cs ... -
Java基础之:nio
2019-11-13 15:38 409一、理论讲解: 史上最强Java NIO入门:担心从入门到放弃 ... -
Java 分布式之:RPC 基本概念
2019-11-13 15:07 398转载: https://www.jianshu.com/p/ ... -
Java之 volatile 关键字原理详解
2019-11-07 15:36 439一、什么是 volatile ? ... -
POI实现excell批注背景图片(仿html浮窗显示图片)
2019-10-21 08:17 597POI实现excell批注背景图片(仿html浮窗显示图片) ... -
Java之设计模式之 Observer 观察者
2019-07-04 17:21 969观察者设计模式 Java 已经实现了该模式,并且提供了使用类 ... -
HashMap, LinkedHashMap and TreeMap
2019-03-01 11:04 622https://stackoverflow.com/a/177 ... -
Java lib 操作 excel 插入图片
2019-01-19 12:46 837https://poi.apache.org/componen ... -
数据库连接池C3P0
2018-05-29 16:50 808一、名字的由来 很多 ... -
Java8之集合(Collection)遍历 forEach()、stream()
2018-05-29 14:39 20656package java8.collections; ... -
Junit Vs main on "java.util.concurrent.Executors"
2017-11-10 16:44 731Same code with different result ... -
Java之大数据学习路线
2017-11-03 10:08 5671三个月大数据研发学习 ... -
Java中创建对象的5种方式
2017-10-26 14:21 802一、Java之5种创建对象的方式 ————————————— ... -
Log4j和Slf4j的比较
2017-06-23 12:41 1358一直搞不清 Log4j 和 SLF4j 的关系。今天才若有所 ... -
Java之Java7新特性之try资源句式
2017-04-20 14:58 5337Java之Java7新特性之try资源句式 一、【try资源 ... -
Java之 java.util.concurrent 包之Executor与ExecutorService
2017-03-04 21:18 2638一、问题: execute() 与 submit() 的区别? ... -
JAVAEE之单用户登录
2017-02-05 11:55 1030单用户登录是系统中数据一直性的解决方案之一。 问题背景: 试 ... -
Java之多线程之线程池之线程重复使用
2017-02-04 13:33 5520一、问题背景 在使用多线程时,如果要开启一个任务,则就需要新 ... -
Java之语法之方法调用之地址传值之空对象(null)
2017-01-26 14:05 3232一、问题: public void test ...
相关推荐
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
java并发工具包 java.util.concurrent中文版-带书签版
java并发工具包 java.util.concurrent中文版pdf
java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1)
标签:atlassian-util-concurrent-0.0.12.jar.zip,atlassian,util,concurrent,0.0.12,jar.zip包下载,依赖包
java.util.concurrent总体概览图。 收取资源分3分。需要的同学可以下载一下。 java.util.concurrent主要包括5个部分executor,colletions,locks,atomic,tools。 该图详细的列举了并发包下面的结构,包含所有接口和...
主要介绍了java.util.concurrent.ExecutionException 问题解决方法的相关资料,需要的朋友可以参考下
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用java.util.zip包实现数据的压缩与解压,并扩展到在网络传输方面如何应用java.util.zip包现数据压缩与解压
如何启动:以win7系统为例,最好jdk8 1.打开cmd,cd到jdk的path,本机是:cd C:\Java\jdk6\bin ...java -cp D:\javaConcurrentAnimated.jar vgrazi.concurrent.samples.launcher.ConcurrentExampleLauncher
java_util_concurrent_user_guide.zipjava_util_concurrent_user_guide.zipjava_util_concurrent_user_guide.zipjava_util_concurrent_user_guide.zipjava_util_concurrent_user_guide.zipjava_util_concurrent_user...
Java并发编程工具包java.util.concurrent的UML类结构图 PDF
JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用
详细介绍了java.util.logging.Logger的用法和结构,对如果扩展Logger起到抛砖引玉的作用!尊重劳动成果,亲下载了要给个评价!
java.util.concurrent系列文章(2) java.util.concurrent系列文章(2) java.util.concurrent系列文章(2) java.util.concurrent系列文章(2)
Java 并发工具包 java.util.concurrent 用户指南 中英文对照阅读版 pdf
backport-util-concurrent.jarbackport-util-concurrent.jarbackport-util-concurrent.jar
花了一段时间辛苦整理的ppt,与大家分享,请提出您的宝贵意见。