• 【翻译七】java-同步


    Synchronization

    Threads communicate primarily by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.

    • Thread Interference describes how errors are introduced when multiple threads access shared data.
    • Memory Consistency Errors describes errors that result from inconsistent views of shared memory.
    • Synchronized Methods describes a simple idiom that can effectively prevent thread interference and memory consistency errors.
    • Implicit Locks and Synchronization describes a more general synchronization idiom, and describes how synchronization is based on implicit locks.
    • Atomic Access talks about the general idea of operations that can't be interfered with by other threads.

    译文:

    同步

    线程间的通信主要是依靠共享域和引用相同的对象。这种通信是非常高效的,但是会出现两种错误:线程冲突和访问相同内存的错误。能够阻止这些错误的工具是Synchronization.

    • 线程冲突 描述当多线程访问相同的数据时会出现的错误
    • 访问相同内存错误 共享内存引起的不一致结果的错误
    • 同步方法 能够有效的阻止线程冲突和访问相同内存错误的问题
    • 隐式锁和同步 描述了更多的同步方法,以及同步方法如何基于隐式锁实现
    • 原子性 描述了操作不能被其他线程所干扰的性质

    Thread Interference

    Consider a simple class called Counter

    class Counter {
        private int c = 0;
    
        public void increment() {
            c++;
        }
    
        public void decrement() {
            c--;
        }
    
        public int value() {
            return c;
        }
    
    }
    

    Counter is designed so that each invocation of increment will add 1 to c, and each invocation of decrement will subtract 1 from c. However, if a Counter object is referenced from multiple threads, interference between threads may prevent this from happening as expected.

    Interference happens when two operations, running in different threads, but acting on the same data, interleave. This means that the two operations consist of multiple steps, and the sequences of steps overlap.

    It might not seem possible for operations on instances of Counter to interleave, since both operations on c are single, simple statements. However, even simple statements can translate to multiple steps by the virtual machine. We won't examine the specific steps the virtual machine takes — it is enough to know that the single expression c++ can be decomposed into three steps:

    1. Retrieve the current value of c.
    2. Increment the retrieved value by 1.
    3. Store the incremented value back in c.

    The expression c-- can be decomposed the same way, except that the second step decrements instead of increments.

    Suppose Thread A invokes increment at about the same time Thread B invokes decrement. If the initial value of c is 0, their interleaved actions might follow this sequence:

    1. Thread A: Retrieve c.
    2. Thread B: Retrieve c.
    3. Thread A: Increment retrieved value; result is 1.
    4. Thread B: Decrement retrieved value; result is -1.
    5. Thread A: Store result in c; c is now 1.
    6. Thread B: Store result in c; c is now -1.

    Thread A's result is lost, overwritten by Thread B. This particular interleaving is only one possibility. Under different circumstances it might be Thread B's result that gets lost, or there could be no error at all. Because they are unpredictable, thread interference bugs can be difficult to detect and fix.

    简历: 三年国内著名软件公司Java信息系统开发经验。熟悉税务相关业务知识。一年以上互联网公司工作经验。 目标职位: Java研发工程师、大数据、推荐算法等。 目标城市: 北京,杭州,沈阳。 联系方式: 邮箱:hecuking@126.com
  • 相关阅读:
    javascript判断页面第一次加载还是刷新操作【转】
    vs 2008 不能切换到设计视图的解决办法
    sql update 触发器 获得被update的行的信息
    方便winform中的数据验证,制作一个使用正则表达式验证数据的复合控件
    求一个n选x的算法
    在html链接里执行js和使用标签事件执行的不同
    咸吃萝卜淡操心:导入xlsx文件表格新增数据
    我亲爱的你,有两副面孔:表格末尾添加新内容
    Torture:跨域访问的功臣:window.name
    那么轻,那么重:图片下载与压缩包下载
  • 原文地址:https://www.cnblogs.com/accipiter/p/3182545.html
Copyright © 2020-2023  润新知