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.

译文:
线程冲突
看一个简单的计数的类:
 class Counter {
private int c = 0; public void increment() {
c++;
} public void decrement() {
c--;
} public int value() {
return c;
} }

Counter类被设计成为,每次调用increment方法的时候,使c增加1;每次调用decrement方法的时候,使c减去1.但是,如果Counter对象被多线程引用了,那么线程间的冲突可能阻止这种期待的方法执行。

当两个操作,运行在不同的线程中,但是访问相同的代码,交叉运行,那么就会发生线程冲突。这意味着这两个操作有很多步骤,并且这些步骤中有重叠的地方。由于两个操作操作c的语句都是简单的单语句,这似乎不太可能引起Counter实例在执行过程中的交叉。然而,即使是简单的步骤也会被虚拟机转换为复杂的步骤。我们不太可能检查虚拟机指定的步骤,但是C++可以被分解为三个步骤:

1.获得当前c的值

2.对当前的值加1

3.将增加后的值赋给原来的c

对于c--可以分解为相同的步骤,除了第二步的操作是做减操作外。

假设线程A执行加操作同时线程B执行减操作。如果c的初始值为0,那么执行的序列如下:

1.线程A:获取c

2.线程B:获取c

3.线程A:增加获得的值,结果为1

4.线程B:减少获得的值,结果为-1

5.线程A:存储结果到c,c现在是1

6.线程A:存储结果到c,c现在是-1

线程A的结果丢失了,被线程B的结果重写了。这种实际的交叉是其中的一种可能。在不同的环境中,可能是线程B的值丢失,也可能不会出现问题。由于这种问题是不可预知的,因此线程冲突的bug是很难察觉和修复的。

养眼是必须滴^^

【翻译七】java-同步的更多相关文章

  1. 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10

    关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...

  2. 监视锁——Java同步的基本思想

    翻译人员: 铁锚翻译时间: 2013年11月13日原文链接: Monitors – The Basic Idea of Java synchronization如果你上过操作系统课程,你就知道监视锁( ...

  3. 锁——Java同步的基本思想

    翻译人员: 铁锚 翻译时间: 2013年11月13日 原文链接:  Monitors – The Basic Idea of Java synchronization 如果你上过操作系统课程,你就知道 ...

  4. 把Scheme翻译成Java和C++的工具

    一.为什么要写这个工具? 公司内容有多个项目需要同一个功能,而这些项目中,有的是用Java的,有的是用C++的,同时由于某些现实条件限制,无法所有项目都调用统一的服务接口(如:可能运行在无网络的情况下 ...

  5. 死磕 java同步系列之CyclicBarrier源码解析——有图有真相

    问题 (1)CyclicBarrier是什么? (2)CyclicBarrier具有什么特性? (3)CyclicBarrier与CountDownLatch的对比? 简介 CyclicBarrier ...

  6. 死磕 java同步系列之Phaser源码解析

    问题 (1)Phaser是什么? (2)Phaser具有哪些特性? (3)Phaser相对于CyclicBarrier和CountDownLatch的优势? 简介 Phaser,翻译为阶段,它适用于这 ...

  7. 死磕 java同步系列之ReentrantLock源码解析(一)——公平锁、非公平锁

    问题 (1)重入锁是什么? (2)ReentrantLock如何实现重入锁? (3)ReentrantLock为什么默认是非公平模式? (4)ReentrantLock除了可重入还有哪些特性? 简介 ...

  8. Java同步块

    原文:http://ifeve.com/synchronized-blocks/ Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本 ...

  9. 探索 Java 同步机制[Monitor Object 并发模式在 Java 同步机制中的实现]

    探索 Java 同步机制[Monitor Object 并发模式在 Java 同步机制中的实现] https://www.ibm.com/developerworks/cn/java/j-lo-syn ...

  10. Java同步块(synchronized block)使用详解

    Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本文介绍以下内容: Java同步关键字(synchronzied) 实例方法同步 静 ...

随机推荐

  1. 【原创】ReFlux细说

    ReFlux细说 Flux作为一种应用架构(application architecture)或是设计模式(pattern),阐述的是单向数据流(a unidirectional data flow) ...

  2. phpcms评论框iframe无法自适应问题

    问题背景: 之前用友言的时候改过网站的ip地址,改成127开头的了.但是协同开发的时候别人用的还是localhost. 结果在用评论的时候iframe死活不能自适应,看了一下源代码v9本身已经写过if ...

  3. Asp.net MVC网站的基本结构

    Asp.net MVC网站的基本结构:Controller->IBLL(定义业务接口)->BLL->DA(调用Utility)->数据库 ,其中A->B表示A调用B 1. ...

  4. javascript特殊运算符

    in运算符                 in运算符要求其左边的运算数是一个字符串,或可以被转换为字符串,右边的运算数十一个对象或数组.如果该 运算符左边的值是右边对象的一个属性名,则返回true, ...

  5. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  6. uniq 重复行统计

    uniq 命令   文字 uniq 是LINUX命令 用途 报告或删除文件中重复的行. 语法 uniq [ -c | -d | -u ] [ -f Fields ] [ -s Characters ] ...

  7. c++ 引用和指针

    1.引用 程序把引用和它的初始值绑定在一起,而不是将初始值拷贝给引用.一旦初始化完成,引用将和它的初始值对象一直绑定在一起.因为无法令引用重新绑定到另外一个对象,因此引用必须初始化. int ival ...

  8. ACM/ICPC 之 网络流入门-Ford Fulkerson与SAP算法(POJ1149-POJ1273)

    第一题:按顾客访问猪圈的顺序依次构图(顾客为结点),汇点->第一个顾客->第二个顾客->...->汇点 //第一道网络流 //Ford-Fulkerson //Time:47M ...

  9. ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)

    这一题是练习广度优先搜索很好的例题,在很多广搜教学中经常用到,放在这里供学习搜索算法的孩纸们看看= = 题目大意:一维数轴上,农夫在N点,牛在K点,假定牛不会移动,农夫要找到这头牛只能够进行以下三种移 ...

  10. python之集合,函数,装饰器

    本节主要内容如下: 1. set集合 2. 函数 -- 自定义函数 -- 内置函数 3. 装饰器 一. set 集合: 一个无序且不重复的序列. tuple算是list和str的杂合(杂交的都有自己的 ...