Java Concurrent Programming Practical note-Thread safety

Source: Internet
Author: User
What is thread safety
线程安全性定义中最核心的概念就是:**正确性**。我们将单线程的正确性近似定义为“所见即所知”,当多个线程访问这个类的时候,始终能表现出正确的行为,那么这个类就是线程安全类。当多个线程访问某个类时,不管运行时环境采用什么调度方式或者这些线程将如何交替运行,并且调用代码时,不需要额外的同步,就可以产生正确的结果。这个类就是线程安全类。在线程安全类上执行任何串行或者并行的操作都不会使对象处于无效状态。可重入代码:就是这段代码,和其他代码不存在共享状态,只包含形参和局部变量,肯定是线程安全的,又被称为无状态。
Race condition
当计算的正确性取决于多个线程执行的顺序时,就会发生竞态条件。常见的竞态条件就是先检查后执行操作。延迟加载中,getInstance中先判断是否为空,然后在创建实例返回。还有另一种常见的:读取-修改-写回竞态条件。复合操作。只要不是原子操作,就会有问题。可通过加锁变成原子操作。只要有竞态条件,就不是线程安全的。
Protect status with lock
如果要使用同步来协调对一个变量的访问,那么在访问这个变量的所有位置上都要使用同步。并且使用锁来协调的时候,还必须使用同一个锁。每个可变的或者共享的变量都应该要有一个锁来保护。只有多线程共享的,可变的数据才需要锁来保护。对于包含多个变量的不变性条件,其中涉及的所有变量都需要使用同一个锁来保护。synchronized只能确保单个方法是线程安全的,对于多个操作的符合操作,还需要另外的同步才可以。注意:并不是只有修改的时候,才需要同步。当执行较长时间的运算时,或者无法快速完成的操作,一定不要持有锁。

Java Concurrent Programming Practical note-Thread safety

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.