zoukankan      html  css  js  c++  java
  • Concurrency Programming Guide 并发设计指引(二)

    以下翻译是本人通过谷歌工具进行翻译,并进行修正后的结果,希望能对大家有所帮助。如果您发现翻译的不正确不合适的地方,希望您能够发表评论指正,谢谢。转载请注明出处。

    Concurrency and Application Design

    并发和程序设计


    In the early days of computing, the maximum amount of work per unit of time that a computer could perform was determined by the clock speed of the CPU. But as technology advanced and processor designs became more compact, heat and other physical constraints started to limit the maximum clock speeds of processors. And so, chip manufacturers looked for other ways to increase the total performance of their chips. The solution they settled on was increasing the number of processor cores on each chip. By increasing the number of cores, a single chip could execute more instructions per second without increasing the CPU speed or changing the chip size or thermal characteristics. The only problem was how to take advantage of the extra cores.
    在计算机的早期,计算机单位时间运行的工作量由CPU得时钟速度来决定。但随着技术的进度和处理器设计的更加紧凑,发热和其他的物理因素开始限制处理器的最大时钟速度。所以,芯片制造商们寻找了其他的办法去增加他们芯片的性能。他们选择的解决办法是增加一个芯片上得处理器得个数。通过增加核心个数,一个单一的芯片每秒钟能够执行更多得指令而不需要增加CPU的速度或者改变芯片的尺寸或者传热性能。唯一的问题是如何去利用额外的核。


    In order to take advantage of multiple cores, a computer needs software that can do multiple things simultaneously. For a modern, multitasking operating system like OS X or iOS, there can be a hundred or more programs running at any given time, so scheduling each program on a different core should be possible. However, most of these programs are either system daemons or background applications that consume very little real processing time. Instead, what is really needed is a way for individual applications to make use of the extra cores more effectively.
    为了充分利用多内核,一台电脑需要的软件,可以同时做许多事情的。对于一个现代的,像OS X或iOS的多任务操作系统,可以有一百个或多个程序运行在任何给定的时间,所以排定每个程序在不同的核心应该是可能的。然而,大多数这些计划要么是系统守护进程或消耗很少真正的处理时间的后台应用程序。相反,真正需要的是一个单个应用程序的方式,更有效地利用额外的核心。


    The traditional way for an application to use multiple cores is to create multiple threads. However, as the number of cores increases, there are problems with threaded solutions. The biggest problem is that threaded code does not scale very well to arbitrary numbers of cores. You cannot create as many threads as there are cores and expect a program to run well. What you would need to know is the number of cores that can be used efficiently, which is a challenging thing for an application to compute on its own. Even if you manage to get the numbers correct, there is still the challenge of programming for so many threads, of making them run efficiently, and of keeping them from interfering with one another.
    应用程序使用多个内核的传统方式是创建多线程。然而,由于增加的核心数量,有许多线程的问题需要解决。最大的问题是,线程代码不很好地扩展到任意数目的核心。您不能创建非常多的线程在多内核,而希望某个程序运行良好。你需要知道的是,这是一个具有挑战性的事情,在自己的应用程序来计算的核心数量,可以有效地用于。即使你设法得到的数字是正确的,仍然有这么多线程编程的挑战,使他们有效地运行,使他们从彼此干扰。


    So, to summarize the problem, there needs to be a way for applications to take advantage of a variable number of computer cores. The amount of work performed by a single application also needs to be able to scale dynamically to accommodate changing system conditions. And the solution has to be simple enough so as to not increase the amount of work needed to take advantage of those cores. The good news is that Apple’s operating systems provide the solution to all of these problems, and this chapter takes a look at the technologies that comprise this solution and the design tweaks you can make to your code to take advantage of them.
    因此,要总结的问题,需要有一种方法为应用程序能够利用一个可变数目的计算机核心。由一个单一的应用程序进行的工作还需要能够动态扩展,以适应不断变化的系统条件。并将该溶液具有足够简单,以便不增加大量的工作,需要利用这些核心。好消息是,苹果的操作系统提供了解决所有这些问题,本章需要看看在这个解决方案的技术,包括设计的调整,你可以让你的代码利用它们。


    The Move Away from Threads

    抛弃线程


    Although threads have been around for many years and continue to have their uses, they do not solve the general problem of executing multiple tasks in a scalable way. With threads, the burden of creating a scalable solution rests squarely on the shoulders of you, the developer. You have to decide how many threads to create and adjust that number dynamically as system conditions change. Another problem is that your application assumes most of the costs associated with creating and maintaining any threads it uses.
    虽然线程已经使用很多年了而且有他们使用的地方,但是他们不能用可扩展的方式解决多任务运行的问题。线程,创建一个可扩展的解决方案的负担落在你的肩膀上,开发者。你必须决定多少线程创建和系统条件的变化动态调整这个数字。另一个问题是,您的应用程序假定它使用的任何线程创建和维护相关的成本。


    Instead of relying on threads, OS X and iOS take an asynchronous design approach to solving the concurrency problem. Asynchronous functions have been present in operating systems for many years and are often used to initiate tasks that might take a long time, such as reading data from the disk. When called, an asynchronous function does some work behind the scenes to start a task running but returns before that task might actually be complete. Typically, this work involves acquiring a background thread, starting the desired task on that thread, and then sending a notification to the caller (usually through a callback function) when the task is done. In the past, if an asynchronous function did not exist for what you want to do, you would have to write your own asynchronous function and create your own threads. But now, OS X and iOS provide technologies to allow you to perform any task asynchronously without having to manage the threads yourself.
    而不是依赖于线程,OS X和iOS异步设计方法来解决并发问题。异步函数在操作系统中存在多年,并经常被用来启动可能需要很长的时间任务,例如从磁盘中读取数据。异步函数调用时,开始运行的任务时做一些幕后工作,但可能在任务完成之前就已经有了返回。通常情况下,这项工作需要获得一个后台线程,该线程上启动所需的任务,然后当任务完成时发送一个通知给调用者(通常是通过回调函数)。在过去,如果是一个你想要的异步函数不存在,你必须编写自己的异步函数,创建你自己的线程。但现在,OS X和iOS提供的技术允许你执行任何任务,而无需自己管理线程异步。


    One of the technologies for starting tasks asynchronously is Grand Central Dispatch (GCD). This technology takes the thread management code you would normally write in your own applications and moves that code down to the system level. All you have to do is define the tasks you want to execute and add them to an appropriate dispatch queue. GCD takes care of creating the needed threads and of scheduling your tasks to run on those threads. Because the thread management is now part of the system, GCD provides a holistic approach to task management and execution, providing better efficiency than traditional threads.
    异步启动任务的技术之一是GCD。此技术需要的线程管理代码通常会写在自己的应用程序中和移动代码到系统级。但是现在所有你必须做的只是确定你要执行的任务,并把它们添加到一个适当的调度队列。 GCD会负责创建需要的线程和任务调度上运行这些线程。因为线程管理现在是系统的一部分,GCD提供了一个全面的方法管理和执行任务,提供更好的效率比传统的线程。


    Operation queues are Objective-C objects that act very much like dispatch queues. You define the tasks you want to execute and then add them to an operation queue, which handles the scheduling and execution of those tasks. Like GCD, operation queues handle all of the thread management for you, ensuring that tasks are executed as quickly and as efficiently as possible on the system.
    操作队列是Objective-C的对象,对象行为很像调度队列。您定义你要执行的任务,然后将它们添加到一个操作队列,处理这些任务的计划和执行。 GCD一样,操作队列处理所有线程管理,确保任务尽快和尽可能高效的系统上执行。


    The following sections provide more information about dispatch queues, operation queues, and some other related asynchronous technologies you can use in your applications.

    以下各节提供更多信息调度队列,操作队列,以及其他一些相关的异步技术,你可以在你的应用程序中使用。


  • 相关阅读:
    Java基础小结
    Struts2
    Ajax
    IIS 调优、支持同时10万个请求
    sqlserver2014 数据完整备份、日志备份
    windows server 2016添加开机启动项
    FileZilla服务器
    Windows server 2016 设置多用户登陆
    centos7 调整XFS格式的磁盘容量大小
    IIS 500.19 0x8007000d 0x80070032 0x80070021 提示“执行此操作时出错”
  • 原文地址:https://www.cnblogs.com/riskyer/p/3402586.html
Copyright © 2011-2022 走看看