`
lvhuiqing
  • 浏览: 256697 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

2.1. What is Thread Safety?(什么是线程安全)

阅读更多
2.1. What is Thread Safety?(什么是线程安全)
Defining thread safety is surprisingly tricky. The more formal attempts are so complicated as to offer little practical guidance or intuitive understanding, and the rest are informal descriptions that can seem downright circular. A quick Google search turns up numerous "definitions" like these:
线程安全的定义出奇的复杂。比较严格的定义有可能会过于复杂以至于难以理解并且没有实际的价值。而一些非正式的描述则有可能会陷入循环论证的陷阱。从google中,我们搜索到了这样的描述:
. . . can be called from multiple program threads without unwanted interactions between the threads.
即使被多个线程调用,线程间也不会出现意料之外的交互时序。
. . .may be called by more than one thread at a time without requiring any other action on the caller's part.
可以同时被多个线程调用,而要求调用方其他任何的动作。
Given definitions like these, it's no wonder we find thread safety confusing! They sound suspiciously like "a class is thread-safe if it can be used safely from multiple threads." You can't really argue with such a statement, but it doesn't offer much practical help either. How do we tell a thread-safe class from an unsafe one? What do we even mean by "safe"?
看到上述的定义后,我们对线程安全的定义可能反而更加迷惑。这就好像说“一个类是线程安全的就是我们可以在多线程中放心的使用它们”。你的确从这样的话中找不出瑕疵,但是这样的定义也的确不会让你明白任何事情。我们如何区分一个类是线程安全的,还是不安全的。甚至,什么是“安全”?
At the heart of any reasonable definition of thread safety is the concept of correctness. If our definition of thread safety is fuzzy, it is because we lack a clear definition of correctness.
任何针对线程安全的合理定义中,正确性都应该是核心内容。如果我们关于线程安全的定义是模糊的,那通常是因为我们缺少了对正确性的定义。
Correctness means that a class conforms to its specification. A good specification defines invariants constraining an object's state and post conditions describing the effects of its operations. Since we often don't write adequate specifications for our classes, how can we possibly know they are correct? We can't, but that doesn't stop us from using them anyway once we've convinced ourselves that "the code works". This "code confidence" is about as close as many of us get to correctness, so let's just assume that single-threaded correctness is something that "we know it when we see it". Having optimistically defined "correctness" as something that can be recognized, we can now define thread safety in a somewhat less circular way: a class is thread-safe when it continues to behave correctly when accessed from multiple threads.
正确性意味着一个类实现了它的类规范说明中的行为。好的规范会定义常量来约束对象的状态并且提供使用其方法的条件。对于我们开发的类,我们经常会无法提供足够的文档来描述他们,那么,我们怎样能够知道如何使用它们才是正确的?答案是,我们不能。但是无论如何,我们仍将会使用这些代码,因为我们确信他们是可以工作的。这种“代码自信”在大部分情况下,所以我们可以大胆的宣称,单线程的安全性就是“所见即所得”。这样我们就可以在很大程度上避免“循环论证”地给出一个线程安全的定义了:线程安全就是一个类被多线程访问的时候可以持续的保持正确的行为。
A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.
线程安全就是一个类被多线程访问的时候可以持续的保持正确的行为。而不需要考虑由运行时环境决定线程的时序和交叉。并且不需要在其调用方附加同步机制或其他条件。

Since any single-threaded program is also a valid multithreaded program, it cannot be thread-safe if it is not even correct in a single-threaded environment. [2] If an object is correctly implemented, no sequence of operations calls to public methods and reads or writes of public fields should be able to violate any of its invariants or post conditions. No set of operations performed sequentially or concurrently on instances of a thread-safe class can cause an instance to be in an invalid state.
[2] If the loose use of "correctness" here bothers you, you may prefer to think of a thread-safe class as one that is no more broken in a concurrent environment than in a single-threaded environment.
任何单线程程序都可以转变成一个正确的多线程程序,因为如果一个程序如果不是线程安全的,那么他在单线程环境中也不会是线程安全的。如果一个对象被正确的实现的话,那么对其public方法和public域的任何操作时序都不会破坏他的不变性和post conditions。也不会有任何操作能够把线程安全的类变成不合法的状态。
如果对“正确性”这个字眼的频繁使用会让你觉得困扰的话,你可以认为一个线程安全的类既不会在单线程环境中被破坏,也不会在多线程环境中被破坏。
Thread-safe classes encapsulate any needed synchronization so that clients need not provide their own.
线程安全的类在其内部封装了所有需要的同步机制所以客户端不需要自己来提供。

2.1.1. Example: A Stateless Servlet(实例:无状态的servlet)
In Chapter 1, we listed a number of frameworks that create threads and call your components from those threads, leaving you with the responsibility of making your components thread-safe. Very often, thread-safety requirements stem not from a decision to use threads directly but from a decision to use a facility like the Servlets framework. We're going to develop a simple example a servlet-based factorization service and slowly extend it to add features while preserving its thread safety.
在第一章中,我们列举了很多可能会创建线程来回调你的应用组件的框架,这些框架把维护线程安全的责任留给了你的应用组件。在很多情况下,线程安全的需求不是来源于你自身的决定而是来源于你所使用的框架的需求,比如Servlets框架就是这样。在保证线程安全的前提下,我们将会开发一个基于servlet的服务,并且慢慢加入一些有特色的东西。
Listing 2.1 shows our simple factorization servlet. It unpacks the number to be factored from the servlet request, factors it, and packages the results into the servlet response.
表2.1向我们展现了一个简单的servlet的元素。它把请求解包、把请求因子化、把结果打包进入servlet响应。
Listing 2.1. A Stateless Servlet.(无状态的servlet)
@ThreadSafe
public class StatelessFactorizer implements Servlet {
    public void service(ServletRequest req, ServletResponse resp) {
        BigInteger i = extractFromRequest(req);
        BigInteger[] factors = factor(i);
        encodeIntoResponse(resp, factors);
    }
}

Stateless Factorizer is, like most servlets, stateless: it has no fields and references no fields from other classes. The transient state for a particular computation exists solely in local variables that are stored on the thread's stack and are accessible only to the executing thread. er thread accessing the same Stateless Factorizer; because the two threads do not share state, it is as if they were accessing different instances. Since the actions of a thread accessing a stateless object cannot affect the correctness of operations in other threads, stateless objects are thread-safe.
像大多数servlet的实现的那样,无状态分解就是没有被其他类应用的域和引用,也没有引用其他的类。一次特定的计算中的瞬时状态保存在线程的栈内存中,不会被其他线程访问。访问一个无状态的因子的线程不会影响其他访问该因子的线程,因为两个线程不会分享状态。就如同他们在分别访问两个实例。由于访问一个无状态的对象的线程的动作不会妨碍其他线程的正确性,因此无状态对象是线程安全的。
Stateless objects are always thread-safe.
无状态对象永远是线程安全的。

The fact that most servlets can be implemented with no state greatly reduces the burden of making servlets thread-safe. It is only when servlets want to remember things from one request to another that the thread safety requirement becomes an issue.
大多数servlet可以被设计成线程安全的现实极大的降低了把servlet做成线程安全这种事情的难度。只有一个servlet想要从一个request记住某些事情的时候,线程安全的需求才值得被重视。
0
0
分享到:
评论

相关推荐

    五年级英语特殊疑问句练习.doc

    11. What day is it today?(今天是星期几?) 12. What’s the date today?(今天是几号?) 13. What can you do on your birthday?(你生日那天能做什么?) 14. What are you doing?(你正在做什么?) 15. What...

    hyperscan-2.1.zip

    FreeDPI User Space ...随着大数据时代的来临,针对网络流量的数据采集和数据挖掘需求也变得越来越强烈,而FreeDPI技术可以为数据采集的“4W(Who、When、Where、What)”中最重要的“What”维度提供最丰富的信息。

    wireshark抓包实验之TCP

    1. What is the IP address and TCP port number used by the client computer (source) that is transferring the file to gaia.cs.umass.edu? To answer this question, it’s probably easiest to select an ...

    超级网搜(SuperSearch) - 2.1.2.4

    最新版本发布(What's New) * SuperSearch?(超级网搜) 2.0.2.4 版 !- Boom 2006-3-4 o 更新日文搜索引擎 * SuperSearch?(超级网搜) 2.0.1.4 版 !- Boom 2006-2-22 o 更换工具栏图标 o 增加[使用多线程]设定...

    开心版三年级下册小学英语 综合练习题.doc

    9. What color is this? It’s yellow. → blue, colors 四、用英语填空,使句子通顺合理 1. How are you? I am fine. 2. Jenny is cold. She is sad, too. 3. They are sad. 4. What color are they? They are ...

    ODZ_Getting Started With ZEMAX_version2.1.pdf

    zemax软件学习资料,包含7个章节。 1.ABOUT THIS GUIDE 2.INSTALLING ZEMAX 3.THE ZEMAX USER INTERFACE 4.OPTIMIZATION ...5.NON-SEQUENTIAL RAY TRACING (EE ONLY) ...6.POLARIZATION, COATINGS...7.WHAT’S NEXT?

    开心版三年级下册小学英语基础题.doc

    What time is it? - do:用于构成一般疑问句,如7. Do you like tennis? - can:询问能力,如8. Can you ride a bike? - are:用于构成一般疑问句,如9. Are you happy? - what color:询问颜色,如10. What ...

    四年级英语上册期中测试卷及听力材料2精选.doc

    1. What day is it today?(今天是星期几?) 2. I like fruits.(我喜欢水果。) 3. It’s cold.(天气很冷。) 4. It’s summer in China.(中国现在是夏天。) 5. Let’s go to bed.(我们去睡觉吧。) 三.听音...

    初一上学期英语知识点总结11.doc

    16. What colour is it (are they)? It’s (They’re)… - 它/它们是什么颜色?它是/它们是... 17. Whose …is this? It’s…. - 这是谁的...?它是... 18. What time is it? It’s…. - 几点了?现在是... 三、...

    中考英语知识点总结.doc

    15. What colour is it (are they)? It’s (They’re)… - 它(它们)是什么颜色?它是(它们是)... 16. Whose …is this? It’s…. - 这是谁的...?这是... 17. What time is it? It’s…. - 几点了?现在是... ...

    英语900句文本 新东方英语900句

    136. What day is today? 今天星期几? 137. Today is Monday. 今天星期一。 138. What's the date today? 今天几号? 139. Today is May 21st. 今天是五月二十一号。 140. When were you born? 你什么时候出生的? ...

    朗文英语1b chapter 5练习题4精选.doc

    He is cooking.(他在做什么?他正在做饭。) 2. What are they doing? They are playing football.(他们在做什么?他们正在踢足球。) 3. What is he doing? He is watching TV.(他在做什么?他正在看电视。) 4...

    小学英语句型转换专项训练.doc

    11. What is in the vase? 12. Who is this? 13. What are you doing? 14. Do they like making the puppet? (疑问词可省略) 15. Do Su Hai and Su Yang live in a new house? (疑问词可省略) 16. What did you put ...

    Frequently Asked Questions about FMC+

    1. What is FMC? FMC is the acronym for FPGA Mezzanine Card. It is a small mezzanine module optimized to provide the physical interface for FPGAs on a carrier card. The physical IO is typically on the ...

    小学英语句子大全_小学英语语句.doc

    1. What colour is it? 它是什么颜色的? 回答时用"It’s ________.",例如"It’s yellow and white." 2. What colour are they? 它们是什么颜色的? 对于复数形式的物体,回答也用"They’re ________.",例如"They...

    七年级重点短语和重要句型PPT学习教案.pptx

    16. What colour is it (are they)? It’s (They’re)…:它(它们)是什么颜色?它是(它们是)… 17. Whose …is this? It’s….:这是谁的…?它是… 18. What time is it? It’s….:几点了?是… 三、交际用语...

    五年级英语下册问答句专练精选.doc

    20. What is the date today? — It’s May 21st. 询问今天的日期。 21. What day is it today? — It’s Thursday. 询问今天是星期几。 22. What are the students doing? — The students are reading a book. ...

    Online maps v3.3.2.1.zip

    Online Maps 3.3.2.1 - 支持16个公司88种地图类型 , 特点: •Unity 2017.1 - 2019.x; •独立,Android,iOS,Windows Store,WebGL; •在线和离线地图; •带有高程的2D地图和3D地图; •2D,3D和Billboard标记;...

Global site tag (gtag.js) - Google Analytics