volatile

volatile和synchronized区别

你。 提交于 2019-12-02 05:47:27
volatile是Java提供的一种轻量级的同步机制,在并发编程中,它也扮演着比较重要的角色。同synchronized相比(synchronized通常称为重量级锁),volatile更轻量级,相比使用synchronized所带来的庞大开销,倘若能恰当的合理的使用volatile,自然是美事一桩。 为了能比较清晰彻底的理解volatile,我们一步一步来分析。首先来看看如下代码 public class TestVolatile { boolean status = false; /** * 状态切换为true */ public void changeStatus(){ status = true; } /** * 若状态为true,则running。 */ public void run(String t){ if(status){ System.out.println("running...." + t); } } } 上面这个例子,在多线程环境里,假设线程A执行changeStatus()方法后,线程B运行run()方法,可以保证输出"running....."吗?   答案是NO!   这个结论会让人有些疑惑,可以理解。因为倘若在单线程模型里,先运行changeStatus方法,再执行run方法,自然是可以正确输出"running...."的;但是在多线程模型中

单例模式

徘徊边缘 提交于 2019-12-02 05:36:43
/** * 饿汉式 在类加载时实现初始化,浪费内存 * 线程安全 * 用于访问量大,多线程访问 * */public class Utils{ private static final Utils instanch=new Utils(); private Utils() { } public static Utils getInstance (){ return instanch; }} /** * 懒汉式 在调用时实现初始化 * 非线程安全 * 用于访问量少,单线程访问,多线程时可能会出现实例化多个对象 * eg:线程A,B同时执行到if (instance == null) { 中会有多个实例 */public class Utils { private static Utils instance; private Utils() { } public static Utils getInstance() { if (instance == null) { instance = new Utils(); } return instance; }} /** * 懒汉式优化(DCL式) 在调用时实现初始化 * 非线程安全 * 用于访问量少,单线程访问,多线程时可能会出现实例化多个对象 * synchronized: 只允许一个线程进入特定代码段,从而避免多线程同时修改同一数据

多线程volatile关键字

扶醉桌前 提交于 2019-12-02 05:10:47
编译优化 我们都知道,所有的高级程序设计语言所编写的源代码,都要经过编译系统或解释系统的翻译,转换为计算机硬件系统能够识别的机器语言代码,才能最终在计算机上执行。 而现代的编译或解释软件都很强大,很智能,它们会尽可能选择能让我们的程序以最高效率的形式工作,即,它们会尽可能地“优化”我们的代码,使得最终编译或解释出的机器语言代码与我们的源代码有所差异! 这里要涉及计算机存储体系的概念。 计算机存储体系 计算机体统有包括外存、内存等不同的存储层次,相对完整地说。计算机存储体系从“外”到“内”分为5层: 海量外存——存储空间最大,速度最慢; 外存——存储空间大,速度也比较慢; 内存——存储空间不是很大,速度却很快; 高速缓存——存储空间小得多,速度更快; 寄存器——存储空间最小,速度最快; 其实,寄存器已经是CPU的范畴了,它们是CPU不可或缺的组成部分。 在上述存储方式中,最快的是寄存器(组),是CPU指令访问的常客,但是,存储容量非常的少:内存是计算机指令与数据存储的最主要的空间,CPU可以访问内存,但与寄存器比较,对内存的访问速度要慢很多很多。 我们所编写的程序中,变量、数组的本质就是内存空间(无论是系统堆栈还是系统堆),对变量、数组元素的访问,就是对内存的访问。 对于像循环中的控制量 for ( int i = 0 ; i < 10 ; i ++ ) 中的变量i

pcDuino 硬件LED驱动实战

别说谁变了你拦得住时间么 提交于 2019-12-02 04:00:58
最近调驱动时,调试led时遇到了点问题,于是回过头来再写个led裸板程序。在我写的pcDuino第一个裸板程序uart的基础上,再写个led裸板程序还是很轻松的。很多人觉得没有必要写什么pcDuino裸板程序,觉得没啥意义。我觉得可以用来熟悉硬件,特别是想做底层驱动开发,以及系统移植,熟悉底层硬件还是有用的。其实做底层驱动开发,也是跟硬件打交道,硬件相关的操作和裸板程序是一样的。下面介绍怎样在pcDuino上跑一个最简单的led裸板程序。 开发环境: 宿主机:ubuntu 12.04 64位 目标机:pcDuino V2 编译器:arm-linux-gnueabihf-gcc (4.6) 目标:实现pcDuino上的TX_LED闪烁 文档说明: 命令提示符 $ 表示在pcDuino上面运行的指令; 命令提示符 # 表示在x86_64的linux主机上运行的指令 命令提示符 > 表示在u-boot状态下运行的指令 仔细看pcDuino上的原理图和pcDuino的手册,发现二者不是完全对应的,还是以原理图为准。根据原理图知道TX_LED是接到PH15上,可以当做普通IO口用,不需要连跳线 主要是看手册30.Port Controller,根据手册写led初始化程序主要包括设为输出、是能上拉及Multi-Driving寄存器设置。包括start.S、main.c、clock.c

Why volatile is not working properly

本小妞迷上赌 提交于 2019-12-02 03:48:18
问题 Today I was creating one timeout job using TimerTask but fell in to a new problem where i have a static volatile boolean variable flag . My understanding is as soon as value of this variable get changed it is notified by all running thread . But when I ran this program I got below output which is not acceptable. O/P: -------------- -------------- DD BB Exiting process.. CC My expectation is my last print should be Exiting process.. Why is this strange behavior? My code is: public class

Should I volatile the field with synchronized methods?

大憨熊 提交于 2019-12-02 03:36:00
问题 With following class, // This class should be thread-safe!!! class BankAccount { private long balance; // Should it be volatile? synchronized void deposit(long amount) { // ... balance += amount; } synchronized void withdraw(long amount) { // ... balance -= amount; } } Should I add volatile to balance field? 回答1: No, compared with synchronized keyword, volatile is lightweight. volatile can gurantee the reader thread always get fresh balance value, but it can not make balance += amount; atomic

Inline Assembler for wrapper function doesn't work for some reason

五迷三道 提交于 2019-12-02 03:02:14
I'm trying to write a wrapper function for read() system call , using asm volatile , but it won't work , since the res doesn't change its value . Here's the code : ssize_t my_read(int fd, void *buf, size_t count) { ssize_t res; __asm__ volatile( "int $0x80" /* make the request to the OS */ : "=a" (res), /* return result in eax ("a") */ "+b" (fd), /* pass arg1 in ebx ("b") */ "+c" (buf), /* pass arg2 in ecx ("c") */ "+d" (count) /* pass arg3 in edx ("d") */ : "a" (5) /* passing the system call for read to %eax , with call number 5 */ : "memory", "cc"); /* announce to the compiler that the

Atomic long 和long的区别

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 02:28:10
Atomic long 和long:   前者是一个对象,包含对象头(object header)以用来保存hashcode、lock等信息,32位系统占用8个字节,64位系统占16个字节,所以在64位系统的情况下:    * volatile long = 8 bytes   *AtomicLong = 8 bytes (volatile long) + 16bytes(对象头) + 8bytes(引用) = 32bytes 至少节约了24字节! 结论: Atomic*objects ->volatile primary type + static Atomic*FieldUpdater   《借鉴Netty源码设计》 来源: https://www.cnblogs.com/-qilin/p/11725861.html

Should I volatile the field with synchronized methods?

♀尐吖头ヾ 提交于 2019-12-02 02:18:33
With following class, // This class should be thread-safe!!! class BankAccount { private long balance; // Should it be volatile? synchronized void deposit(long amount) { // ... balance += amount; } synchronized void withdraw(long amount) { // ... balance -= amount; } } Should I add volatile to balance field? No, compared with synchronized keyword, volatile is lightweight. volatile can gurantee the reader thread always get fresh balance value, but it can not make balance += amount; atomic. synchronized can do both. You don’t need volatile in the code shown, because the variable is used only

Redis 系列(04-2)Redis原理 - 内存回收

时间秒杀一切 提交于 2019-12-02 01:35:59
目录 Redis 系列(04-2)Redis原理 - 内存回收 Redis 系列目录 1. 过期策略 1.1 定时过期(主动淘汰) 1.2 惰性过期(被动淘汰) 1.3 定期过期 2. 淘汰策略 2.1 最大内存设置 2.2 淘汰策略 2.4 LFU Redis 系列(04-2)Redis原理 - 内存回收 Redis 系列目录 相关文档推荐: Redis - LRU Reids 所有的数据都是存储在内存中的,在某些情况下需要对占用的内存空间进行回收。内存回收主要分为两类,一类是 key 过期,一类是内存使用达到上限(max_memory)触发内存淘汰。 Redis 设置 key 的过期时间: expire k1 1 # 设置过期时间s expireat k1 1 # 设置过期时间,时间戳s pexpire k1 1000 # 设置过期时间ms pexpireat k1 1 # 设置过期时间,时间戳ms ttl k1 persist k1 # 取消过期时间设置 1. 过期策略 要实现 key 过期,我们有几种思路。 定期过期(主动淘汰) 惰性过期(被动淘汰) 定期过期 1.1 定时过期(主动淘汰) 每个设置过期时间的 key 都需要创建一个定时器,到过期时间就会立即清除。该策略可以立即清除过期的数据,对内存很友好;但是会占用大量的 CPU 资源去处理过期的数据