我是靠谱客的博主 烂漫黄豆,最近开发中收集的这篇文章主要介绍java线程池 —— 类继承结构及实现方式类继承结构线程池创建Runnable 和 Callable,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

类继承结构Executor类继承图

线程池创建

  • Executors
ExecutorService service = Executors.newFixedThreadPool(int nThreads);
ExecutorCompletionService completionService = new ExecutorCompletionService(service);
service.submit(...);
service.execute(...);

ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  • ThreadPoolExecutor
ThreadPoolExecutor executor = new ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,  long keepAliveTime, TimeUnit unit,  BlockingQueue<Runnable> workQueue);
ThreadPoolExecutor executor = new ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,  long keepAliveTime, TimeUnit unit,  BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory);
ThreadPoolExecutor executor = new ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,  long keepAliveTime, TimeUnit unit,  BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler);
  • ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(int corePoolSize);
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory);
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler);
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler);

Runnable 和 Callable

public interface Callable<V> {
    V call() throws Exception;
}

public interface Runnable {
    public abstract void run();
}
  • 区别:
    a. Callable接口的call()方法可以有返回值(返回值通过Future接口获取),而Runnable接口的run()方法没有返回值。
    b. Callable接口的call()方法可以声明抛出异常,而Runnable接口的run()方法不可以。

最后

以上就是烂漫黄豆为你收集整理的java线程池 —— 类继承结构及实现方式类继承结构线程池创建Runnable 和 Callable的全部内容,希望文章能够帮你解决java线程池 —— 类继承结构及实现方式类继承结构线程池创建Runnable 和 Callable所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部