The Java™ Tutorials — Concurrency :Thread Interference 线程冲突

The Java™ Tutorials — Concurrency :Thread Interference 线程冲突

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html

关键点

  • 线程冲突的原因:并发环境下,非原子操作所带来的结果不确定性

全文翻译

Consider a simple class called Counter

看下下面这个名为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.

Counter的设计是这样的,每次调用increment是都会对C加一,调用decrement都会为C减一。然而,如果一个Counter对象被多个线程引用,线程间的冲突就会阻止程序按照预期发展。

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.

看起来针对Counter实例的操作是不可能交错进行的,因为所有对c的操作都是单独的、简单的语句。然而,即使是简单的语句也可以被JVM翻译成多个步骤。我们不去查证JVM究竟进行了那些具体的步骤,知道一个简单的c++表达式可被分解成三步操作就足够了:

  1. 重新获取c的值
  2. 对获取的值加一
  3. 把新的值存回到c中去

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

c--表达式也按照同样的规则被分解,不过第二步要把加改为减。

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.

假设线程A调用了increment而此时线程B也调用了decrement。如果c的初值为0,它们交错进行的操作也许是按照下面的节奏进行的:

  1. 线程A:获取c的值
  2. 线程B:获取c的值
  3. 线程A:对获取到的c值加一;结果为1
  4. 线程B:对获取到的c值减一;结果为-1
  5. 线程A:将结果存回到c;c现在是1
  6. 线程B:将结果存回到c;c现在是-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.

线程A的结果丢掉了,被线程B的结果所覆盖。这个交错进行的顺序只是其中的一种可能。在不同情况下,线程B的结果可能被丢到了,或者可能根本也就不出错。由于这是不可预见的,线程冲突的缺陷很难检测与修复。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值