Java Concurrencyin Practice 并发编程实践系列 第二章 线程安全 Thread Safety 上

Chapter 2: Thread Safety

第二章,主要讲的是线程安全的问题,及解决方法,现在写的是如何去理解线程安全

Perhaps surprisingly, concurrent programming isn’t so much about threads or locks, any more than civil engineering is about rivets and I-beams. Of course, building bridges that don’t fall down requires the correct use of a lot of rivets and I-beams, just as building concurrent programs require the correct use of threads and locks. But these are just mechanisms—means to an end. Writing thread-safe code is, at its core, about managing access to state, and in particular to shared, mutable state.

这句话的意思是,尽管并发编程涉及到线程和锁等概念,但它的本质并不局限于这些具体的实现细节,就像土木工程并不仅仅关乎铆钉和I型钢梁一样。

这句话的目的是提醒人们,对于并发编程来说,更重要的是理解并解决并发编程所带来的挑战和问题,而不仅仅局限于线程和锁的使用。并发编程涉及到如何设计并发安全的算法、如何管理共享资源、如何处理竞争条件等等。类似地,土木工程涉及到设计建造稳固的结构、解决土地利用问题、考虑自然环境等方面。

因此,理解并发编程应该超越具体的线程和锁的细节,而是注重在整体设计和解决问题的层面上进行思考和处理。这样才能更好地应对并发编程的挑战,并开发出高效、可靠的并发程序。

threads and locks. But these are just mechanisms—means to an end.

线程和锁等并发编程的机制只是实现并发编程的手段,它们并不是目标的终点。

并发编程的目标是解决问题、提高性能、改善用户体验等。线程和锁等机制只是为了支持实现这些目标而存在,它们是一种工具,通过合理的使用来达到预期的效果。

Writing thread-safe code is, at its core, about managing access to state, and in particular to shared, mutable state.

编写线程安全的代码,在其核心是管理对状态的访问,特别是对共享的、可变的状态的访问。

在并发环境下,多个线程可能同时访问和修改共享的状态,如果没有适当的管理,就会产生数据竞争和不一致的结果。

Informally, an object’s state is its data, stored in state variables such as instance or static fields.

An object’s state may include fields from other, dependent objects;

a HashMap’s state is partially stored in the HashMap object itself, but also in many Map.Entry objects.

An object’s state encompasses any data that can affect its externally visible behavior.

(instance fields, static fields: 实例字段/实例域/实例变量, 静态字段/静态域/静态变量

有时候直接看英文原版会比看翻译会更明白一点,虽然阅读过程会比较曲折,但尽量阅读。)

object’s state指的是它的数据,存储在状态变量中,例如实例变量或静态变量。(object’s state约等于object instance fields, static fields )

对象的状态可能包括来自其他相关对象的字段;例如,HashMap的状态部分存储在HashMap对象本身中,但也存储在许多Map.Entry对象中。object’s state包括所有可能影响其对外可见行为的数据。(其实就算翻译了,还是有点看不懂的。直接举例吧。)

public class Counter {
    private int count;

    public void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

在这个例子中,计数器 object’s state 就是它的count字段的值。

该对象的行为取决于该状态,当调用increment方法时,状态会发生变化。

Counter Object:

  • State Variables: count (an integer field)
  • State: The value of the count field
  • Behavior: The increment() method increases the value of count, and the getCount() method returns the current value of count.

In this examples, the state of an object refers to its internal data stored in state variables. The behavior of the object depends on its state, and various methods can modify the state to achieve different functionality. Managing shared mutable state correctly is crucial in concurrent programming to ensure thread safety and proper concurrent operations.

By shared, we mean that a variable could be accessed by multiple threads;

by mutable, we mean that its value could change during its lifetime.

We may talk about thread safety as if it were about code, but what we are really trying to do is protect data from uncontrolled concurrent access.

Whether an object needs to be thread-safe depends on whether it will be accessed from multiple threads. This is a property of how the object is used in a program, not what it does. Making an object thread-safe requires using synchronization to coordinate access to its mutable state; failing to do so could result in data corruption and other undesirable consequences

By shared, we mean that a variable could be accessed by multiple threads;

multiple:与等于 n多个。variable:变量(变来变去的,百变怪)

by mutable, we mean that its value could change during its lifetime.

during its lifetime 在生命周期内/存活周期内等

这两句都是解释前面这个单词在本文的意思。

We may talk about thread safety as if it were about code, but what we are really trying to do is protect data from uncontrolled concurrent access.

我们讨论的线程安全性好像是关于代码的,但是我们真正要做的,是在不可控制的并发访问中保护数据

这句话是重点,编写线程安全的代码,真正要做的,其实是对object’s state的并发安全。而不是所有代码。

在工作中,分辨出哪些是常规代码,哪些是需要并发的,具体仔细到某一段逻辑或字段。

This is a property of how the object is used in a program, not what it does.

对象的状态是由其在程序中的使用方式决定的,而不是定义其目的或功能。

换句话说,对象的状态是指存储在其状态变量中的值,在对象被操作或与之交互时可以随着时间变化。状态表示对象在任何给定时刻的当前数据快照。

另一方面,对象的行为是指其可以执行的操作或方法,以及它对方法调用的响应方式。行为由对象的方法和其实现决定。

这句话强调了对象的状态是如何在程序执行中被使用、修改或访问而产生影响的,而行为则关注对象能做什么以及它在方法调用时如何响应。

总而言之,这句话强调了对象状态(由数据决定)与对象行为(由方法决定)之间的区别,并强调了对象的状态是由其在程序中的使用方式所影响的。

Making an object thread-safe requires using synchronization to coordinate access to its mutable state; failing to do so could result in data corruption and other undesirable consequences.

确保对象的线程安全性需要使用同步机制来协调对其可变状态的访问;如果不这样做,可能会导致数据损坏和其他不良后果。

当多个线程同时访问一个对象的可变状态时,如果没有适当的同步机制来保证线程间的协调,可能会发生以下问题:

  • 竞态条件(Race condition):多个线程同时修改对象的状态,导致结果依赖于执行的顺序,可能产生不确定的结果。
  • 数据不一致(Data inconsistency):多个线程同时读取和修改对象的状态,导致数据出现错误或不一致的情况。
  • 内存可见性问题(Memory visibility problem):多个线程在各自的缓存中保存对象的副本,没有及时将修改后的值刷新到主内存,导致其他线程无法看到最新的状态。

没完待续……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小大凳子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值