`
aahyhaa
  • 浏览: 7898 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Atomicity and volatility

    博客分类:
  • Java
 
阅读更多

Atomicity applies to "simple operations" on primitive types except for longs and doubles. 

Reading and writing primitive variables other than long and double is guaranteed to go to 

and from memory as indivisible (atomic) operations. However, the JVM is allowed to 

perform reads and writes of 64- bit quantities (long and double variables) as two separate 

32-bit operations, raising the possibility that a context switch could happen in the middle of a 

read or write, and then different tasks could see incorrect results (this is sometimes called 

word tearing, because you might see the value after only part of it has been changed). 

However, you do get atomicity (for simple assignments and returns) if you use the volatile 

keyword when defining a long or double variable (note that volatile was not working 

properly before Java SE5). Different JVMs are free to provide stronger guarantees, but you 

should not rely on platform-specific features.  

Atomic operations are thus not interruptible by the threading mechanism. Expert 

programmers can take advantage of this to write lock-free code, which does not need to be 

synchronized. But even this is an oversimplification. Sometimes, even when it seems like an 

atomic operation should be safe, it may not be. Readers of this book will typically not be able 

to pass the aforementioned Goetz Test, and will thus not be qualified to try to replace 

synchronization with atomic operations. Trying to remove synchronization is usually a sign 

of premature optimization, and will cause you a lot of trouble, probably without gaining 

much, or anything.  

On multiprocessor systems (which are now appearing in the form of multicore processors—

multiple CPUs on a single chip), visibility rather than atomicity is much more of an issue 

than on single-processor systems. Changes made by one task, even if they’re atomic in the 

sense of not being interruptible, might not be visible to other tasks (the changes might be 

temporarily stored in a local processor cache, for example), so different tasks will have a 

different view of the application’s state. The synchronization mechanism, on the other hand, 

forces changes by one task on a multiprocessor system to be visible across the application. 

Without synchronization, it’s indeterminate when changes become visible.  

The volatile keyword also ensures visibility across the application. If you declare a field to be 

volatile, this means that as soon as a write occurs for that field, all reads will see the change. 

This is true even if local caches are involved—volatile fields are immediately written through 

to main memory, and reads occur from main memory.  

It’s important to understand that atomicity and volatility are distinct concepts. An atomic 

operation on a non-volatile field will not necessarily be flushed to main memory, and so 

another task that reads that field will not necessarily see the new value. If multiple tasks are 

accessing a field, that field should be volatile; otherwise, the field should only be accessed 

via synchronization. Synchronization also causes flushing to main memory, so if a field is 

completely guarded by synchronized methods or blocks, it is not necessary to make it 

volatile.  

Any writes that a task makes will be visible to that task, so you don’t need to make a field 

volatile if it is only seen within a task.  

volatile doesn’t work when the value of a field depends on its previous value (such as 

incrementing a counter), nor does it work on fields whose values are constrained by the 

values of other fields, such as the lower and upper bound of a Range class which must 

obey the constraint lower <= upper.

 

It’s typically only safe to use volatile instead of synchronized if the class has only one 

mutable field. Again, your first choice should be to use the synchronized keyword—that’s 

the safest approach, and trying to do anything else is risky.  

What qualifies as an atomic operation? Assignment and returning the value in a field will 

usually be atomic.

分享到:
评论

相关推荐

    Thinking in Java 4th Edition

    Java SE5 and SE6 .................. 2 Java SE6 ......................................... 2 The 4th edition........................ 2 Changes .......................................... 3 Note on the ...

    Design and Architecture of CockroachDb

    If all keys affected by a logical mutation fall within the same range, atomicity and consistency are guaranteed by Raft; this is the fast commit path. Otherwise, a non-locking distributed commit ...

    数据库系统概念Database System Concept(英文第6版)文字版

    14.4 Transaction Atomicity and Durability 633 14.5 Transaction Isolation 635 14.6 Serializability 641 14.7 Transaction Isolation and Atomicity 646 14.8 Transaction Isolation Levels 648 14.9 ...

    atomicity:来自过去的经典 Mac OS。 原子锁、堆栈、受保护的堆栈、队列和受保护的队列

    来自过去的经典 Mac OS。 原子锁、堆栈、受保护的堆栈、队列和受保护的队列。 68020+ 和 PowerPC。 没有 x86 或 ARM(尽管添加起来并不难)。

    Android SQLite 介紹

    ACID Compliant (Atomicity, Consistency, Isolation, Durability) Uses dynamic, weakly types data types and syntax Uses SQL query language – Implements most of SQL-92 SQL is mostly portable between ...

    微软内部资料-SQL性能优化3

    Atomicity A transaction either commits or aborts. If a transaction commits, all of its effects remain. If it aborts, all of its effects are undone. It is an “all or nothing” operation. Consistency ...

    Transactional Memory 2nd edition - Synthesis Lectures on Computer Architecture

    (atomicity, consistency, isolation) properties of transactions provide a foundation to ensure that con- current reads and writes of shared data do not produce inconsistent or incorrect results. At a ...

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 76: Strive for failure atomicity Item 77: Don’t ignore exceptions 11 Concurrency Item 78: Synchronize access to shared mutable data Item 79: Avoid excessive synchronization Item 80: Prefer ...

    SPIN经典论文集-第11届SPIN workshop论文集

    Verifying Commit-Atomicity Using Model-Checking Cormac Flanagan 252 Analysis of Distributed Spin Applied to Industrial-Scale Models Murali Rangarajan, Samar Dajani-Brown, Kirk Schloegel, Darren Cofer ...

    C# 语言规格说明(English Edition第五版)

    5.5 Atomicity of variable references 107 6. Conversions 109 6.1 Implicit conversions 109 6.1.1 Identity conversion 109 6.1.2 Implicit numeric conversions 110 6.1.3 Implicit enumeration conversions 110...

    详述MySQL事务的实现原理

    相信大家都用过事务以及了解他的特点,如原子性(Atomicity),一致性(Consistency),隔离型(Isolation)以及持久性(Durability)等。今天想跟大家一起研究下事务内部到底是怎么实现的。本文来自搜狐,由火龙果软件Anna...

    java concurrency programming in practice

    2.2.Atomicity 13 2.3.Locking 16 2.4.GuardingStatewithLocks 19 2.5.LivenessandPerformance 20 Chapter 3. Sharing Objects 23 3.1.Visibility 23 3.2.PublicationandEscape 26 3.3.ThreadConfinement 28 3.4....

    MongoDB数据库设计.pptx

    原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)。一个支持事务(Transaction)的数据库,必须要具有这四种特性,否则在事务过程(Transaction processing)当中无法保证...

    MySQL常见面试题 .docx

    ACID:原子性(Atomicity)、一致性(Correspondence)、隔离性(Isolation)、持久性(Durability) 视图 drop、delete、truncate 索引:唯一索引、主键索引、聚集索引。 连接:左联接、右联接、完整外部联接、内连接 数据库...

    Pomelo游戏框架深度优化版quick-pomelo.zip

    高性能和可伸缩快速内存数据访问分布式架构,系统可以水平扩展 分布式 ACID 事务在分布式环境支持 ACID(Stands for Atomicity, Consistency, Isolation, Durability) 事务数据原子性和一致性保证,不会在内存留下脏...

    深入理解SQLServer 2008的锁机制

    任何关系数据库必须支持事务的ACID属性,即原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、永久性(Durability)。ACID属性确保数据库中的数据更改被正确地收集到一起,并且数据将保持在与所...

    分布式架构中的幂等性

    电商的很多业务,考虑更多的是 BASE(即Basically Available、Soft state、和Eventually consistent),而不是 ACID(Atomicity、Consistency、Isolation和 Durability)。即为了满足高负载的用户访问,我们可以容忍...

    GeoProtocol.pdf

    为了保证有效性,每个数据库事务要满足四大标准,也就是所谓的ACID 模型,即原子性(Atomicity)、一致性(Consistency)、 隔离性(Isolation)以及持久性(Durability)。 本文主要围绕原子性展开。

    分布式事务处理方案.docx

    数据库事务的几个特性:原子性(Atomicity )、一致性( Consistency )、隔离性或独立性( Isolation)和持久性(Durabilily),简称就是ACID。 从广义上来看,分布式事务其实也是事务,只是由于业务上的定义以及微服务架构...

    Hyperstore.Core:超级商店

    并发线程可以使用(ACID : Atomicity, Coherence, Isolation and Durability with adapter)事务来操作数据库 描述域的所有元素以及验证规则的可扩展元模型, 内存中多域托管, 具有自动事件通知的领域驱动设计...

Global site tag (gtag.js) - Google Analytics