正确运用synchronized和二次判断 实现多线程安全
来源:互联网 发布:诺曼底公爵 知乎 编辑:程序博客网 时间:2024/05/17 11:03
正确运用synchronized和二次判断 实现多线程安全,做出高效二符合预期的程序,特别是多个线程跑一个对象的时候,如下图所示:
测试代码如下:
特别注意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
0 0
- 正确运用synchronized和二次判断 实现多线程安全
- java 多线程系列之线程安全和synchronized
- 如何正确实现多线程安全的singleton patterns
- Synchronized和线程安全
- 对Java中线程安全Synchronized的深入理解和运用
- 多线程 继承Thread类 实现Runnable接口 多线程安全 synchronized 单例设计懒汉 锁死
- 多线程runnable 和 synchronized
- 使用synchronized和volatile实现Java多线程同步
- 【Java多线程】synchronized与线程安全
- 【多线程】实例变量(synchronized)与线程安全
- Java多线程编程-(1)-线程安全和锁Synchronized概念
- Java多线程编程-(1)-线程安全和锁Synchronized概念
- Java多线程编程-(1)-线程安全和锁Synchronized概念
- Java多线程编程-(1)-线程安全和锁Synchronized概念
- 利用synchronized实现多线程同步
- JAVA线程安全之synchronized关键字的正确用法
- JAVA线程安全之synchronized关键字的正确用法
- JAVA线程安全之synchronized关键字的正确用法
- Leetcode 231. Power of Two (Easy) (cpp)
- 手机APP开发之车牌识别SDK移植
- 邮件批处理(业务流程分析)
- 获取Executor提交的并发执行的任务返回结果的两种方式/ExecutorCompletionService使用
- 图片压缩工具
- 正确运用synchronized和二次判断 实现多线程安全
- Android基础知识---EditView属性名称及描述
- 关于Linux内核学习
- UVA-1587 Box
- UE4学习笔记(一): 基本概念
- Live555 分析(一):类介绍
- Java内存区域与内存溢出异常
- 第二天JAVA回顾
- android 开发连接真机录制视频(转GIF,方便演示)