`

Task Execution

阅读更多
Chapter 6. Task Execution
Most concurrent applications are organized around the execution of tasks: abstract, discrete units of work. Dividing the work of an application into tasks simplifies program organization, facilitates error recovery by providing natural transaction boundaries, and promotes concurrency by providing a natural structure for parallelizing work.

大多数并发应用程序,都是围绕着任务(抽象的,离散的工作单位)的执行。将应用程序的工作拆成多个任务,简化了程序的组织,通过提供自然事务边界有利于错误恢复,并通过提供了一个并行工作的自然结构来提高并发性。

6.1. Executing Tasks in Threads
The first step in organizing a program around task execution is identifying sensible task boundaries. Ideally, tasks are independent activities: work that doesn't depend on the state, result, or side effects of other tasks. Independence facilitates concurrency, as independent tasks can be executed in parallel if there are adequate processing resources. For greater flexibility in scheduling and load balancing tasks, each task should also represent a small fraction of your application's processing capacity.
将一个程序组织成任务的执行的第一步,是辨别明智的任务界限。理论上,任务都是独立的活动,不依赖于其他任务的状态、结果或副作用。独立性有助于编写并发程序,因为在处理资源充足的情况下,独立的任务可以并发执行。对于更大的灵活调度和负载均衡的任务,每个任务应该也代表了您的应用程序的处理能力的一小部分

Server applications should exhibit both good throughput and good responsiveness under normal load. Application providers want applications to support as many users as possible, so as to reduce provisioning costs per user; users want to get their response quickly. Further, applications should exhibit graceful degradation as they become overloaded, rather than simply falling over under heavy load. Choosing good task boundaries, coupled with a sensible task execution policy (see Section 6.2.2), can help achieve these goals.

服务器应用程序应表现出良好的吞吐量和正常负载下良好的响应。应用程序提供者希望应用程序,以支持尽可能多的用户,从而降低每个用户的配置成本,用户希望得到他们的反应很快。此外,应用程序应表现出优美的退化,而不是在重负载下,简单地崩溃。选择良好的任务边界,再加上一个明智的任务执行的政策(见6.2.2节),可以帮助实现这些目标。

Most server applications offer a natural choice of task boundary: individual client requests. Web servers, mail servers, file servers, EJB containers, and database servers all accept requests via network connections from remote clients. Using individual requests as task boundaries usually offers both independence and appropriate task sizing. For example, the result of submitting a message to a mail server is not affected by the other messages being processed at the same time, and handling a single message usually requires a very small percentage of the server's total capacity.


大多数服务端应用程序提供的任务边界的自然选择:个人客户端的请求。所有的Web服务器,邮件服务器,文件服务器,EJB容器和数据库服务器接受通过网络从远程客户端连接请求。通常使用个别请求作为任务边界提供了独立性和适当的任务大小。例如,提交邮件到邮件服务器的结果是不受在同一时间正在处理的其他消息的影响,处理一个单一的消息,通常需要一个服务器的总容量非常小的比例。

6.1.1. Executing Tasks Sequentially
There are a number of possible policies for scheduling tasks within an application, some of which exploit the potential for concurrency better than others. The simplest is to execute tasks sequentially in a single thread. SingleThreadWeb-Server in Listing 6.1 processes its tasksHTTP requests arriving on port 80sequentially. The details of the request processing aren't important; we're interested in characterizing the concurrency of various scheduling policies.
应用程序内的调度任务的的策略有许多,其中一些并发的利用比别的更好。最简单的方法是在一个单独的线程中顺序地执行任务。在清单6.1中,Web服务器顺序地处理的到达80端口的HTTP请求的任务。请求处理的细节并不重要,我们的兴趣在于描述各种调度策略的并发性。


SingleThreadedWebServer is simple and theoretically correct, but would perform poorly in production because it can handle only one request at a time. The main thread alternates between accepting connections and processing the associated request. While the server is handling a request, new connections must wait until it finishes the current request and calls accept again. This might work if request processing were so fast that handleRequest effectively returned immediately, but this doesn't describe any web server in the real world.
SingleThreadedWebServer比较简单,在理论上是正确的,但在执行中性能可能会很差,因为它只能一次处理一个请求。主线程接受连接,和处理相关的请求之间的交替。当服务器正在处理请求,新的连接必须等待,直到它完成当前请求,并再次接受新的请求。如果处理请求很快,能够立即返回处理结果,这可能会奏效。但是这并不能代表在现实世界中的任何Web服务器。


Processing a web request involves a mix of computation and I/O. The server must perform socket I/O to read the request and write the response, which can block due to network congestion or connectivity problems. It may also perform file I/O or make database requests, which can also block. In a single-threaded server, blocking not only delays completing the current request, but prevents pending requests from being processed at all. If one request blocks for an unusually long time, users might think the server is unavailable because it appears unresponsive. At the same time, resource utilization is poor, since the CPU sits idle while the single thread waits for its I/O to complete.

处理Web请求涉及计算和输入输出。服务端要执行socket的输入输出来读取请求和输出结果,由于网路的堵塞或者连接问题可能导致阻塞。它也可以执行文件I/O或数据库的请求,也可能导致阻塞。在一个单线程的服务器,阻塞不仅延误完成当前的请求,但阻止正在处理挂起的请求。如果一个请求的阻塞时间过长,用户可能会认为服务器是不可用的,因为它似乎反应迟钝。在同一时间,资源利用率差,因为C单个线程等待它的I / O完成的时候,CPU处于闲置状态。

In server applications, sequential processing rarely provides either good throughput or good responsiveness. There are exceptions such as when tasks are few and long-lived, or when the server serves a single client that makes only a single request at a time but most server applications do not work this way.[1]
在服务器应用程序,顺序处理很少提供任何良好的吞吐量或良好的响应。也有例外,如任务少时、存在时间长,或当服务器只有一个客户端,一次只有一个请求。不过大多数服务器不以这种方式工作。

6.1.2. Explicitly Creating Threads for Tasks
A more responsive approach is to create a new thread for servicing each request, as shown in ThreadPerTaskWebServer in Listing 6.2.
反应更加灵敏的方法是为每个请求创建一个新的线程来服务。如ThreadPerTaskWebServer在6.2所示。

THReadPerTaskWebServer is similar in structure to the single-threaded version the main thread still alternates between accepting an incoming connection and dispatching the request. The difference is that for each connection, the main loop creates a new thread to process the request instead of processing it within the main thread. This has three main consequences:
THReadPerTaskWebServer类是在结构上类似于单线程版本,主线程在接受连接,处理请求之间切换。所不同的是,主线程为每个连接创建一个新的线程来处理请求,而不是在主线程中处理。这主要有三个后果:

Task processing is offloaded from the main thread, enabling the main loop to resume waiting for the next incoming connection more quickly. This enables new connections to be accepted before previous requests complete, improving responsiveness.
任务处理从主线程中卸载了,使主线程更迅速地恢复等待下一个传入的连接。这使得新的连接在处理请求被完成之前被接受的,提高了反应。


Tasks can be processed in parallel, enabling multiple requests to be serviced simultaneously. This may improve throughput if there are multiple processors, or if tasks need to block for any reason such as I/O completion, lock acquisition, or resource availability.
任务可以并行处理,从而使多个请求同时进行处理。这可能会提高吞吐量,如果有多个处理器,或者如果任务需要被阻止,如I / O完成,锁定收购,或资源的可用性。

Task-handling code must be thread-safe, because it may be invoked concurrently for multiple tasks.
任务处理代码必须是线程安全的,因为它可能被多个任务同时调用。

Under light to moderate load, the thread-per-task approach is an improvement over sequential execution. As long as the request arrival rate does not exceed the server's capacity to handle requests, this approach offers better responsiveness and throughput.
在轻至中度负载,每个任务一个线程的方法是对于顺序执行任务的改善。只要请求到达率不超过服务器处理请求的能力,这种方法提供了更好的响应和吞吐量。

6.1.3. Disadvantages of Unbounded Thread Creation
For production use, however, the thread-per-task approach has some practical drawbacks, especially when a large number of threads may be created:
然而,在现实中,每个任务一个线程的方法具有一些缺点,特别是可能创建大量的线程的时候:

Thread lifecycle overhead. Thread creation and teardown are not free. The actual overhead varies across platforms, but thread creation takes time, introducing latency into request processing, and requires some processing activity by the JVM and OS. If requests are frequent and lightweight, as in most server applications, creating a new thread for each request can consume significant computing resources.
线程生命周期开销。线程创建和销毁,是不是免费的。实际开销由于平台不同而不一样,但线程的创建需要时间,引入请求的处理延迟,需要一些由JVM和OS的处理活动。如果请求频繁并且是轻量级的,在大多数服务器应用程序中,为每个请求创建一个新的线程,都会消耗大量的计算资源。

Resource consumption. Active threads consume system resources, especially memory. When there are more runnable threads than available processors, threads sit idle. Having many idle threads can tie up a lot of memory, putting pressure on the garbage collector, and having many threads competing for the CPUs can impose other performance costs as well. If you have enough threads to keep all the CPUs busy, creating more threads won't help and may even hurt.
资源的消耗。活动线程会消耗系统资源,尤其是内存。当有运行的线程数目超出可用的处理器的数目,线程就会处于闲置状态。空闲线程很多的话,会占用大量的内存,增加垃圾收集器的压力,多个线程争夺CPU会增加他性能成本。如果你有足够的线程所有CPU保持忙碌,创建更多的线程不会帮助,反而适得其反。

Stability. There is a limit on how many threads can be created. The limit varies by platform and is affected by factors including JVM invocation parameters, the requested stack size in the Thread constructor, and limits on threads placed by the underlying operating system.[2] When you hit this limit, the most likely result is an OutOfMemoryError. trying to recover from such an error is very risky; it is far easier to structure your program to avoid hitting this limit.
稳定。有一个可以创建多少个线程的限制。限制因平台而异,受以下因素的影响:JVM调用参数、线程构造器要求的堆栈大小,底层操作系统的线程限制等。当你超过了这个限制,最可能的结果是一个OutOfMemoryError,试图从这样的错误恢复是非常危险的;通过组织程序要比避免超过限制要容易得多。

Up to a certain point, more threads can improve throughput, but beyond that point creating more threads just slows down your application, and creating one thread too many can cause your entire application to crash horribly. The way to stay out of danger is to place some bound on how many threads your application creates, and to test your application thoroughly to ensure that, even when this bound is reached, it does not run out of resources.
在某个临界点之前,多个线程可以提高吞吐量。但是当超过这个临界点,创造更多的线程只会减慢您的应用程序,并且创建线程太多,可能会导致整个应用程序崩溃可怕。解决危险的方法是限制应用程序创建线程的数目,彻底测试您的应用程序来确保即使达到这个界限,它不会耗尽资源。

The problem with the thread-per-task approach is that nothing places any limit on the number of threads created except the rate at which remote users can throw HTTP requests at it. Like other concurrency hazards, unbounded thread creation may appear to work just fine during prototyping and development, with problems surfacing only when the application is deployed and under heavy load. So a malicious user, or enough ordinary users, can make your web server crash if the traffic load ever reaches a certain threshold. For a server application that is supposed to provide high availability and graceful degradation under load, this is a serious failing.
每个任务一个线程的方法问题,是除了客户端发送HTTP请求到服务端的速率之外,没有限制创建的线程数量。像其他并发危害一样,在创建原型设计和开发过程中,无限制创建线程可能没有问题,当应用程序部署和重负载下,就会导致问题的出现。因此,一个恶意用户,或足够普通用户,可以让你的Web服务器崩溃,如果服务器负荷达到一定的阈值。对于服务器应用程序应该在重负载下提供高可用性和优美的退化,这是一个严重的失败。

分享到:
评论

相关推荐

    distributed-task:用go语言编写的分布式任务调度框架. A framework of task execution in distributed system Written with go

    A light framework of task execution in distributed system Written with go 安装: go get github.com/foreversmart/distributed-task 更新: go get -u github.com/foreversmart/distributed-task 添加flag u ...

    Architecture Support for Task Out-of-Order Execution in MPSoCs

    In order to solve the problem, this paper proposes a novel high level architecture support for automatic out-of-order (OoO) task execution on FPGA based heterogeneous MPSoCs. The architecture support...

    数据库备份可能出错的十种情况

    如果你只做全备份,那么受限于全备份的大小和备份时间,不可能常做。而且只有 全备份,不能将数据库恢复至某个时间点。所以,我们需要全备份+日志备份。比如每天一个全备份,每隔1小时或若干分钟一个日志备份。...

    解决Java_heap_space问题

    解决Java_heap_space问题

    UPnP Simple Task Execution Manager-开源

    简单任务执行管理器 (STEM) 是一种 OSGi UPnP 服务,当可用 UPnP 服务或设备上的一组条件评估为真时,它会在 UPnP 设备和服务上生成事件和/或执行操作。

    scala-sbt-scala编译工具

    scala 编译工具 sbt 安装包。...Parallel task execution, including parallel test execution Library management support: inline declarations, external Ivy or Maven configuration files, or manual management

    JBPM4中ProcessDefinition、ProcessInstance、Execution、Task关系和区别

    BPM4中ProcessDefinition、ProcessInstance、Execution、Task关系和区别

    Efficient Android Threading(O'Reilly,2014)

    Multithreading is essential if you want ... Handle background task execution with AsyncTask and IntentService Access content providers with AsyncQueryHandler Use loaders to update the Ui with new data

    Mastering.Ansible.178439548X

    Explore use cases for Ansible's advanced features including task delegation, fast failures, and serial task execution Extend Ansible with custom modules, plugins, and inventory sources Who This Book ...

    hadoop 权威指南(第三版)英文版

    The Task Execution Environment Speculative Execution Output Committers Task JVM Reuse Skipping Bad Records 7. MapReduce Types and Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

    java线程池概念.txt

    private void runTask(Runnable task) { final ReentrantLock runLock = this.runLock; runLock.lock(); try { //多次检查线程池有没有关闭 if (runState Thread.interrupted() && runState >= STOP) ...

    opengoo 1.3 RC1

    opengoo 的新版本,修复了一群bug: Changelog ========= Since 1.3-beta -------------- - usability: Emails ... - bugfix: Total task execution time report included task pause time as well as task time.

    java concurrency programming in practice

    Task Execution 72 6.1.ExecutingTasksinThreads 72 6.2.TheExecutorFramework 74 6.3.FindingExploitableParallelism 78 Summary 83 Chapter 7. Cancellation and Shutdown 85 7.1.TaskCancellation 85 7.2....

    sails-hook-ohmytasks:吊起前钩上正在运行的任务

    [ ] , //this is a list of tasks to be executed before : function ( sails , next ) { } , //function to be executed before task execution onLift : function ( ) { } , //function to execute on lifted ...

    hadoop_the_definitive_guide_3nd_edition

    The Task Execution Environment 212 Speculative Execution 213 Output Committers 215 Task JVM Reuse 216 Skipping Bad Records 217 7. MapReduce Types and Formats . . . . . . . . . . . . . . . . . . . . . ...

    vxworks_kernel_programmers_guide_6.9

    Task Switch Hook Support .............................................................................. 18 Task Create Hook Support .......................................................................

    go-grpc-task-execution-engine

    go-grpc-task-execution-engine

    Swift 多线程任务管理库,很强大.zip

    Swift 多线程任务管理库,很强大。.zip,A lightweight, pure-Swift library for manage the task execution in different threads. Through the definition a simple but powerful concept, Kommand.

    hadoop mapreduce

    6.3 Task Execution & Environment.................................................................................. 14 6.4 Job Submission and Monitoring....................................................

Global site tag (gtag.js) - Google Analytics