- 浏览: 837219 次
- 性别:
- 来自: 长沙
文章分类
- 全部博客 (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 1864最近在研究表达式求值,看了下论坛推荐的JSEL和Aviator ... -
log4j日志文件保存路径的设置
2010-08-12 11:56 6732一直都是把log4j的日志文件位置设定成绝对路径,直到今天发 ... -
jsp页面突然不支持EL的问题
2010-07-12 16:11 2076转自http://www.iteye.com/topic/15 ... -
jvm监控
2010-07-08 00:49 15837一直没有做过jvm监控, 总以为要找些专门的工具才能做jvm监 ... -
JAVA从键盘读取输入信息
2010-02-28 17:58 2739import java.io.BufferedReader; ... -
JAVA UUID
2010-02-26 23:24 4445UUID(Universally Unique Identif ... -
JAVA中浮点数的运算
2010-02-24 15:30 1671问题的提出: 如果我们编译运行下面这个程序会看到什么 ... -
JAVA中控制double和float的精度
2010-02-24 15:21 7702本篇介绍了在JAVA中如何对double或者float的浮点数 ... -
JAVA的位移运算
2010-02-24 10:38 1105左移操作: x << nx可以是byte, ... -
各种排序JAVA实现
2009-12-15 17:25 1127package sort; import ... -
JDK自带VM分析工具jps,jstat,jmap,jconsole
2009-08-13 13:13 1712转自:http://jiajun.iteye.com/blog ... -
javabean属性命名难以为人所知的秘密
2009-07-09 10:46 3536原文:http://www.blogjav ... -
java properties文件读取乱码问题解决之道
2009-06-10 11:09 4774Poperties Editor 插件------ ... -
正则表达式参考
2009-05-21 01:40 958http://www.cnblogs.com/netshuai ... -
Apache Commons Logging 是如何决定使用哪个日志实现类的
2009-05-20 11:36 7617原文是:http://www.blogjava.net/Unm ... -
java keytool 常用命令
2009-05-05 17:10 5413最近在做ssl连接active dir ... -
java.sql.Date,java.sql.Time和java.sql.Timestamp
2009-02-07 13:12 9294java.sql.Date,java.sq ... -
理解Unsupported major.minor version 49.0为何错误
2008-09-09 11:33 2664今天一位同事在发布一个portlet到ibm portal时报 ... -
关于Java异常抛出和处理的思考
2008-07-30 17:38 8002看过不少关于java异常处 ... -
反射私有方法
2008-07-30 10:54 1760曾经碰到一件怪事:Spring可以把构造函数为私有的对象实例也 ...
相关推荐
Java JDK 5是Java开发工具包的一个重要版本,...在"example"这个文件中,可能包含了这些概念的实例代码和解释,对于初学者来说,通过阅读这些代码和笔记,能够更好地理解和掌握Java JDK 5的新特性,从而提升编程技能。
在并发编程方面,JDK 8对`java.util.concurrent`包进行了增强,添加了新的并发工具类,如`CompletableFuture`,这是一个强大的异步编程工具,支持组合多个异步操作,以及处理异常和回调。`ForkJoinPool`和`...
压缩包中的`jdk-1_5_0_22-windows-amd64.exe`是安装程序,用户可以通过执行这个文件来安装JDK 1.5.0_22。安装过程中,系统会自动配置环境变量,包括`JAVA_HOME`、`PATH`和`CLASSPATH`,使得用户可以在命令行中直接...
- CMS(Concurrent Mark Sweep)、G1、ZGC和Shenandoah都是JDK提供的不同垃圾收集器,针对不同场景优化内存管理。 8. 社区支持: - Java有庞大的开发者社区,提供各种教程、框架和库,如Spring、Hibernate、...
9. **多线程编程**:JDK11中的`java.util.concurrent`包提供了丰富的并发工具类,源码中可以看到这些类的实现细节,有助于理解并发原理。 10. **类加载机制**:JDK11的类加载机制仍然遵循“双亲委派模型”,源码中...
6. **改进的并发工具**: 如Fork/Join框架,用于并行执行任务,以及`java.util.concurrent`包中新增的`CompletableFuture`类,方便处理异步计算结果。 7. **try-with-resources**: 这个新特性确保了资源(如文件、...
3. **并发库**:包括`java.util.concurrent`包中的线程池、锁、同步器等组件的优化,提升了并发性能和易用性。 4. **日期与时间API**:`java.time`包中的类如`LocalDate`, `LocalTime`, `LocalDateTime`等得到了...
5. **动态代理增强**:JDK 1.6增强了`java.lang.reflect.Proxy`,使得动态代理类可以处理桥接方法,提高了接口实现的灵活性。 6. **改进的内存管理**:JVM在1.6版本中对垃圾收集进行了优化,如使用CMS(Concurrent ...
5. **本地链接器**:`java.lang.ProcessHandle`接口扩展了新的方法,如`info()`,允许获取进程的详细信息,而`Linker`接口则提供了加载自定义库的能力。 6. **强引用集合**:`java.util.concurrent`包新增了`...
5. **NIO.2**(New IO 2.0):虽然完整的NIO.2在JDK7中推出,但JDK1.6已经引入了部分改进,如文件通道(FileChannel)和异步文件操作。 6. **增强的for循环(Enhanced For Loop)**:也称为foreach循环,简化了遍历数组和...
5. **安全管理**:JDK 1.6的安全模型也有了显著增强,`java.security`包提供了安全策略、权限管理等工具,以确保代码在运行时的安全性。此外,`javax.net.ssl`包提供了SSL/TLS协议的支持,用于加密网络通信,保护...
5. **删除CMS垃圾收集器 (JEP 338)**:为了简化JDK的内存管理,JDK 11移除了Concurrent Mark Sweep (CMS) 垃圾收集器,推荐使用G1或ZGC。 6. **改进的JNI签名 (JEP 330)**:更新了JNI头文件生成器,允许在JNI函数...
11. **并发编程改进**:`java.util.concurrent`包提供了线程池、并发集合和同步工具类,如`ExecutorService`、`Future`和`Semaphore`,帮助开发者更方便地编写并发程序。 以上是Java JDK 5.0的关键特性,这些改进...
《深入解析JDK6 API中文版》 JDK(Java Development Kit)是Oracle公司发布的用于开发Java应用程序的软件包,其中包含了...无论你是初学者还是经验丰富的程序员,深入研读JDK6 API都将对你的Java学习和实践大有裨益。
5. **改进的JDBC**:JDBC 4.0作为Java SE 6的一部分,增加了自动连接管理和关闭,以及对数据库元数据的新查询,提升了数据库操作的便利性。 6. **增强的安全性**:JDK 1.6增强了安全模型,包括对证书和密钥管理的...
**正文** Java Development Kit(JDK)是Oracle公司提供的用于开发和运行Java应用程序的重要工具集。...无论你是初学者还是经验丰富的开发者,掌握JDK 1.7都将有助于你更好地理解和利用Java语言。
### JDK 7 下载及其相关知识点 #### 一、JDK 7 概述 ...无论是对于初学者还是经验丰富的开发者来说,掌握JDK 7 的使用都是十分必要的。通过本文介绍的相关知识点,希望能够帮助大家更好地理解和使用JDK 7。
Java开发工具包(Java Development Kit,简称JDK)是Java编程语言的核心组件,它提供了编译、调试和运行Java程序所需...无论是初学者还是经验丰富的开发者,都应定期查阅和学习JDK API,以保持对最新技术的掌握和理解。
《深入解析JDK7源码》 JDK7(Java Development Kit 7)是Java编程语言的一个重要版本,它的发布带来了许多新特性和改进,极大地提升...无论是初学者还是资深开发者,都应该将阅读JDK源码作为提升自我能力的重要途径。
在Windows 32位系统上安装和使用JDK 1.5对于初学者和专业开发者来说都是至关重要的,因为它是编写、编译、调试以及运行Java应用程序的基础。 JDK 1.5的一些主要特性包括: 1. **泛型(Generics)**:泛型是Java 5...