美文网首页
线程相关(七)线程池ExecutorService

线程相关(七)线程池ExecutorService

作者: 云鲸鱼rain | 来源:发表于2019-03-28 12:18 被阅读0次
package com.tianci.demo.mybatis.thread;

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

/**
 * 练习线程池
 */
public class ThreadPoolTest {

    /**
     * 为什么不用new thread,而要用ExecutorSerivice
     * new thread坏处
     * 1. new Thread 性能差
     * 2. 缺乏统一管理,相互之间竞争,可能占用过多系统资源导致死机
     * 3. 功能少,不能定时执行,定期执行
     * ExecutorService好处
     * 1. 减少对象创建消亡开销
     * 2. 有效控制最大并发线程数,提高系统资源利用率,避免过多资源竞争,避免堵塞
     * 3. 提供很多功能,定时执行,定期执行
     */

    /**
     * corePoolSize:核心线程数,创建之后不会释放。
     * maximumPoolSize:最大线程数。
     * keepAliveTime:线程空闲时所允许保存的最大时间,超过时间,非核心线程将被释放。
     * unit:时间单位。
     * workQueue:任务队列,存储暂时无法执行的任务。
     */
    public static void main(String[] args) {
        /* return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>()); */
        /*可缓存线程池:大量耗时任务不适合,适合生命周期短的任务。*/
        ExecutorService cachePool = Executors.newCachedThreadPool();


        /*return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));*/
        /*单线程池:保证任务按顺序执行*/
        ExecutorService singlePool = Executors.newSingleThreadExecutor();


        /*return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());*/
        /*固定线程数线程池*/
        ExecutorService fixedPool = Executors.newFixedThreadPool(3);


        /*super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue());*/
        /*固定线程数,支持定时和周期性任务*/
        ExecutorService schedulePool = Executors.newScheduledThreadPool(5);



        
    }

}

execute,submit,shundown,awaitTermination方法
先说execute和submit
上源码

public interface Executor {
    void execute(Runnable command);
}
public interface ExecutorService extends Executor {
    Future<?> submit(Runnable task);
    <T> Future<T> submit(Runnable task, T result);
    <T> Future<T> submit(Callable<T> task);
}
  1. 入参不一样
  2. 一个有返回值,一个没有
  3. submit可以进行exception处理

execute的入参是Runnable类,submit的入参是Runnable和Callable。
上callable源码

@FunctionalInterface
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

submit有返回值,可以根据返回值判断逻辑,继续下面的操作。
看callable的源码也知道实现call方法后,可以抛出异常,进行exception处理。

再说shundown和awaitTermination
shundown:平滑的关闭线程池,停止接受新任务,等待已提交的任务执行完毕后关闭线程池。
awaitTermination:当等待超过设定时间时,检测线程池是否关闭,关闭返回true,否则返回false。

相关文章

网友评论

      本文标题:线程相关(七)线程池ExecutorService

      本文链接:https://www.haomeiwen.com/subject/lgcqbqtx.html