原先使用普通for循环,然后保存数据的时候,一直显示保存数据中……急死个人!
慢的要死!现改进使用线程写
线程池管理类:
package com.awspaas.user.apps.deptbulletin.manage;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author: fangwanchen
* @Date: 2019/5/20 16:02
* @Description: 线程池管理(线程统一调度管理)
*/
public class ThreadPoolManager {
private static ThreadPoolManager sThreadPoolManager = new ThreadPoolManager();
/**
* 线程池维护线程的最少数量
*/
private static final int SIZE_CORE_POOL = 15;
/**
* 线程池维护线程的最大数量
*/
private static final int SIZE_MAX_POOL = 15;
private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
/**
* 线程池单例创建方法
*/
public static ThreadPoolManager newInstance() {
return sThreadPoolManager;
}
/**
* 线程池
*/
private final ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL, 0L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
/**
* 将构造方法访问修饰符设为私有,禁止任意实例化。
*/
private ThreadPoolManager() {}
/**
* 向线程池中添加任务方法
*/
public void addExecuteTask(Runnable task) {
System.out.println("线程池中线程数目:" + ThreadPoolManager.newInstance().getPoolSize() +
",队列中等待执行的任务数目:" + ThreadPoolManager.newInstance().getQueue() +
",已执行完别的任务数目:" + ThreadPoolManager.newInstance().getCompletedTaskCount());
if (task != null) {
mThreadPool.execute(task);
}
}
/**
* 获取缓存大小
*/
public int getQueue() {
return mThreadPool.getQueue().size();
}
/**
* 获取线程池中的线程数目
*/
public int getPoolSize() {
return mThreadPool.getPoolSize();
}
/**
* 获取已完成的任务数
*/
public long getCompletedTaskCount() {
return mThreadPool.getCompletedTaskCount();
}
/**
* 关闭线程池,不在接受新的任务,会把已接受的任务执行完
*/
public void shutdown() {
mThreadPool.shutdown();
}
}
线程类
package com.awspaas.user.apps.deptbulletin.thread;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.exception.AWSDataAccessException;
import com.awspaas.user.apps.deptbulletin.dao.BulletinRecDeptDao;
import com.awspaas.user.apps.deptbulletin.model.BulletinRecDeptModel;
import java.util.List;
/**
* @author: fangwanchen
* @Date: 2019/5/20 14:50
* @Description:写公告信息接收部门关系表的线程类
*/
public class BulletinRecDepThread implements Runnable{
/**
* 部门公告ID
*/
private String bulletinId;
/**
* 信息接收部门id数组
*/
private String[] rcvOrgIdArr;
/**
* 操作人
*/
private String userId;
/**
* 选择接收部门类型
*/
private String chosenType;
/**
* 职能类型或者级次
*/
private String condi;
BulletinRecDeptDao bulletinRecDeptDao = new BulletinRecDeptDao();
/**
* @param bulletinId
* @param rcvOrgIdArr
* @param chosenType
* @param condi :deptype或者grade
* @author fangwanchen
* @date 2019/5/20 15:52
*/
public BulletinRecDepThread(String bulletinId, String[] rcvOrgIdArr,String chosenType,String condi,String userId) {
this.bulletinId = bulletinId;
this.rcvOrgIdArr = rcvOrgIdArr ==null ? null: rcvOrgIdArr.clone();
this.chosenType = chosenType;
this.condi = condi;
this.userId = userId;
}
@Override
public void run() {
System.out.println("正在执行...BulletinRecDepThread:写公告接收部门表(BO_EU_BULLETINRECDEPT)的线程");
try {
//1.根据条件取到所有部门id和部门名称
List<RowMap> deptList = bulletinRecDeptDao.selectDepByCondi(chosenType,condi);
System.out.println("----开始写数据到公告接收部门表(BO_EU_BULLETINRECDEPT)中,总数:" + deptList.size());
//2.循环写数据到联合惩戒接收部门关系表(BO_EU_JD_RELATION)中
int j = 0;
for (RowMap deptModel : deptList) {
BulletinRecDeptModel recModel = new BulletinRecDeptModel();
recModel.setId(UUIDGener.getUUID());
recModel.setCreateUser(userId);
recModel.setUpdateUser(userId);
recModel.setBulletinId(bulletinId);
recModel.setRecDeptId(deptModel.getString("ID"));
recModel.setRecDeptName(deptModel.getString("DEPARTMENTNAME"));
bulletinRecDeptDao.insert(recModel);
}
System.out.println("----写数据到公告接收部门表(BO_EU_BULLETINRECDEPT)完成");
} catch (AWSDataAccessException e) {
e.printStackTrace();
}
System.out.println("线程执行完毕...");
}
}
在步骤1-2之间,最好插入一个信息插入情况的表,来显示执行的完成度,保证数据的完整性。
这次太忙了就算了叭…能省就省………………