正确运用synchronized和二次判断 实现多线程安全
2016-01-14 00:33
330 查看
正确运用synchronized和二次判断 实现多线程安全,做出高效二符合预期的程序,特别是多个线程跑一个对象的时候,如下图所示:
![](http://static.oschina.net/uploads/space/2013/0930/170333_kGQ3_177808.png)
测试代码如下:
特别注意if(shutdownRequested){ *部分不同的写法。
不然就会输出与逻辑不符的现象:
如:
runner—-false—-我没有关闭。。。
runner—-true—-我没有关闭。。。
runner—-true—-我关闭了=====»>
package com.xue.gang.volatiler;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class VolatileRunner{
}
原:http://my.oschina.net/u/177808/blog/165442
![](http://static.oschina.net/uploads/space/2013/0930/170333_kGQ3_177808.png)
测试代码如下:
特别注意if(shutdownRequested){ *部分不同的写法。
不然就会输出与逻辑不符的现象:
如:
runner—-false—-我没有关闭。。。
runner—-true—-我没有关闭。。。
runner—-true—-我关闭了=====»>
package com.xue.gang.volatiler;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class VolatileRunner{
public static void main(String args[]) throws InterruptedException { int size=1000; CountDownLatch countDownLatch = new CountDownLatch(size); TomRunner tomRunner = new TomRunner(false,countDownLatch,"runner"); ExecutorService executorService = Executors.newCachedThreadPool(); for(int i=1;i<=size;i++){ executorService.execute(new Thread2RunTomRunner(countDownLatch,tomRunner,i+"_号")); } countDownLatch.await(); executorService.shutdown(); //new Thread(volatileRunner).start(); } static class Thread2RunTomRunner implements Runnable{ private CountDownLatch countDownLatch; private TomRunner tomRunner; private String name; public Thread2RunTomRunner(CountDownLatch countDownLatch, TomRunner tomRunner, String name) { super(); this.countDownLatch = countDownLatch; this.tomRunner = tomRunner; this.name = name; } public void run() { System.out.println(this.name+":running..."); this.tomRunner.doWork(); System.out.println(this.name+":结束..."); this.countDownLatch.countDown(); } } static class TomRunner{ volatile boolean shutdownRequested = false; //boolean shutdownRequested = false; String name; public TomRunner(boolean shutdownRequested, CountDownLatch countDownLatch, String name) { super(); this.shutdownRequested = shutdownRequested; this.name = name; } public void shutdown() { this.shutdownRequested = true; } public void doWork() { while (true) { /** *多个线程的代码去执行: System.out.println(this.name + "----" + this.shutdownRequested +"----" +"我没有关闭。。。"); * */ /*if(shutdownRequested){ System.out.println(this.name + "----" + this.shutdownRequested +"----" +"我关闭了=====>>>"); break; }else{ System.out.println(this.name + "----" + this.shutdownRequested +"----" +"我没有关闭。。。"); shutdown(); }*/ /** * 如果没有二次判断,也会出现比较脏数据. * */ /* if(shutdownRequested){ System.out.println(this.name + "----" + this.shutdownRequested +"----" +"我关闭了=====>>>"); break; } synchronized (this) { System.out.println(this.name + "----" + this.shutdownRequested +"----" +"我没有关闭。。。"); shutdown(); }*/ /** * 加上二次判断,能够正确 * */ if(shutdownRequested){ System.out.println(this.name + "----" + this.shutdownRequested +"----" +"我关闭了=====>>>"); break; } synchronized (this) { if(!shutdownRequested){ System.out.println(this.name + "----" + this.shutdownRequested +"----" +"我没有关闭。。。"); shutdown(); } } } } }
}
原:http://my.oschina.net/u/177808/blog/165442
相关文章推荐
- JS执行顺序
- 通过网页打开App并传递参数
- 《计算机科学导论》人工智能基础知识
- nginx http跳转到https
- SurfaceView 基础用法
- linux的主机名以及修改主机名 以及 hostname命令小结
- Java 字符与字符串运算
- JVM学习之常用概念
- 解决IScroll5超链接点击无效的问题
- Xcode中的常用快捷键
- webcreated need
- HTML learning
- android138 360 小火箭
- apache安装完成之后,start出现了"the requested operation has failed !"
- 2016/01/14开始学习git:分支管理:Feature分支
- django orm总结
- 用 C# 实现带键值的优先队列
- 泡妞计划-java
- 去掉删除discuz x3.2 的-Powered by Discuz!
- Eclipse+Pydev环境下使用utf-8编码