- 浏览: 1130829 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (411)
- Java Foundation (41)
- AI/机器学习/数据挖掘/模式识别/自然语言处理/信息检索 (2)
- 云计算/NoSQL/数据分析 (11)
- Linux (13)
- Open Source (12)
- J2EE (52)
- Data Structures (4)
- other (10)
- Dev Error (41)
- Ajax/JS/JSP/HTML5 (47)
- Oracle (68)
- FLEX (19)
- Tools (19)
- 设计模式 (4)
- Database (12)
- SQL Server (9)
- 例子程序 (4)
- mysql (2)
- Web Services (4)
- 面试 (8)
- 嵌入式/移动开发 (18)
- 软件工程/UML (15)
- C/C++ (7)
- 架构Architecture/分布式Distributed (1)
最新评论
-
a535114641:
LZ你好, 用了这个方法后子页面里的JS方法就全不能用了呀
页面局部刷新的两种方式:form+iframe 和 ajax -
di1984HIT:
学习了,真不错,做个记号啊
Machine Learning -
赵师傅临死前:
我一台老机器,myeclipse9 + FB3.5 可以正常使 ...
myeclipse 10 安装 flash builder 4.6 -
Wu_Jiang:
触发时间在将来的某个时间 但是第一次触发的时间超出了失效时间, ...
Based on configured schedule, the given trigger will never fire. -
cylove007:
找了好久,顶你
Editable Select 可编辑select
Java Tutorials -> Concurrency:
http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
Java Concurrency Utilities:
http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/index.html
作者 Doug Lea 的个人主页,里头不少好东西:
http://g.oswego.edu/
http://www.vogella.com/articles/JavaConcurrency/article.html
http://tutorials.jenkov.com/java-util-concurrent/index.html
concurrent 包详解:
http://www.cnblogs.com/crazybit/p/3150806.html
线程同步: 待完善。。。
concurrent 包之前,我们可以使用 synchronized 关键字实现线程同步;cooncurrent 包之后,实现 synchronization 的方式除了 synchronized keyword 之外,你还可以使用:
CountDownLatch: A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
CyclicBarrier: A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.
Semaphore
如果通过ExecutorService创建了n个线程,那在主线程中是怎么知道ExecutorService中所有的Callable是否都已经执行完毕那?答案是Callable接口的返回值!Callable接口是有返回值的,该返回值封装在ExecutorService的返回值Future里。Future值返回的时候,也就是ExecutorService所起的线程执行完毕的时候;而主线程中,可以将所起线程的返回值Future赋给一个局部变量,从而实现“所起线程未执行完毕时主线程卡在局部变量的赋值操作上,所起线程执行完毕后主线程才继续往下走”的效果。
http://stackoverflow.com/questions/3269445/executorservice-how-to-wait-for-all-tasks-to-finish
使用ExecutorService,一定记得在用完后调用其executorService.shutdown()将其shutdown掉;如果你未调用shutdown()的话,则executorService所起的线程不会退出(这些线程在等着你往其中加入更多的tasks),而这会导致jvm无法退出。
shutdown()方法不会立即terminated掉executorService,只是使其不再接受新的tasks,已submmit入其中的tasks会继续执行,这些already submmitted tasks都执行完后,executorService才会真正的terminated。
如果想立即terminated掉executorService,使用executorService.shutdownNow(),该方法不光使executorService不再接受新的tasks,而且会使其尝试终止所有已提交(尚未开始或尚未完成)的tasks。
http://tutorials.jenkov.com/java-util-concurrent/executorservice.html
引用
Closing an ExecutorService
When you are done using the ExecutorService you should shut it down, so the threads do not keep running.
For instance, if your application is started via a main() method and your main thread exits your application, the application will keep running if you have an active ExexutorService in your application. The active threads inside this ExecutorService prevents the JVM from shutting down.
To terminate the threads inside the ExecutorService you call its shutdown() method. The ExecutorService will not shut down immediately, but it will no longer accept new tasks, and once all threads have finished current tasks, the ExecutorService shuts down. All tasks submitted to the ExecutorService before shutdown() is called, are executed.
If you want to shut down the ExecutorService immediately, you can call the shutdownNow() method. This will attempt to stop all executing tasks right away, and skips all submitted but non-processed tasks. There are no guarantees given about the executing tasks. Perhaps they stop, perhaps the execute until the end. It is a best effort attempt.
When you are done using the ExecutorService you should shut it down, so the threads do not keep running.
For instance, if your application is started via a main() method and your main thread exits your application, the application will keep running if you have an active ExexutorService in your application. The active threads inside this ExecutorService prevents the JVM from shutting down.
To terminate the threads inside the ExecutorService you call its shutdown() method. The ExecutorService will not shut down immediately, but it will no longer accept new tasks, and once all threads have finished current tasks, the ExecutorService shuts down. All tasks submitted to the ExecutorService before shutdown() is called, are executed.
If you want to shut down the ExecutorService immediately, you can call the shutdownNow() method. This will attempt to stop all executing tasks right away, and skips all submitted but non-processed tasks. There are no guarantees given about the executing tasks. Perhaps they stop, perhaps the execute until the end. It is a best effort attempt.
invokeAll 是 blocking 的,future.get 也是 blocking 的。那么,invokeAll 后,在 for 里的 future.get 会阻塞当前线程吗?
invokeAll(tasks),即调用不带超时的 invokeAll 后, future.get 肯定不会阻塞当前线程,因为 invokeAll(tasks) 阻塞结束后,所有的 tasks 的 Future.isDone 都已经是 true 了(Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true)。
invokeAll(tasks, timeout,unit),即调用带超时的 invokeAll 后,future.get 其实也不会阻塞当前线程,因为这是 tasks 或者是 completed,或者是 cancelled了,详见带超时invokeAll的注释。
代码例子:待补
引申:
试想一种场景:实现类似 invokeAll 的批量提交 task,但又不希望其阻塞当前外围线程,怎么做? 待补且查。。。。
http://stackoverflow.com/questions/18493318/is-there-a-non-blocking-method-analogous-to-executorservice-invokeall
http://stackoverflow.com/questions/826212/java-executors-how-to-be-notified-without-blocking-when-a-task-completes
invokeAll vs CompletionService:待补
execute() 和 submit() :
这两个方法都是立即提交 task 给线程池中的线程,不会阻塞当前外围线程的执行。但它们还是有些区别的:
http://stackoverflow.com/questions/3929342/choose-between-executorservices-submit-and-executorservices-execute
主要有一下几点:
1. execute 只可以传 Runnable 作为参数,submit() 的参数既可以 Runnable,也可以是并发包里新增的 Callable。
2. submit 的返回值是包含了 result 的 Future,而 execute 的返回值为 void。
3. 当提交的任务在线程池中执行遇到异常时:
submit 会将抛出的异常绑定到其返回结果 Future 上,当你试图在外围线程中调用 Future.get() 时,会抛出以 Future 绑定的异常作为 cause 的 ExecutionException;
execute 则会导致运行 task 的线程的 UncaughtExceptionHandler 被执行。
引申:
当 使用 execute 提交 task 到 threadpool 中,如果发生异常,会使运行 task 的线程 terminated 掉吗? 待补。。。
关于 RejectedExecutionException:
http://stackoverflow.com/questions/8183205/what-could-cause-of-rejectedexecutionexception
http://examples.javacodegeeks.com/core-java/util/concurrent/rejectedexecutionexception/java-util-concurrent-rejectedexecutionexception-how-to-solve-rejectedexecutionexception/
抛出该异常可能有两种原因:
1. executor 已经 shuttdown 掉;
2. 线程池的 task queue 满了且所有 worker thread 都没闲着,而你恰好此时将往其中提交(使用execute(),或submit())新的task。
当 task 的执行比较耗时,且 task 又比较多致使其迅速将 task queue 占满时,第二种抛 RejectedExecutionException 的情况就极有可能发生。这时,我们就需要某种机制,来保证此种情况下线程池任然能够有条不紊正常的工作:
http://stackoverflow.com/questions/2001086/how-to-make-threadpoolexecutors-submit-method-block-if-it-is-saturated
Using Callable to Return Results From Runnables:
https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6
ReentrantReadWriteLock:
Many readers may acquire the read lock at once, as long as the write lock is free; writer may acquire the write lock only if no other lock is acquired (either read or write).
http://java.dzone.com/news/java-concurrency-read-write-lo?page=0,0
- Java_Concurrency_In_Practice.pdf (6.3 MB)
- 下载次数: 3
- Design_Principles_and_Patterns__Second_Edition_By_Doug_Lea.pdf (2.5 MB)
- 下载次数: 2
发表评论
-
J2SE Evolution
2013-04-11 15:39 1162Java 7 New Features Java SE 7 ... -
未完 Java: IO & NIO(new I/O)
2013-01-11 20:56 1994适用: event and data-driven apps ... -
未完 java设计: naming convention | 命名规范
2012-11-20 16:45 2097应该遵循的规范: 类/接口/属性名,使用名词或形容词 ... -
未完 Java: enum 枚举
2012-11-19 20:29 1767http://stackoverflow.com/que ... -
未完 Java Tips & Tricks & Notes
2012-09-12 10:00 1081Hidden Features of Java: h ... -
未完 Java Socket
2012-09-12 08:42 981Java SocketJava SocketJava Sock ... -
Java For-each Loop & Iterable | 增强型For循环和Iterable接口
2012-09-11 21:50 2007增强型For循环没什么好说的,Just see link ... -
未完 Java Collections | 容器
2012-09-06 11:35 1795Sources: http://docs.oracle.com ... -
Java object Initialization (class Instantiation) | 对象的初始化(即类的实例化)
2012-09-03 09:12 2950类实例即对象 ... -
未完Java class&interfac 's Loading, Linking and Initializing | 类与接口的加载、链接和初始化
2012-08-31 19:01 1621JVM装载一个类的时候,首先检查他有没有父类,如果有父类则装载 ... -
未完 java Static 总结
2012-08-31 18:47 1367static可以用来修饰: 字段 Fields 方法 Meth ... -
未完 JVM Runtime Data Areas & Java Memory Model | 内存分配模型 & Java数据存储
2012-08-31 18:43 1850Java虚拟机内存分配模型 需精读:Chapter 5 of ... -
Java Data Types & Literals | 数据类型 和 字面量
2012-08-30 18:12 3860Java数据类型划分: OR http:// ... -
未完 Variables 变量 (Instance/Class/Local)
2012-08-29 10:59 1647Local/Instance/Class Variables ... -
未完 Regular Expressions | 正则表达式
2011-08-25 11:43 1488Extended Regular Expression ... -
java Date(util.Date/sql.Date/sql.Timestamp/sql.Time) & Oracle DATE Type 时分秒 精度问题
2011-05-17 09:32 3894遇到的问题描述: 数据库为Oracle,其jdbc驱动为ojd ... -
Java byte code (bytecode)
2011-05-04 02:55 3843keys: bytecode, byte code, opco ... -
Java Classloading Mechanism : ClassLoader & ASM & 动态字节码增强
2011-04-21 13:29 2384Setting the class path: http:// ... -
class literal & instance.getClass() & Class.forName(String className)
2011-04-20 12:33 2294常用的几种取得Class类实例的方式: 1 class lit ... -
Java 数值计算
2011-04-20 02:44 2059java.lang.Math's floor(),ceil() ...
相关推荐
包含了java1.6 API的中文工具,能对API进行查找,查看中文描述,对API类里面的结构,构造器,方法,成员变量都能一目了然,并且打包concurrent并发包,助你在java多线程道路上一帆风顺。
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
一个高性能的Java线程库,该库是 JDK 1.5 中的 java.util.concurrent 包的补充,可用于基于并发消息机制的应用。该类库不提供远程的消息功能,其设计的宗旨是实现一个内存中的消息传递机制. 主要特点有: * All ...
并发包总结 思维导图 concurrent
00 IBM developerWorks 中国 : Java 多线程与并发编程专题 02 Java 程序中的多线程 03 编写多线程的 Java 应用程序 04 如果我是国王:关于解决 Java编程语言线程问题的建议 (2) 05 构建Java并发模型框架 (2) 06...
java.util.concurrent-多线程框架
java.util.concurrent总体概览图。 收取资源分3分。需要的同学可以下载一下。 java.util.concurrent主要包括5个部分executor,colletions,locks,atomic...该图详细的列举了并发包下面的结构,包含所有接口和具体实现类。
java.util.concurrent 包含许多线程安全、测试良好、高性能的并发构建块。不客气地说,创建 java.util.concurrent 的目的就是要实现 Collection 框架对数据结构所执行的并发操作。通过提供一组可靠的、高性能并发...
java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1)
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
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...
多线程concurrent的详细使用,为转载,里面是java类,直接拷到Eclipse中就可以运行
java并发工具包 java.util.concurrent中文版pdf
很多人学习完JavaSE/JavaEE之后想往更深入的技术进行探索,比如对大数据、分布式、高并发类的专题进行攻克时,立即遇到针对java.lang包中线程类的学习,但线程类的学习并不像JDBC一样简单,学习曲线陡峭,多弯路与...
Thread概念 Thread实现方法 Thread生命周期 Thread的安全和锁 Concurrent包(安全集合类、安全Queue) 线程阻塞机制 线程池详解(原理,实际使用) 线程的监控,分析方法 扩展数据库连接池
java并发工具包 java.util.concurrent中文版-带书签版
日本人写的js多线程库Concurrent.Thread-full-20080319.zip 日本人写的js多线程库Concurrent.Thread-full-20080319.zip 日本人写的js多线程库Concurrent.Thread-full-20080319.zip
面试 面试_Java一些常见面试题+题解之多线程开发_JavaConcurrent
NULL 博文链接:https://janeky.iteye.com/blog/769965
java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...