JAVA-线程池(精简版)

线程池:

提示:线程池相关必备内容

两种创建方式:

  1. Executors(项目中不推荐使用)
  2. ThreadPoolExecutor

ThreadPoolTaskExecutor 是 Spring 的线程池技术,在spring core包中,ThreadPoolTaskExecutor是对ThreadPoolExecutor进行了封装处理。

七个核心参数:

  1. corePoolSize
  2. maximumPoolSize
  3. keepAliveTime
  4. timeUnit
  5. blockingQueue
  6. threadFactory
  7. rejectedExecutionHandler
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler){
...
}

决绝策略:

  1. AbortPolicy (默认的策略)
  2. CallerRunsPolicy
  3. DiscardPolicy
  4. DiscardOldestPolicy

(1)ThreadPoolExecutor.AbortPolicy —— 默认的策略,它会抛出“拒绝执行”异常。
(2)ThreadPoolExecutor.CallerRunsPolicy —— 它在执行方法的调用线程中直接运行被拒绝的任务,除非已关闭执行器,在这种情况下,该任务将被丢弃。
(3)ThreadPoolExecutor.DiscardPolicy —— 它会默默地丢弃被拒绝的任务。
(4)ThreadPoolExecutor.DiscardOldestPolicy —— 它会丢弃最旧的未处理请求,然后重试执行,除非执行器已关闭,在这种情况下,任务将被丢弃。


线程池状态:

  1. RUNNING
  2. SHUTDOWN
  3. STOP
  4. TIDYING
  5. TERMINATED

execute()和submit()

execute方法

Executor接口中

public interface Executor {
    void execute(Runnable command);
}
submit方法

ExecutorService接口中

public interface ExecutorService extends Executor {
	<T> Future<T> submit(Callable<T> task);
	<T> Future<T> submit(Runnable task, T result);
	Future<?> submit(Runnable task);
}

执行任务流程

public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值