多线程优化为线程池

This commit is contained in:
thinkgem
2022-03-20 14:11:35 +08:00
parent 41da9ef5cb
commit 87e8352418
3 changed files with 24 additions and 9 deletions

View File

@@ -8,6 +8,10 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -28,6 +32,8 @@ import com.jeesite.modules.cms.utils.CmsUtils;
import com.jeesite.modules.file.utils.FileUploadUtils; import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.utils.UserUtils; import com.jeesite.modules.sys.utils.UserUtils;
import io.netty.util.concurrent.DefaultThreadFactory;
/** /**
* 文章表Service * 文章表Service
* @author 长春叭哥、ThinkGem * @author 长春叭哥、ThinkGem
@@ -39,6 +45,10 @@ public class ArticleService extends CrudService<ArticleDao, Article> {
@Autowired @Autowired
private ArticleDataDao articleDataDao; private ArticleDataDao articleDataDao;
private static ExecutorService updateExpiredWeightThreadPool = new ThreadPoolExecutor(5, 20,
60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
new DefaultThreadFactory("cms-update-expired-weight"));
/** /**
* 获取单条数据 * 获取单条数据
@@ -77,14 +87,12 @@ public class ArticleService extends CrudService<ArticleDao, Article> {
*/ */
@Override @Override
public Page<Article> findPage(Article article) { public Page<Article> findPage(Article article) {
Thread thread = new Thread("cms-update-expired-weight") { updateExpiredWeightThreadPool.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
updateExpiredWeight(article); updateExpiredWeight(article);
} }
}; });
thread.setDaemon(true);
thread.start();
return super.findPage(article); return super.findPage(article);
} }

View File

@@ -6,6 +6,10 @@ package com.jeesite.modules.msg.service;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -34,6 +38,8 @@ import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User; import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService; import com.jeesite.modules.sys.service.EmpUserService;
import io.netty.util.concurrent.DefaultThreadFactory;
/** /**
* 内部消息Service * 内部消息Service
* @author ThinkGem * @author ThinkGem
@@ -47,6 +53,10 @@ public class MsgInnerService extends CrudService<MsgInnerDao, MsgInner> {
private EmpUserService empUserService; private EmpUserService empUserService;
@Autowired @Autowired
private MsgInnerRecordDao msgInnerRecordDao; private MsgInnerRecordDao msgInnerRecordDao;
private static ExecutorService msgPushThreadPool = new ThreadPoolExecutor(5, 20,
60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
new DefaultThreadFactory("cms-update-expired-weight"));
/** /**
* 获取单条数据 * 获取单条数据
@@ -191,7 +201,7 @@ public class MsgInnerService extends CrudService<MsgInnerDao, MsgInner> {
}); });
// 手动触发消息推送任务 // 手动触发消息推送任务
if (Global.TRUE.equals(Global.getProperty("msg.realtime.enabled"))){ if (Global.TRUE.equals(Global.getProperty("msg.realtime.enabled"))){
Thread thread = new Thread("msg-push-task-execute"){ msgPushThreadPool.submit(new Runnable() {
public void run() { public void run() {
try{ try{
MsgPushUtils.getMsgPushTask().execute(); MsgPushUtils.getMsgPushTask().execute();
@@ -199,9 +209,7 @@ public class MsgInnerService extends CrudService<MsgInnerDao, MsgInner> {
logger.error("实时消息发送失败,推送服务配置不正确。", ex); logger.error("实时消息发送失败,推送服务配置不正确。", ex);
} }
} }
}; });
thread.setDaemon(true);
thread.start();
} }
} }

View File

@@ -48,7 +48,6 @@ import io.netty.util.concurrent.DefaultThreadFactory;
*/ */
public class LogUtils { public class LogUtils {
// 采用线程池优化性能
private static ExecutorService logThreadPool = new ThreadPoolExecutor(5, 20, private static ExecutorService logThreadPool = new ThreadPoolExecutor(5, 20,
60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
new DefaultThreadFactory("log-save")); new DefaultThreadFactory("log-save"));