1.1 A (Very) Brief History of Concurrency
Operating systems evolved to allow more than one program to run at once, running individual programs in processes: isolated, independently executing programs to which the operating system allocates resources such as memory, file handles, and security credentials. If they needed to, processes could communicate with one another through a variety of coarse-grained communication mechanisms: sockets, signal handlers, shared memory, semaphores, and files.
大意:现在的操作系统已经可以同时运行多个程序。每个程序都是隔离地、独立地以进程执行,os负责分配它们所需的资源,例如像:memory、file handles 和security credentials.如果进程之间需要通信,可以通过一个coarse-grained的通信机制的多个方式来实现,像sockets, signal handlers, shared memory, semaphores, and files。
Several motivating factors led to the development of operating systems that allowed multiple programs to execute simultaneously:
Resource utilization. 在一个程序等待时,让另外一个程序去运行。
Fairness.(公平) 用户和程序可以平等地申请资源。通过细粒度(finer-grained)地时间划分来让用户和程序来共享计算机资源。
Convenience.设计多个程序(每个程序都执行一个任务),让它们在有需要的时候进行相互协作,比设计一个程序去执行多个任务要好并且更简单。
In early timesharing systems, each process was a virtual von Neumann computer; it had a memory space storing both instructions and data, executing instructions sequentially according to the semantics of the machine language, and interacting with the outside world via the operating system through a set of I/O primitives. For each instruction executed there was a clearly defined "next instruction", and control flowed through the program according to the rules of the instruction set. Nearly all widely used programming languages today follow this sequential programming model, where the language specification clearly defines "what comes next" after a given action is executed.
关键字:冯*诺依曼(von Neumann computer)计算机,顺序编程模型(sequential programming model)。几乎今天所有广泛使用的变成语言都遵循这个模型。顺序编程模型是直观并天然的,因为它建立于人类行为:在大多数情况下顺序地在同一时间做一件事情。
进程是一个程序控制流,这种流控制着程序当前执行哪个指令,下一步要执行什么指令。
The same concerns (resource utilization, fairness, and convenience) that motivated the development of processes also motivated the development of threads. Threads allow multiple streams of program control flow to coexist(共存) within a process. They share process-wide resources such as memory and file handles, but each thread has its own program counter, stack, and local variables. Threads also provide a natural decomposition(分解,composition的反义词) for exploiting hardware parallelism(平行) on multiprocessor systems; multiple threads within the same program can be scheduled simultaneously on multiple CPUs.
Threads are sometimes called lightweight processes, and most modern operating systems treat threads, not processes, as the basic units of scheduling. In the absence(缺乏,缺失) of explicit coordination, threads execute simultaneously and asynchronously with respect to(with respect to关于,至于) one another. Since threads share the memory address space of their owning process, all threads within a process have access to the same variables and allocate objects from the same heap, which allows finer-grained data sharing than inter-process mechanisms. But without explicit synchronization to coordinate access to shared data, a thread may modify variables that another thread is in the middle of using, with unpredictable results.
线程允许在一个进程内存在多个程序控制流。这些控制流共享进程域的资源(process-wide resources such as memory and file handles),但是每个线程都有自己的程序计数器、栈和局部变量(program counter, stack, and local variables)。线程同时提供“天生”的在多处理器系统上利用硬件的并行性。同一个进行内的多个线程可以被多个CPU平行调度。
线程也被叫做轻量级的进程,大多现代操作系统把线程作为调度的基本单元(而不是进程)。在缺乏明确调度协调时,线程同步和异步执行跟其它线程有关。由于线程共享着拥有他们的进程的内存地址,这个进程中的所有线程都可以访问同一对象和把同一heap分配给多个对象,因此线程允许比进程间更细粒度的数据共享机制。否则没有没有明确的异步机制去访问共享数据,很可能一个线程会将令一个线程正在使用的变量改变,从而导致不可预料的结果。
1.2. Benefits of Threads
使用得当的话,线程可以降低开发和维护成本,并提升复杂程序的性能。通过将异步工作流转换成几乎全部的顺序的工作流,线程使得模仿人类工作和互相影响变得容易。线程同样可以把那些不能转换成顺序工作流的复杂的代码转换成直线的代码,使之更易编写、阅读和维护。
线程很有用,在GUI程序中以改善用户界面的响应,在服务器程序中改善资源利用和吞吐。它们同样简化了JVM的实现:垃圾回收器(garbage collector)通常以一到多个专用的线程来运行。很多重要的Java应用的结构在某种程度上依靠线程。
1.2.1. Exploiting Multiple Processors
由于多核CPU的广泛使用,编写多线程的程序就很重要了。因为CPU调度的基本单元是线程,那么一个单线程的程序在同一时间只能在一个处理上运行。单线程程序在2个处理器的系统上运行时,同一时间就会有一个处理器处于等待,一半的系统资源没有被利用。那么如果在100核系统,就会有99个处理器处于空闲状态。所以设计良好的多线程程序会显著的提升处理器资源的利用。
多线程也可以帮助单CPU系统达到更高的吞吐量。当一个线程处于等待,就可以让另一个线程去执行,这样会避免整个程序的等待。就像在烧水的同时读报,而不是等到水开了再读报。
1.2.2. Simplicity of Modeling
Java线程使得设计多线程程序变的简单(C++中多线程处理就相当复杂)。例如Servlet和RMI框架就利用了这一点。这些框架处理了request management、thread creation和load balancing的细节,在准确的时间分发处理的结果给发送request的组件(dispatching portions of the request handling to the appropriate application component at the appropriate point in the work-flow)。Servlet的编写者不用去担心同时有多少个request将要被处理或者socket input和output是不是阻塞了;当一个servlet的service方法被调用来回应一个web请求,它可以异步地处理这个请求。这可以简化组件开发并减低学习这些框架的难度。
1.2.3. Simplified Handling of Asynchronous Events
1.2.4. More Responsive User Interfaces
1.3. Risks of Threads
1.3.1. Safety Hazards
对可能多线程访问的变量忘记进行同步操作。
1.3.2. Liveness Hazards
不合理的线程使用,可能会引起多种liveness failure:deadlock, starvation, livelock..
1.3.3. Performance Hazards
多线程具有全部单线程程序会出现的风险。
have significant costs: saving and restoring execution context, loss of locality, and CPU time spent scheduling threads instead of running them.
1.4. Threads are Everywhere
分享到:
相关推荐
Chapter 1. An Introduction to Concurrency Chapter 2. Modelling your code: Communicating Sequential Processes Chapter 3. Go’s Concurrency Building Blocks Chapter 4. Concurrency Patterns in Go Chapter ...
《Java并发实践》是Addison-Wesley在2006年5月出版的一本经典书籍,由Brian Goetz、Tim Peierls、Joshua Bloch、David Holmes和Doug Lea合著,全面深入地探讨了Java编程中的多线程和并发问题。这本书是Java并发领域...
通过以上章节的概述,可以看出《Java Concurrency in Practice》这本书全面地覆盖了 Java 并发编程的关键知识点和技术细节,是一本非常有价值的参考书籍。无论是对于初学者还是有经验的开发者来说,都能从中获得关于...
java_concurrency_in_practice.pdf jcip-examples-src.jar jcip-annotations-src.jar 英文版是高清晰的,实战和实践都是同一帮人对英文版书的翻译,网传实战的翻译质量更好,实战是2012年出版的,应该是对前一版实践...
### Java并发编程实践 #### 书籍概述 《Java并发编程实践》是一本由Brian Goetz等人编写的关于Java并发编程的经典著作。本书深入浅出地介绍了Java 5.0及之后版本中新增加的并发特性,并对并发编程进行了全面而详尽...
介绍java的concurrency,绝对超值
Using the concurrency building blocks in java.util.concurrent Performance optimization dos and don'ts Testing concurrent programs Advanced topics such as atomic variables, nonblocking algorithms, ...
Chapter 1. Introduction Chapter 2. Concurrency on the JVM and the Java Memory Model Chapter 3. Traditional Building Blocks of Concurrency Chapter 4. Asynchronous Programming with Futures and Promises ...
<<java并行编程>>英文版chm格式,英文名称<Java Concurrency in Practice>,一直想买这本书,但总是缺货,找到了电子版,分享给大家。 Java Concurrency in Practice By Brian Goetz, Tim Peierls, Joshua Bloch,...
1. **简化复杂系统的开发**:线程作为Java语言的固有特性之一,能够将复杂的异步代码转换为更简单的顺序代码。 2. **利用多处理器系统的计算能力**:线程是利用多核处理器的强大计算能力最直接的方式。 3. **适应...
In the first chapter, the focus is on understanding declarations and access control within Java. Key concepts include: - **Variable Declarations**: This section covers the basics of declaring ...
《Java Concurrency in Practice》是Java并发编程领域的一本经典著作,由Brian Goetz、Tim Peierls、Joshua Bloch、Joseph Bowles和Doug Lea等专家共同编写。这本书深入探讨了Java平台上的多线程和并发编程,旨在...
资源分类:Python库 所属语言:Python 资源全名:oslo.concurrency-4.0.2-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
《Java并发编程实践》是Java并发编程领域的一本经典著作,由Addison-Wesley于2006年出版。这本书深入浅出地探讨了Java平台上的多线程和并发编程,为开发者提供了实用的指导和最佳实践。下面将详细阐述其中的知识点。...
Java Concurrency in Practice 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
《Java并发编程实践》是Java开发者必读的经典之作,由Brian Goetz等多位专家共同撰写。这本书深入浅出地探讨了Java平台上的并发问题,帮助读者理解和掌握如何编写高效、可靠且可维护的多线程应用程序。以下是该书...
《Java并发实践》是Addison-Wesley在2006年出版的一本经典书籍,由Brian Goetz、Tim Peierls、Joshua Bloch、Joseph Bowles和David Holmes等专家合著,全面深入地探讨了Java编程中的多线程和并发问题。这本书是Java...