自定义拒绝策略、自定义ThreadFactory、自定义线程池
package com.wucj.controller.thread;
import com.alibaba.fastjson.JSON;
import com.wucj.entity.MessageEntity;
import com.wucj.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* \* Created with IntelliJ IDEA.
* \* @author: wucj
* \* @date: 2020/1/27 14:49
* \* @description:
* \
*/
@Slf4j
public class ExcutorServiceSingle{
/** 单例 **/
private static ExcutorServiceSingle instance = null;
/** 线程池管理类:使用自定义的拒绝策略**/
private static ExecutorService executorService =
new ThreadPoolExecutor(2, 5, 5,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(10),
new MessageThreadFactory(),
new MessageRejectedExecutionHandler());
private ExcutorServiceSingle() {
}
/** 获取单例 **/
public static ExcutorServiceSingle getInstance(){
if(null==instance){
synchronized(ExcutorServiceSingle.class){
instance = new ExcutorServiceSingle();
}
}
return instance;
}
/** 发起异步执行 **/
public void excutor(MessageEntity messageEntity){
MessageRunable messageRunable = new MessageRunable(messageEntity);
executorService.submit(messageRunable);
}
/**
* Attempts to stop all actively executing tasks, halts the
* processing of waiting tasks, and returns a list of the tasks
* that were awaiting execution.
*
* <p>This method does not wait for actively executing tasks to
* terminate.
*
* <p>There are no guarantees beyond best-effort attempts to stop
* processing actively executing tasks. For example, typical
* implementations will cancel via {@link Thread#interrupt}, so any
* task that fails to respond to interrupts may never terminate.
*
* @return list of tasks that never commenced execution
* @throws SecurityException if a security manager exists and
* shutting down this ExecutorService may manipulate
* threads that the caller is not permitted to modify
* because it does not hold {@link
* java.lang.RuntimePermission}{@code ("modifyThread")},
* or the security manager's {@code checkAccess} method
* denies access.
*/
public void shutDownNow(){
List<Runnable> messageRunableList = executorService.shutdownNow();
if(CollectionUtils.isEmpty(messageRunableList)){
log.info("线程池关闭时,不存在正在执行的线程...");
}else{
String uuid = UUIDUtils.create();
log.info("{}线程池关闭时,存在正在执行的线程...",uuid);
for (Runnable runnable : messageRunableList) {
log.info("{}线程池关闭时,存在线程:{}",uuid, JSON.toJSONString(runnable));
}
}
}
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
* <p>This method does not wait for previously submitted tasks to
* complete execution.
*
* @throws SecurityException {@inheritDoc}
*/
public void shutDown(){
executorService.shutdown();
}
/**
* Returns {@code true} if this executor has been shut down.
*
* @return {@code true} if this executor has been shut down
*/
public boolean isShutDown(){
return executorService.isShutdown();
}
}
package com.wucj.controller.thread;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
/**
* \* Created with IntelliJ IDEA.
* \* @author: wucj
* \* @date: 2020/1/27 16:33
* \* @description: 自定义线程池丢弃策略
* \
*/
@Slf4j
public class MessageRejectedExecutionHandler implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
log.info("消息推送超过异步任务的最大长度,执行拒绝策略,{}",executor.toString());
}
}
package com.wucj.controller.thread;
import com.alibaba.fastjson.JSON;
import com.wucj.entity.MessageEntity;
import com.wucj.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
/**
* \* Created with IntelliJ IDEA.
* \* @author: wucj
* \* @date: 2020/1/27 14:55
* \* @description:
* \
*/
@Slf4j
public class MessageRunable implements Runnable{
private MessageEntity messageEntity;
public MessageRunable(MessageEntity messageEntity) {
this.messageEntity = messageEntity;
}
@Override
public void run() {
String uuid = UUIDUtils.create();
log.info("{}发送消息:{}",uuid, JSON.toJSONString(messageEntity));
try {
TimeUnit.SECONDS.sleep(60);
} catch (InterruptedException e) {
log.error("{}消息发送异常:{}",uuid,e);
}
log.info("{}消息发送完成",uuid);
}
}
package com.wucj.controller.thread;
import java.util.concurrent.ThreadFactory;
/**
* \* Created with IntelliJ IDEA.
* \* @author: wucj
* \* @date: 2020/1/27 15:26
* \* @description: 线程组线程管理类
* \
*/
public class MessageThreadFactory implements ThreadFactory {
/** 线程所属组名称 **/
private String groupName = "消息发送线程组";
/** 线程名称 **/
private String threadName = "消息发送线程名称";
/** 栈大小 **/
private int stackSize = 0;
/** 线程组 **/
private ThreadGroup threadGroup = new ThreadGroup(groupName);
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(threadGroup,r,threadName,stackSize);
if (t.isDaemon()){
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY){
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}
测试
@Data
public class MessageEntity {
private String message;
private String toUserName;
}
package com.wucj.controller;
import com.wucj.base.BaseResult;
import com.wucj.controller.thread.ExcutorServiceSingle;
import com.wucj.entity.MessageEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
/**
* \* Created with IntelliJ IDEA.
* \* @author: wucj
* \* @date: 2020/1/21 20:12
* \* @description:
* \
*/
@RestController
@Slf4j
@RequestMapping("api/thread")
public class ThreadController {
@GetMapping("info")
public BaseResult nowThreadInfo(){
ThreadGroup group = Thread.currentThread().getThreadGroup();
log.info("当前线程组的名称:",group.getName());
Thread[] list1 = new Thread[group.activeCount()];
group.enumerate(list1);
for (Thread thread:list1){
log.info("分组:{},名称:{}",group.getName(),thread.getName());
}
ThreadGroup groupParent = group.getParent();
log.info("当前线程组的名称:",groupParent.getName());
Thread[] list2 = new Thread[groupParent.activeCount()];
groupParent.enumerate(list2);
for (Thread thread:list2){
log.info("分组:{},名称:{}",groupParent.getName(),thread.getName());
}
ThreadGroup groupParentT = groupParent.getParent();
return BaseResult.success("操作成功");
}
@PostMapping("excutor")
public BaseResult threadExcutoer(@RequestBody MessageEntity messageEntity){
ExcutorServiceSingle excutorServiceSingle = ExcutorServiceSingle.getInstance();
excutorServiceSingle.excutor(messageEntity);
return BaseResult.success("操作成功");
}
@GetMapping("shutDownNow")
public BaseResult shutDownNow(){
ExcutorServiceSingle excutorServiceSingle = ExcutorServiceSingle.getInstance();
try{
excutorServiceSingle.shutDownNow();
return BaseResult.success("停止成功");
}catch (Exception e){
log.error("停止线程池异常:{}",e);
return BaseResult.success("停止异常");
}
}
@GetMapping("shutDown")
public BaseResult shutDown(){
ExcutorServiceSingle excutorServiceSingle = ExcutorServiceSingle.getInstance();
try{
excutorServiceSingle.shutDown();
return BaseResult.success("尝试停止成功");
}catch (Exception e){
log.error("尝试关闭线程池异常:{}",e);
return BaseResult.success("尝试停止异常");
}
}
@GetMapping("isShutDown")
public BaseResult isShutDown(){
ExcutorServiceSingle excutorServiceSingle = ExcutorServiceSingle.getInstance();
try{
boolean bool = excutorServiceSingle.isShutDown();
if(bool){
return BaseResult.success("线程池已经停止");
}else{
return BaseResult.success("线程池未停止");
}
}catch (Exception e){
log.error("检查是否已经停止异常:{}",e);
return BaseResult.success("检查是否已经停止异常");
}
}
}