`
Teok
  • 浏览: 148492 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

读《Java Concurrency in Practise》Chapter 1. -Introduction

阅读更多
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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics