Executor

2020-04-16  本文已影响0人  桑鱼nicoo

JavaSE5 的 java.util.concurrent 包中的执行器( Executor ) 管理Thread对象。从而简化了并发编程。
Executor在客户端和任务执行之间提供了一个间接层;与客户端直接执行任务不同,这个中介对象将执行任务。
Executor允许你管理异步任务的执行,而无须显式地管理线程的生命周期。Executor在JavaSE5/6中是启动任务的优选方法
---节选《Java编程思想》

CachedThreadPool()创建 ExecutorService

newCachedThreadPool()将为每个任务都创建一个线程

 ExecutorService exec = Executors.newCachedThreadPool();

源码

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
}

FixedThreadPool()创建 ExecutorService

FixedThreadPool预先执行代价高昂的线程分配,限制线程的数量,不需要为每个任务都固定地付出创建线程的开销,需要线程的处理器,通过直接从池中获取线程。在任何线程池中,现有线程在可能的情况下,都会被自动复用

 ExecutorService exec = Executors.newFixedThreadPool(5)

SingleThreadExecutor()创建 ExecutorService

SingleThreadExecutor 就像线程数量为1的 FixedThreadPool 。如果希望在另一个线程中连续运行任务可以使用,例如监听进入套接字连接的任务或者是更新本地、远程日志的小任务。如果想SingleThreadExecutor中提交了多个任务,那么这些任务将会排队,每个任务都会在下一个任务开始之前运行结束,所有任务都使用相同的线程

 ExecutorService exec = Executors.newSingleThreadExecutor(5)

示例

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Executor用法
 * User: peng
 * Date: 2020/4/16
 * Time: 上午10:56
 */
public class TestExecutor {
    public static void main(String[] args) {
        /**
         * ExecutorService(具有服务生命周期的Executor,例如关闭)知道如何构建恰当的上下文来执行Runnable对象
         * CachedThreadPool为每个任务都创建一个线程
         * ExecutorService是使用静态Executor方法创建的,常见的情况是创建单个的Executor用来创建和管理系统中的所有任务
         */
        ExecutorService exec = Executors.newCachedThreadPool();
        for(int i = 0; i < 5; i++){
            exec.execute(new LiftOff());
        }
        exec.shutdown(); // 对shutdown的调用可以防止新任务提交给Executor,运行完Executor中所有任务后会尽快退出
        System.out.println("主线程");
    }
}

class LiftOff implements Runnable{

    protected int countDown = 10;
    private static int taskCount = 0; // taskCount是statis的,与实例无关
    private final int id = taskCount++; // 用来区分任务的多个实例,是final的一旦被初始化之后就不会被修改
    public LiftOff(){}
    public LiftOff(int countDown){
        this.countDown = countDown;
    }

    public String status(){
        return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!") + "), ";
    }

    public void run() {
        while (countDown-- > 0){ // run方法总会有某种形式的循环,使得任务一直运行下去直到不再需要,所以要设定跳出循环的条件
            System.out.println(status());
            Thread.yield(); // 让出线程,可以将CPU从一个线程转移给另一个线程
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读