Java并发编程实践学习之:线程安全的概念的经典总结在Java并发编程实践(Java concurrency in practice) 一
Java并发编程实践学习之:线程安全的概念的经典总结
在Java并发编程实践(Java concurrency in practice) 一书中第二章讲到了线程安全的概念.
有些话非常的经典.我同时找来英文放在这里.
/***在没有正确同步的情况下,如果多个线程访问了同一个变量,你的程序就存在隐患.有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;or3. 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.
待续---