- 浏览: 831611 次
- 性别:
- 来自: 长沙
文章分类
- 全部博客 (149)
- ubuntu (27)
- spring (3)
- hibernate (6)
- struts (1)
- jquery (11)
- ext (2)
- ajax (1)
- java (24)
- 设计 (4)
- db (10)
- web (10)
- 软件应用技巧 (6)
- others (2)
- 设计模式 (1)
- subversion (5)
- javascript (2)
- jpa (1)
- windows (6)
- jboss (1)
- junit (2)
- WebSphere (2)
- log4j (2)
- 新闻和感想 (0)
- ldap (3)
- tomcat (6)
- excel (1)
- PDF (1)
- xml (1)
- json (2)
- 正则表达式 (3)
最新评论
-
sunshine_love:
谢啦
svn:ignore 的用处 -
亮0000仔:
非常好。。
JAVA中浮点数的存储 -
u011840397:
你好,备份和还原到指定的目录的参数如何加呢?
svn备份和还原 -
zhglhy:
简单明了,学习了,感谢分享!
Jboss数据源密码加密 -
wmengmeng66:
写的很好,顶
spring中配置proxool数据源
由于上次发现了ReentrantLock,同步的另外一种实现,可提供更好的性能和吞吐率,这么一样好东西。
从包名来看又是java.util.concurrent ,看来这个concurrent 真的不简单啊。于是想学习学习
concurrent。concurrent的中文意思是并行,估计就是sun特别为了并发情况、多线程做的增强和修补。对哪些东西增强了呢?
先看看java.util.concurrent以及子包中的东西
1)java.util.concurrent,concurrent 首先增强的是Collection;
2)java.util.concurrent.locks,ReentrantLock实际是在它里面,locks顾名思义, 提供的就是锁。
3)java.util.concurrent.atomic,这个比较有意思, 里面都是对Integer、Long,以及它们的数组的一个原子化操作的封装类, 什么是原子操作?实际上就是最小的单元操作, 该操作是线程安全的,保证了数据的原子性和可见性。
主要关注一下1)看看
java.util.concurrent为Collection带了些什么,这里是接口列表http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-tree.html
看到interface列表,主要就是BlockingQueue,Executor,ConcurrentMap
a)ConcurrentMap,A Map providing additional atomic putIfAbsent, remove, and replace methods. 看来也是提供了对Map的原子操作,同样是加强了线程安全。
b)BlockingQueue,
BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation. So it is possible, for example, for addAll(c) to fail (throwing an exception) after adding only some of the elements in c.
#BlockingQueue 的实现都是线程安全的,里面的方法保障了并发情况下的原子操作,但bulk Collection operations是例外,因为没有必要用原子操作。
A BlockingQueue does not intrinsically support any kind of "close" or "shutdown" operation to indicate that no more items will be added. The needs and usage of such features tend to be implementation-dependent. For example, a common tactic is for producers to insert special end-of-stream or poison objects, that are interpreted accordingly when taken by consumers.
#BlockingQueue本质上并没有提供close和shutdown操作,即使当队列已经满无法添加新元素。
Usage example, based on a typical producer-consumer scenario. Note that a BlockingQueue can safely be used with multiple producers and multiple consumers.
#以下是一个示例,表明BlockingQueue 可以在多线程环境下安全的表现生产-消费模型。
c)Executor
看出来了,Executor实际上是对线程Runnable 的封装,提供了更多功能。
查看Executor的继承接口ExecutorService可以了解到多了哪些功能:
从方法来看,主要有shutdown、invokeAll、invokeAny、submit,
另外还需要特别注意一下Future和Callable,Future表示线程运行状况和结果,Callable是类似Runable的另外一种线程接口, 却在运行时可以带返回值,这个返回值就是Future:
Callable:
A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call.
The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.
Future:
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled. If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future<?> and return null as a result of the underlying task.
比较实用的还是ScheduledExecutorService,跟以前的Timer类似,也是用作线程的定时任务,特别的是通过Executors.newScheduledThreadPool可以将ScheduledExecutorService用线程池缓存起来。
从包名来看又是java.util.concurrent ,看来这个concurrent 真的不简单啊。于是想学习学习
concurrent。concurrent的中文意思是并行,估计就是sun特别为了并发情况、多线程做的增强和修补。对哪些东西增强了呢?
先看看java.util.concurrent以及子包中的东西
1)java.util.concurrent,concurrent 首先增强的是Collection;
2)java.util.concurrent.locks,ReentrantLock实际是在它里面,locks顾名思义, 提供的就是锁。
3)java.util.concurrent.atomic,这个比较有意思, 里面都是对Integer、Long,以及它们的数组的一个原子化操作的封装类, 什么是原子操作?实际上就是最小的单元操作, 该操作是线程安全的,保证了数据的原子性和可见性。
主要关注一下1)看看
java.util.concurrent为Collection带了些什么,这里是接口列表http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-tree.html
看到interface列表,主要就是BlockingQueue,Executor,ConcurrentMap
a)ConcurrentMap,A Map providing additional atomic putIfAbsent, remove, and replace methods. 看来也是提供了对Map的原子操作,同样是加强了线程安全。
b)BlockingQueue,
引用
BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation. So it is possible, for example, for addAll(c) to fail (throwing an exception) after adding only some of the elements in c.
#BlockingQueue 的实现都是线程安全的,里面的方法保障了并发情况下的原子操作,但bulk Collection operations是例外,因为没有必要用原子操作。
A BlockingQueue does not intrinsically support any kind of "close" or "shutdown" operation to indicate that no more items will be added. The needs and usage of such features tend to be implementation-dependent. For example, a common tactic is for producers to insert special end-of-stream or poison objects, that are interpreted accordingly when taken by consumers.
#BlockingQueue本质上并没有提供close和shutdown操作,即使当队列已经满无法添加新元素。
Usage example, based on a typical producer-consumer scenario. Note that a BlockingQueue can safely be used with multiple producers and multiple consumers.
#以下是一个示例,表明BlockingQueue 可以在多线程环境下安全的表现生产-消费模型。
class Producer implements Runnable { private final BlockingQueue queue; Producer(BlockingQueue q) { queue = q; } public void run() { try { while(true) { queue.put(produce()); } } catch (InterruptedException ex) { ... handle ...} } Object produce() { ... } } class Consumer implements Runnable { private final BlockingQueue queue; Consumer(BlockingQueue q) { queue = q; } public void run() { try { while(true) { consume(queue.take()); } } catch (InterruptedException ex) { ... handle ...} } void consume(Object x) { ... } } class Setup { void main() { BlockingQueue q = new SomeQueueImplementation(); Producer p = new Producer(q); Consumer c1 = new Consumer(q); Consumer c2 = new Consumer(q); new Thread(p).start(); new Thread(c1).start(); new Thread(c2).start(); } }
c)Executor
引用
An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.
看出来了,Executor实际上是对线程Runnable 的封装,提供了更多功能。
查看Executor的继承接口ExecutorService可以了解到多了哪些功能:
引用
An ExecutorService can be shut down, which will cause it to stop accepting new tasks. After being shut down, the executor will eventually terminate, at which point no tasks are actively executing, no tasks are awaiting execution, and no new tasks can be submitted.
Method submit extends base method Executor.execute(java.lang.Runnable) by creating and returning a Future that can be used to cancel execution and/or wait for completion. Methods invokeAny and invokeAll perform the most commonly useful forms of bulk execution, executing a collection of tasks and then waiting for at least one, or all, to complete. (Class ExecutorCompletionService can be used to write customized variants of these methods.)
Method submit extends base method Executor.execute(java.lang.Runnable) by creating and returning a Future that can be used to cancel execution and/or wait for completion. Methods invokeAny and invokeAll perform the most commonly useful forms of bulk execution, executing a collection of tasks and then waiting for at least one, or all, to complete. (Class ExecutorCompletionService can be used to write customized variants of these methods.)
从方法来看,主要有shutdown、invokeAll、invokeAny、submit,
另外还需要特别注意一下Future和Callable,Future表示线程运行状况和结果,Callable是类似Runable的另外一种线程接口, 却在运行时可以带返回值,这个返回值就是Future:
引用
Callable:
A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call.
The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.
Future:
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled. If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future<?> and return null as a result of the underlying task.
比较实用的还是ScheduledExecutorService,跟以前的Timer类似,也是用作线程的定时任务,特别的是通过Executors.newScheduledThreadPool可以将ScheduledExecutorService用线程池缓存起来。
import static java.util.concurrent.TimeUnit.*; class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beepForAnHour() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 60 * 60, SECONDS); } }
发表评论
-
运算符优先级
2010-11-04 14:28 1813最近在研究表达式求值,看了下论坛推荐的JSEL和Aviator ... -
log4j日志文件保存路径的设置
2010-08-12 11:56 6688一直都是把log4j的日志文件位置设定成绝对路径,直到今天发 ... -
jsp页面突然不支持EL的问题
2010-07-12 16:11 2033转自http://www.iteye.com/topic/15 ... -
jvm监控
2010-07-08 00:49 15791一直没有做过jvm监控, 总以为要找些专门的工具才能做jvm监 ... -
JAVA从键盘读取输入信息
2010-02-28 17:58 2704import java.io.BufferedReader; ... -
JAVA UUID
2010-02-26 23:24 4407UUID(Universally Unique Identif ... -
JAVA中浮点数的运算
2010-02-24 15:30 1621问题的提出: 如果我们编译运行下面这个程序会看到什么 ... -
JAVA中控制double和float的精度
2010-02-24 15:21 7664本篇介绍了在JAVA中如何对double或者float的浮点数 ... -
JAVA的位移运算
2010-02-24 10:38 1074左移操作: x << nx可以是byte, ... -
各种排序JAVA实现
2009-12-15 17:25 1094package sort; import ... -
JDK自带VM分析工具jps,jstat,jmap,jconsole
2009-08-13 13:13 1669转自:http://jiajun.iteye.com/blog ... -
javabean属性命名难以为人所知的秘密
2009-07-09 10:46 3497原文:http://www.blogjav ... -
java properties文件读取乱码问题解决之道
2009-06-10 11:09 4729Poperties Editor 插件------ ... -
正则表达式参考
2009-05-21 01:40 928http://www.cnblogs.com/netshuai ... -
Apache Commons Logging 是如何决定使用哪个日志实现类的
2009-05-20 11:36 7567原文是:http://www.blogjava.net/Unm ... -
java keytool 常用命令
2009-05-05 17:10 5377最近在做ssl连接active dir ... -
java.sql.Date,java.sql.Time和java.sql.Timestamp
2009-02-07 13:12 9246java.sql.Date,java.sq ... -
理解Unsupported major.minor version 49.0为何错误
2008-09-09 11:33 2627今天一位同事在发布一个portlet到ibm portal时报 ... -
关于Java异常抛出和处理的思考
2008-07-30 17:38 7971看过不少关于java异常处 ... -
反射私有方法
2008-07-30 10:54 1733曾经碰到一件怪事:Spring可以把构造函数为私有的对象实例也 ...
相关推荐
NULL 博文链接:https://wensiqun.iteye.com/blog/1226770
JDK-并行深入学习 开始从原始二进制研究Concurrent包,以及其他并发相关的包,对于途中遇到的不明白的东西,也会作相应了解。 该项目的缘由是看CopyOnWriteArrayList二进制时,对Unsafe类一无所知,才有了好钻钻一番...
JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的...这是jdk10版本,适合初学者使用,还需要哪个版本或者缺少哪个软件可以私信我,尽量满足
包含了java1.6 API的中文工具,能对API进行查找,查看中文描述,对API类里面的结构,构造器,方法,成员变量都能一目了然,并且打包concurrent并发包,助你在java多线程道路上一帆风顺。
JDK 5中文文档PDF文档JDK 5中文文档PDF文档JDK 5中文文档PDF文档JDK 5中文文档PDF文档
JDK 5 0 zip 欢迎下载 JDK 5 0 zip 欢迎下载 JDK 5 0 zip 欢迎下载
jdk5 win7 64位下载jdk5 win7 64位下载jdk5 win7 64位下载
jdk5 win64
jdk5.zip。程序
沈东良JDK5 API 方便开发者查询察看使用
guava-jdk5
Java 2入门经典 JDK 5 JDK 5 Edition14
JDK5 64位 JDK5 64位
JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11...
JDK5_新特性,当你不知道jdk1.5较之前版本新加了哪些内容,不妨看看它
jdk5、jdk6新特性系统介绍.chm 系统、全面 本人自己整理的资料
jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助文档jdk8帮助...
对于初学者来说比较好的jdk的下载安装文档
IBM JDK5 垃圾收集策略,第 1 部分
对java多线程进行了全面的总结,包括了JDK 5的新特性。