java 线程池简述 Executor

资料来源于 JDK 1.8 API

UML

在这里插入图片描述

Executor 执行器

public interface Executor {

    void execute(Runnable command);
    
}

ExecutorService

核心方法

public interface ExecutorService extends Executor {

	/**
		提交值返回任务以执行,并返回代表任务待处理结果的Future。 未来的get方法将在成功完成后返回任务的结果。
		如果您想立即阻止等待任务,您可以使用result = exec.submit(aCallable).get();格式的result = exec.submit(aCallable).get();
		注意: Executors类包括一组方法,可以将一些其他常见的类似对象的对象,例如PrivilegedAction转换为Callable表单,以便它们可以提交。
	*/
    <T> Future<T> submit(Callable<T> task);
	/**
		提交一个可运行的任务执行,并返回一个表示该任务的未来。 未来的get方法将在成功完成后返回给定的结果。
	*/
    <T> Future<T> submit(Runnable task, T result);

	/**
		提交一个可运行的任务执行,并返回一个表示该任务的未来。 未来的get方法将返回null 成功完成时。
	*/
    Future<?> submit(Runnable task);

}

Executors 工具类

public class Executors {

    /** Cannot instantiate. */
    private Executors() {}

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

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

	public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPool
            (Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }

	public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

	public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

	public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1));
    }

	public static ExecutorService unconfigurableExecutorService(ExecutorService executor) {
        if (executor == null)
            throw new NullPointerException();
        return new DelegatedExecutorService(executor);
    }

	public static ThreadFactory defaultThreadFactory() {
        return new DefaultThreadFactory();
    }

	public static ThreadFactory privilegedThreadFactory() {
        return new PrivilegedThreadFactory();
    }

	public static <T> Callable<T> callable(Runnable task, T result) {}

	public static Callable<Object> callable(Runnable task) {}

	
}

ThreadFactory 线程工厂

/**
	class SimpleThreadFactory implements ThreadFactory {
	   public Thread newThread(Runnable r) {
	     return new Thread(r);
	   }
	 }}
*/
public interface ThreadFactory {

    Thread newThread(Runnable r);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值