zoukankan      html  css  js  c++  java
  • Java并发编程实践第二章线程安全

    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型标一样,并发编程也不只是线程和锁。这一点大家可能会觉得奇怪。诚然,建造不会坍塌的桥梁需要使用大量的铆钉和I型标,编写并发程序需要正确的使用线程和锁。但是,这些只是达到目的的机制。编写线程安全的代码本质是管理对状态的访问,特别共享的可变状态。

    Informally, an object’s state is its data, stored in state variables such as instance or static ?elds. An object’s state may include ?elds 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.
    不严格的说,对象的状态就是存储在对象的入实例变量或静态域的状态变量中得数据。对象状态可以包括该对象所依赖的其他对象的字段。HashMap的状态部分存储在HashMap对象本身中,但也存在于许多Map.Entry对象中。对象状态包括所有影响对象外部可以观察到的行为的数据。

    By shared,wemean 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.
    这里共享的意思是,一个变量可能被多个线程访问。可变状态的意思是状态的值在其生命周期内可能发生变化。当我们说线程安全的时候,好像我们是指代码,但其实我们说的是我们如何保护数据以避免它们被失控的并发访问。

    Whenever more than one thread accesses a given state variable, and one of them might write to it, they all must coordinate their access to it using synchronization. The primary
    mechanism for synchronization in Java is the synchronized keyword, which provides exclusive locking, but the term “synchronization” also includes the use of
    volatile variables, explicit locks, and atomic variables.
    对象是否需要是线程安全的取决于它是否会被多个线程访问。这个属性说的是如何使用对象而不是对象做什么。使对象变成线程安全需要使用同步来协调对其可变状态的访问。如果不能做到这一点就会造成数据损坏和其他的不良后果。

    只要有一个以上的线程会访问一个给定的状态变量,而其中有一个线程可能会写这个状态变量,那么就必须协调所有的线程对该状态变量的访问。Java中主要的同步机制是synchronized关键字。该关键字提供排他锁,“同步”也包含使用volatile,显示锁和原子变量。

    You should avoid the temptation to think that there are “special” situations in which this rule does not apply. A program that omits needed synchronization
    might appear to work, passing its tests and performing well for years, but it is still broken and may fail at any moment.
    你可能会认为上述规则在一些特殊条件下并不适用。但你最好还是尽量避免这样做。一个没有所需同步机制的程序可能开起来是没错的,它能通过测试并且工作了好多年,但它仍是错误的,随时可能失败。

  • 相关阅读:
    焦虑:都说程序员是青春饭,那么程序员老了何去何从呢?
    数据库查询语句优化,mysql优化,join语句优化附带YYC松鼠短视频系统详细demo效果
    IT行业:为什么大部分人都不认可php语言呢?
    拇指赚点赞无加密源码发布分享仅供学习
    区块鼠区块养殖系统源码无加密源码发布分享仅供学习
    3月1日晚突遭大量攻击,网站/APP突然遭遇黑客攻击时该如何应对?
    ThinkPHP内核全行业小程序运营管理系统源码免费分享下载
    2020年不管打工还是创业,居然还有人相信读书无用论?
    在IT界,应聘企业去上班如果老板一点不懂技术那绝对是作茧自缚
    看了这篇文章你还不懂傅里叶变换,那我就真没办法呢!
  • 原文地址:https://www.cnblogs.com/littlesuccess/p/2260448.html
Copyright © 2011-2022 走看看