zoukankan      html  css  js  c++  java
  • Atomic Access

    Atomic Access
    原子访问
    In programming, an atomic action is one that effectively happens all at once. An atomic action cannot stop in the middle: it either happens completely, or it doesn't happen at all. No side effects of an atomic action are visible until the action is complete.
    在编程中,一个原子操作只有效地发生一次。一个原子操作不能够在中间停止:它要么完成,要么根本就没有发生。直到操作结束原子操作没有任何其他的副作用(side effects 俺理解的是原子操作就是java中最基本最小的操作,此操作不能在分解成其他的操作)
    We have already seen that an increment expression, such as c++, does not describe an atomic action. Even very simple expressions can define complex actions that can decompose into other actions. However, there are actions you can specify that are atomic:
    我们已经见过一个自加的表达式,就像c++,这不是一个原子操作。即使是一个简单的表达式能够定义为一个复杂的操作(这个操作可以分解成其他多个操作)。然而,你可以指出哪些操作时原子的

    Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
    Reads and writes are atomic for all variables declared volatile (including long and double variables).
    对变量的引用和大部分基础变量(处了long和double)的读写是原子的。对所有用volatile声明变量的读写也是原子的。

    Atomic actions cannot be interleaved, so they can be used without fear of thread interference. However, this does not eliminate all need to synchronize atomic actions, because memory consistency errors are still possible. Using volatile variables reduces the risk of memory consistency errors, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. This means that changes to a volatile variable are always visible to other threads. What's more, it also means that when a thread reads a volatile variable, it sees not just the latest change to the volatile, but also the side effects of the code that led up the change.
    原子操作不能够交错执行。所以他们的使用不必担心线程冲突。然而,这也不能排除同步原子操作的需要,因为内存一致性错误仍然可能发生。使用volatile变量降低了内存一致性操作,因为任何对volatile变量的写操作都会与随后对同一变量的读操作建立一种happen-before关系。这意味着对一个volatile变量的修改对其他线程是可见的。另外,当一个线程读一个volatile变量,它看不到对volatile变量的最新修改,这也是volatile导致这种改变的副作用


    Using simple atomic variable access is more efficient than accessing these variables through synchronized code, but requires more care by the programmer to avoid memory consistency errors. Whether the extra effort is worthwhile depends on the size and complexity of the application.
    使用简单的原子变量的访问比通过同步代码访问那些变量要更有效率,但是程序员需要特别注意避免内存一致性错误。额外的努力是否值得要依据程序的大小和复杂性
    Some of the classes in the java.util.concurrent package provide atomic methods that do not rely on synchronization. We'll discuss them in the section on High Level Concurrency Objects.
    在java.util.concurrent package中内中有一些类提供了不依赖同步的原子方法。我们将在on High Level Concurrency Objects中讨论它们

  • 相关阅读:
    android studio eclipse keymap theme 快捷键 主题风格设置
    android Observable api请求参数设置注解问题
    量化交易-外汇交易-MetaTrader5
    springboot maven项目,为什么build成功,build path也没错误,project-->clean 也没用,项目上面还是有个红x呢?
    springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法
    深入剖析Kubernetes k8s
    android搜索框列表布局,流程及主要步骤思维导图
    php 当前时间 当前时间戳和数据库里取出的时间datetime格式进行比较大小
    android studio 自动导入包
    第三方统计分析埋点工具对比,神策、Ptmind、GrowingIO、国双,还有谷歌分析,谁更好?
  • 原文地址:https://www.cnblogs.com/yuwenxing/p/2522478.html
Copyright © 2011-2022 走看看