Java线程池原理和场景应用

1 篇文章 0 订阅
1 篇文章 0 订阅

1.线程池的原理

        线程池的主要工作是控制运行线程的数量,将任务放入队列中,然后在线程创建后启动这些任务,如果线程数量超过了最大数量,超出的数量的线程进行排队等候,等待其他线程执行完毕,再从队列中取出任务来执行,超出队列的线程则会进行相应的拒绝策略。主要特点:线程复用、控制最大并发数、管理线程。

2.线程池的组成

ThreadPoolExecutor构造方法:

 public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

相关参数说明:

1.corePoolSize:核心线程数量

2.maximumPoolSize:最大线程数量

3.keepAliveTime:当线程数量超过核心线程时,多余空闲线程存活时间,即多少时间销毁

4.unit:keepAliveTime的单位

5.workQueue:任务队列 多余的线程放入队列中

6.threadFactory:线程工厂,用于创建线程 一般默认

7.handler:拒绝策略,任务太多来不及处理,默认:new AbortPolicy() 抛出异常

3.拒绝策略

3.1 AbortPolicy:直接抛出异常,阻止系统正常运行。

    public static class AbortPolicy implements RejectedExecutionHandler {
        /**
         * Creates an {@code AbortPolicy}.
         */
        public AbortPolicy() { }

        /**
         * Always throws RejectedExecutionException.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         * @throws RejectedExecutionException always.
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() +
                                                 " rejected from " +
                                                 e.toString());
        }
    }

3.2 CallerRunsPolicy:只要线程池未关闭,该策略直接在调用者线程中,运行当前丢弃的任务。

public static class CallerRunsPolicy implements RejectedExecutionHandler {
        /**
         * Creates a {@code CallerRunsPolicy}.
         */
        public CallerRunsPolicy() { }

        /**
         * Executes task r in the caller's thread, unless the executor
         * has been shut down, in which case the task is discarded.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                r.run();
            }
        }
    }

3.3 DiscardOldestPolicy:丢弃最老的请求,也就是即将被执行的任务,并尝试再次提交的任务。

public static class DiscardOldestPolicy implements RejectedExecutionHandler {
        /**
         * Creates a {@code DiscardOldestPolicy} for the given executor.
         */
        public DiscardOldestPolicy() { }

        /**
         * Obtains and ignores the next task that the executor
         * would otherwise execute, if one is immediately available,
         * and then retries execution of task r, unless the executor
         * is shut down, in which case task r is instead discarded.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                e.getQueue().poll();
                e.execute(r);
            }
        }
    }

3.4:DiscardPolicy:丢弃该任务,不做处理。

public static class DiscardPolicy implements RejectedExecutionHandler {
        /**
         * Creates a {@code DiscardPolicy}.
         */
        public DiscardPolicy() { }

        /**
         * Does nothing, which has the effect of discarding task r.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        }
    }

4.java线程池工作过程

4.1 线程池创建时,里面是没有线程的,任务队列是作为参数传进来的。

4.2 当调用execute(Runnable task)时,线程池会进行下面判断:

  • 如果正在运行线程数量小于corePoolSize,马上创建线程运行这个任务
  • 如果正在运行线程数量大于等于corePoolSize,将任务放入队列中
  • 如果这时队列满了,而且运行线程数量小于maximumPoolSize,创建非核心线程运行任务
  • 如果队列满了,并且运行的线程数量大于等于maximumPoolSize,线程池会抛出异常

4.3 当一个线程完成任务时,它会从队列取下一个任务来执行

4.4 当线程无事可做时,超过一定的keepAliveTime,线程池会判断,如果当前线程数大于corePoolSize,那么这个线程就会被销毁,最终线程数量会等于corePoolSize大小

5.线程池场景应用

作为异步线程调用,例如:邮件发送、文章、卡片推送  提高响应速度

代码实现:只需要在对应的service 方法中增加相应的注解:@Async("asynExecutor")

package com;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 异步线程类
 * @author guang
 */
@Configuration
@EnableAsync
public class SpringConfig implements AsyncConfigurer{

	@Override
	@Bean(name="asynExecutor")
	public Executor getAsyncExecutor() {
		// TODO Auto-generated method stub
		//线程池创建
		ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
		pool.setCorePoolSize(10);
		pool.setMaxPoolSize(200);
		pool.setQueueCapacity(25);
		pool.setThreadNamePrefix("taskExecutor->");
		pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		//等待所有任务完成后,再继续销毁其他的bean
		pool.setWaitForTasksToCompleteOnShutdown(true);
		//线程等待时间 超时则销毁 而不是阻塞
		pool.setAwaitTerminationSeconds(60);
		pool.initialize();
		return pool;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		// TODO Auto-generated method stub
		return new SimpleAsyncUncaughtExceptionHandler();
	}
		
}

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值