zoukankan      html  css  js  c++  java
  • java 多线程之 Executors

    In all of the previous examples, there's a close connection between the task being done by a new thread, as defined by its Runnableobject, and the thread itself, as defined by a Thread object. This works well for small applications, but in large-scale applications, it makes sense to separate thread management and creation from the rest of the application. Objects that encapsulate these functions are known as executors. The following subsections describe executors in detail.

    • Executor Interfaces define the three executor object types.
    • Thread Pools are the most common kind of executor implementation.
    • Fork/Join is a framework (new in JDK 7) for taking advantage of multiple processors.

    Executor Interfaces

    The java.util.concurrent package defines three executor interfaces:

    • Executor, a simple interface that supports launching new tasks.
    • ExecutorService, a subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself.
    • ScheduledExecutorService, a subinterface of ExecutorService, supports future and/or periodic execution of tasks.

    Typically, variables that refer to executor objects are declared as one of these three interface types, not with an executor class type.

    The Executor Interface

    The Executor interface provides a single method, execute, designed to be a drop-in replacement for a common thread-creation idiom. If r is a Runnable object, and e is an Executor object you can replace

    (new Thread(r)).start();
    

    with

    e.execute(r);
    

    However, the definition of execute is less specific. The low-level idiom creates a new thread and launches it immediately. Depending on the Executor implementation, execute may do the same thing, but is more likely to use an existing worker thread to run r, or to place r in a queue to wait for a worker thread to become available. (We'll describe worker threads in the section on Thread Pools.)

    The executor implementations in java.util.concurrent are designed to make full use of the more advanced ExecutorService andScheduledExecutorService interfaces, although they also work with the base Executor interface.

    The ExecutorService Interface

    The ExecutorService interface supplements execute with a similar, but more versatile submit method. Like executesubmit accepts Runnableobjects, but also accepts Callable objects, which allow the task to return a value. The submit method returns a Future object, which is used to retrieve the Callable return value and to manage the status of both Callable and Runnable tasks.

    ExecutorService also provides methods for submitting large collections of Callable objects. Finally, ExecutorService provides a number of methods for managing the shutdown of the executor. To support immediate shutdown, tasks should handle interrupts correctly.

    The ScheduledExecutorService Interface

    The ScheduledExecutorService interface supplements the methods of its parent ExecutorService with schedule, which executes a Runnable orCallable task after a specified delay. In addition, the interface defines scheduleAtFixedRate and scheduleWithFixedDelay, which executes specified tasks repeatedly, at defined intervals.

  • 相关阅读:
    年度开源盛会 ApacheCon 来临,Apache Pulsar 专场大咖齐聚
    开源流数据公司 StreamNative 正式加入 CNCF,积极推动云原生策略发展
    php摇杆Tiger摇奖
    php调试局部错误强制输出 display_errors
    php文件写入PHP_EOL与FILE_APPEND
    window自动任务实现数据库定时备份
    php 跨服务器ftp移动文件
    linux 关于session缓存丢失,自己掉坑里面了
    mysql 定时任务
    mysql 查询去重 distinct
  • 原文地址:https://www.cnblogs.com/youxin/p/2710345.html
Copyright © 2011-2022 走看看