• 热门专题

Java并发编程实践学习之:线程安全的概念的经典总结

作者:banxi1988  发布日期:2011-10-17 11:19:01
Tag标签:Java  并发编程  线程安全  
  • 在Java并发编程实践(Java concurrency in practice) 一书中第二章讲到了线程安全的概念.
    有些话非常的经典.我同时找来英文放在这里.

     

    编写线程安全的代码,本质上就是管理状态(state)的访问,而且通过都是共享的,可变状态.
    通俗地说,一个对象的状态就是它的数据,存储在状态变量(state variables)中,比如实例域或静域.对象的状态还包括了其他附属对象的域.
    ======================================================
    Writting 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 stave variables such as instance or static fields.An object's state may include fields from other,dependent object;

     所谓共享,是指一个变量可以被多个线程访问:所谓可变,是指变量的值在其生命周期内可以改变.我们讨论的线程安全性好像是关于代码的.但是我们真正要做的,是在不可控制的并发访问中保护数据.
    ===================================================================
    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 i protect data from uncontrolled concurrent access.

     无论何时,只要有多于一个的线程访问给定的状态变量,而且其中某个线程会写入该变量,此时必须使用同步来协调线程对该变量的访问.
    在Java中首要的同步机制是synchronized 关键字,它提供了独占锁.除此之外,术语"同步"还包括volatitle变量,显示锁和原子变量的使用.

    ============================================================
    Whenever more than one thread access a given state variable,and one of them might write to it,they all must coordinate their access to it using synchronized.The primary mechanism for synchronized in java is the synchronized keyword,which provides exclusive locking,but the term "synchronization" also includes the use of volatitle variables,explicit locks,and atomatic variables.

     

    /***  
    在没有正确同步的情况下,如果多个线程访问了同一个变量,你的程序就存在隐患.有3种方法修复它:  
    1.不要跨线程共享变量;  
    2.使状态变量为不可变的;或者  
    3.在任何访问状态变量的时候使用同步.  
    ==================================================================================  
    If multiple threads access the same mutable state varialbe without appropriate synchronization,your program is broken,There are three ways to fix it:  
    1. Don't share the state variable across threads;  
    2. Make the state variable immutable;or  
    3. Use synchronized whenever accessing the state variable.  
    ***/  
    

     

    一开始就将一个类设计成是线程安全的,比在后期重新修复它更容易.
    ====================================================================
    It is easier to design a class to the thread-safe than to retrofit it for thread safety later.

     
    设计线程安全的类时,优秀的面向对象技术---封装,不可变性以及明确的不变约束,会给你提供诸帮助.
    ========================================================================
    When designing thread-safe classes,good object-oriented techniques-encapsulation,immutability,and clear specification of invariants-are your best friends.

     
    有时,抽象封装会与性能产生冲突,虽然不像很多开发者认为的那样频繁,但是首先让你的代码正确,然后(then)再让它跑得快,总是一个良好的实践.
    ===========================================================
    Sometimes abstraction and encapsulation are at odds with performance-although not nearly as often as many developers believe-but it is always as good practice first to make your code right,and then make it fast.

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规