- 浏览: 248394 次
- 性别:
- 来自: 苏州
文章分类
- 全部博客 (289)
- java (72)
- oracle (3)
- mysql (5)
- spring (28)
- hibernate (2)
- osgi (0)
- linux (2)
- ExtJs (1)
- jvm (0)
- mybatis (7)
- 分布式 (11)
- MINA (6)
- apache+tomcat (13)
- js+htm (7)
- android (44)
- http (1)
- hbase+hdoop (0)
- memcache (13)
- search (27)
- 部署及性能 (12)
- mongoDB (2)
- 多线程 (12)
- 安全管理验证 (9)
- struts (1)
- webservice (0)
- easyUI (1)
- spring security (16)
- pattern (6)
- 算法 (2)
最新评论
-
lzh8189146:
CommonsHttpSolrServer这个类,现在是不是没 ...
CommonsHttpSolrServer -
xiaochanzi:
我按照你的方法试了下,tomcat6可以发布,但是访问任何网页 ...
基于内嵌Tomcat的应用开发 -
phoneeye:
麻烦你,如果是抄来的文章,请给出来源。谢谢
ant 两则技巧 -
neverforget:
转载不注明出处
Spring Security3.1登陆验证 替换 usernamepasswordfilter -
liang1022:
若不使用eclipse ,如何在命令行下 运行服务端程序 ?
WebService CXF学习(入门篇2):HelloWorld
java—atomic
- 博客分类:
- java
J2SE 5.0提供了一组atomic class来帮助我们简化同步处理。基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增、减、赋值(更新)操作. 比如对于++运算符AtomicInteger可以将它持有的integer 能够atomic 地递增。在需要访问两个或两个以上 atomic变量的程序代码(或者是对单一的atomic变量执行两个或两个以上的操作)通常都需要被synchronize以便两者的操作能够被当作是一个atomic的单元。
对array atomic变量来说,一次只有一个索引变量可以变动,并没有功能可以对整个array做atomic化的变动。
关于Atomic的几个方法
getAndSet() : 设置新值,返回旧值.
compareAndSet(expectedValue, newValue) : 如果当前值(current value)等于期待的值(expectedValue), 则原子地更新指定值为新值(newValue), 如果更新成功,返回true, 否则返回false, 换句话可以这样说: 将原子变量设置为新的值, 但是如果从我上次看到的这个变量之后到现在被其他线程修改了(和我期望看到的值不符), 那么更新失败
从effective java (2)中拿来的一个关于AtomicReference的一个例子:
Java代码
上面的例子比较容易懂, 不过貌似没什么价值, 而在实际的应用中, 我们一般采用下面的方式来使用atomic class:
Java代码
类似i++这样的"读-改-写"复合操作(在一个操作序列中, 后一个操作依赖前一次操作的结果), 在多线程并发处理的时候会出现问题, 因为可能一个线程修改了变量, 而另一个线程没有察觉到这样变化, 当使用原子变量之后, 则将一系列的复合操作合并为一个原子操作,从而避免这种问题, i++=>i.incrementAndGet()
原子变量只能保证对一个变量的操作是原子的, 如果有多个原子变量之间存在依赖的复合操作, 也不可能是安全的, 另外一种情况是要将更多的复合操作作为一个原子操作, 则需要使用synchronized将要作为原子操作的语句包围起来. 因为涉及到可变的共享变量(类实例成员变量)才会涉及到同步, 否则不必使用synchronized
对array atomic变量来说,一次只有一个索引变量可以变动,并没有功能可以对整个array做atomic化的变动。
关于Atomic的几个方法
getAndSet() : 设置新值,返回旧值.
compareAndSet(expectedValue, newValue) : 如果当前值(current value)等于期待的值(expectedValue), 则原子地更新指定值为新值(newValue), 如果更新成功,返回true, 否则返回false, 换句话可以这样说: 将原子变量设置为新的值, 但是如果从我上次看到的这个变量之后到现在被其他线程修改了(和我期望看到的值不符), 那么更新失败
从effective java (2)中拿来的一个关于AtomicReference的一个例子:
Java代码
- public class AtomicTest {
- private int x, y;
- private enum State {
- NEW, INITIALIZING, INITIALIZED
- };
- private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);
- public AtomicTest() {
- }
- public AtomicTest(int x, int y) {
- initialize(x, y);
- }
- private void initialize(int x, int y) {
- if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {
- throw new IllegalStateException("initialize is error");
- }
- this.x = x;
- this.y = y;
- init.set(State.INITIALIZED);
- }
- public int getX() {
- checkInit();
- return x;
- }
- public int getY() {
- checkInit();
- return y;
- }
- private void checkInit() {
- if (init.get() == State.INITIALIZED) {
- throw new IllegalStateException("uninitialized");
- }
- }
- }
- public class AtomicTest {
- private int x, y;
- private enum State {
- NEW, INITIALIZING, INITIALIZED
- };
- private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);
- public AtomicTest() {
- }
- public AtomicTest(int x, int y) {
- initialize(x, y);
- }
- private void initialize(int x, int y) {
- if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {
- throw new IllegalStateException("initialize is error");
- }
- this.x = x;
- this.y = y;
- init.set(State.INITIALIZED);
- }
- public int getX() {
- checkInit();
- return x;
- }
- public int getY() {
- checkInit();
- return y;
- }
- private void checkInit() {
- if (init.get() == State.INITIALIZED) {
- throw new IllegalStateException("uninitialized");
- }
- }
- }
- public class AtomicTest {
- private int x, y;
- private enum State {
- NEW, INITIALIZING, INITIALIZED
- };
- private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);
- public AtomicTest() {
- }
- public AtomicTest(int x, int y) {
- initialize(x, y);
- }
- private void initialize(int x, int y) {
- if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {
- throw new IllegalStateException("initialize is error");
- }
- this.x = x;
- this.y = y;
- init.set(State.INITIALIZED);
- }
- public int getX() {
- checkInit();
- return x;
- }
- public int getY() {
- checkInit();
- return y;
- }
- private void checkInit() {
- if (init.get() == State.INITIALIZED) {
- throw new IllegalStateException("uninitialized");
- }
- }
- }
- public class AtomicTest {
- private int x, y;
- private enum State {
- NEW, INITIALIZING, INITIALIZED
- };
- private final AtomicReference<State> init = new AtomicReference<State>(State.NEW);
- public AtomicTest() {
- }
- public AtomicTest(int x, int y) {
- initialize(x, y);
- }
- private void initialize(int x, int y) {
- if (!init.compareAndSet(State.NEW, State.INITIALIZING)) {
- throw new IllegalStateException("initialize is error");
- }
- this.x = x;
- this.y = y;
- init.set(State.INITIALIZED);
- }
- public int getX() {
- checkInit();
- return x;
- }
- public int getY() {
- checkInit();
- return y;
- }
- private void checkInit() {
- if (init.get() == State.INITIALIZED) {
- throw new IllegalStateException("uninitialized");
- }
- }
- }
上面的例子比较容易懂, 不过貌似没什么价值, 而在实际的应用中, 我们一般采用下面的方式来使用atomic class:
Java代码
- public class CounterTest {
- AtomicInteger counter = new AtomicInteger(0);
- public int count() {
- int result;
- boolean flag;
- do {
- result = counter.get();
- // 断点
- // 单线程下, compareAndSet返回永远为true,
- // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,
- // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值
- flag = counter.compareAndSet(result, result + 1);
- } while (!flag);
- return result;
- }
- public static void main(String[] args) {
- final CounterTest c = new CounterTest();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- }
- }
- public class CounterTest {
- AtomicInteger counter = new AtomicInteger(0);
- public int count() {
- int result;
- boolean flag;
- do {
- result = counter.get();
- // 断点
- // 单线程下, compareAndSet返回永远为true,
- // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,
- // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值
- flag = counter.compareAndSet(result, result + 1);
- } while (!flag);
- return result;
- }
- public static void main(String[] args) {
- final CounterTest c = new CounterTest();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- }
- }
- public class CounterTest {
- AtomicInteger counter = new AtomicInteger(0);
- public int count() {
- int result;
- boolean flag;
- do {
- result = counter.get();
- // 断点
- // 单线程下, compareAndSet返回永远为true,
- // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,
- // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值
- flag = counter.compareAndSet(result, result + 1);
- } while (!flag);
- return result;
- }
- public static void main(String[] args) {
- final CounterTest c = new CounterTest();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- }
- }
- public class CounterTest {
- AtomicInteger counter = new AtomicInteger(0);
- public int count() {
- int result;
- boolean flag;
- do {
- result = counter.get();
- // 断点
- // 单线程下, compareAndSet返回永远为true,
- // 多线程下, 在与result进行compare时, counter可能被其他线程set了新值, 这时需要重新再取一遍再比较,
- // 如果还是没有拿到最新的值, 则一直循环下去, 直到拿到最新的那个值
- flag = counter.compareAndSet(result, result + 1);
- } while (!flag);
- return result;
- }
- public static void main(String[] args) {
- final CounterTest c = new CounterTest();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- c.count();
- }
- }.start();
- }
- }
类似i++这样的"读-改-写"复合操作(在一个操作序列中, 后一个操作依赖前一次操作的结果), 在多线程并发处理的时候会出现问题, 因为可能一个线程修改了变量, 而另一个线程没有察觉到这样变化, 当使用原子变量之后, 则将一系列的复合操作合并为一个原子操作,从而避免这种问题, i++=>i.incrementAndGet()
原子变量只能保证对一个变量的操作是原子的, 如果有多个原子变量之间存在依赖的复合操作, 也不可能是安全的, 另外一种情况是要将更多的复合操作作为一个原子操作, 则需要使用synchronized将要作为原子操作的语句包围起来. 因为涉及到可变的共享变量(类实例成员变量)才会涉及到同步, 否则不必使用synchronized
发表评论
-
Java keytool 安全证书学习笔记
2012-08-02 14:16 748http://linliangyi2007.iteye.com ... -
java国际化
2012-07-16 14:08 374http://lavasoft.blog.51cto.com/ ... -
Java版远程控制V1.0
2012-06-17 21:37 703http://www.cnblogs.com/syxchina ... -
浅析Java中CountDownLatch用法
2012-05-16 20:57 760CountDownLatch如其所写,是一个 ... -
SMTP发送邮件
2012-04-18 09:41 724SMTP发送邮件 openkk 2011-06-0 ... -
smtp 返回代码 信息
2012-04-18 08:52 1409SMTP Server Response Cod ... -
JavaMail详解
2012-04-18 02:24 0JavaMail详解 博客分类: JAV ... -
安装Eclipse反编译插件
2012-04-17 09:34 775安装Eclipse反编译插件 博客分类: ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-04-13 08:30 636最近的机器内存又爆满了,除了新增机器内存外,还应该好好r ... -
Dijkstra算法
2012-04-11 08:00 823Dijkstra算法 博客分类: 算 ... -
java 播放音乐
2012-04-11 08:00 968java 播放音乐 博客分类: J2 ... -
Java中的native,transient,volatile和strictfp关键字
2012-04-06 08:49 700Java中的native,transient,v ... -
用ReentrantLock模拟宴会的热闹情景
2012-04-05 08:32 870用ReentrantLock模拟宴会的热闹情景 ... -
Hashmap 分析
2012-04-05 08:32 697Hashmap 博客分类: 算法 ... -
ExecutorService线程池
2012-04-05 08:32 728ExecutorService线程池 (2010 ... -
Java并发:juc Executor框架详解
2012-04-05 08:32 734Java并发:juc Executor ... -
java并发包,多线程,工具类,笔记
2012-04-11 08:00 857JDK 线程池 Executors.newCachedT ... -
利用 Spring 和 EHCache 做方法缓存处理〔转〕
2012-04-09 09:49 792利用 Spring 和 EHCache 做方法缓存处理〔 ... -
EhCache使用详细介绍
2012-04-09 09:49 1319EhCache使用详细介绍 Ehcache中不仅可 ... -
HashMap 分析
2012-04-01 08:21 1856http://www.blogjava.net ...
相关推荐
主要介绍了Java Atomic类及线程同步新机制原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了深入了解Java atomic原子类的使用方法和原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
主要介绍了Java多线程atomic包介绍及使用方法,涉及原子更新基本类型介绍及代码示例,具有一定参考价值,需要的朋友可以了解下。
主要介绍了浅谈Java中的atomic包实现原理及应用,涉及Atomic在硬件上的支持,Atomic包简介及源码分析等相关内容,具有一定借鉴价值,需要的朋友可以参考下。
无原子模拟层 嘲笑用户功能以匹配免费原子层上的功能,该功能在map_atomic_plan_cap中的map_atomic_plan_cap中定义
java线程-Atomic的含义及示例_.docx
java.util.concurrent.atomic 类的小工具包,支持在单个变量上解除锁的线程安全编程。 java.util.concurrent.locks 为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。 java.util.jar 提供读写 ...
主要介绍了Java多线程Atomic包操作原子变量与原子类详解,简单介绍了Atomic,同时涉及java.util.concurrent中的原子变量,Atomic类的作用等相关内容,具有一定参考价值,需要的朋友可以了解下。
主要给大家介绍了关于Java并发编程包中atomic的实现原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
java.util.concurrent.atomic 类的小工具包,支持在单个变量上解除锁的线程安全编程。 java.util.concurrent.locks 为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。 java.util.jar 提供读写 JAR ...
java.util.concurrent.atomic 类的小工具包,支持在单个变量上解除锁的线程安全编程。 java.util.concurrent.locks 为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。 java.util.jar 提供读写 ...
java.util.concurrent.atomic 类的小工具包,支持在单个变量上解除锁的线程安全编程。 java.util.concurrent.locks 为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。 java.util.jar 提供读写 JAR ...
The chapter on threads follows this up and discusses everything from the very basic concepts of a thread to the most advanced topics such as synchronizers, the fork/join framework, and atomic ...
ZooKeeper’s atomic broadcast protocol 翻译版
引言JavaGuide :一份涵盖大部分Java程序员所需要掌握的核心知识。star:45159,替他宣传一下子这位大佬,总结的真好!我引用这位大佬的文章,因为
旨在提供类似于Java中的java.util.concurrent.atomic中的原子数组类型。 提供以下类型: AtomicOptionRefArray –对应于 。 AtomicRefArray –具有强制默认值的AtomicOptionRefArray ,用于删除元素的可选属性。...
JAVA面试秘籍一份通向理想互联网公司的面试汇总,包括Java基础、Java并发、JVM、MySQL、...七、Atomic 原子类 八、MySQL 九、Redis 十、Spring 十一、MyBatis 十二、MQ 十三、计算机网络 十四、操作系统 十五、Dubbo
java.util.concurrent.atomic 类的小工具包,支持在单个变量上解除锁的线程安全编程。 java.util.concurrent.locks 为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。 java.util.jar 提供读写 ...
java.util.concurrent.atomic 类的小工具包,支持在单个变量上解除锁的线程安全编程。 java.util.concurrent.locks 为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。 java.util.jar 提供读写 ...