一. 使用场景
1)例如export导出10万条数据,一般的写法是直接捞数据库然后导出,耗时太长;
采用多线程的方式,开100个线程,一个线程处理1000条数据,这样大大缩短了时间,将这1000个线程放入同一个线程池中,start开启之后,1000个线程同时开始,以耗时时间最长的一个线程的时间为最终时间,大大提升了用户的使用体验。
2)在做mes的unit 待生产和已生产看板的时候,出现刷新页面长达8秒的情况,现将wait和start的unit,分别用一个线程(放入同一个线程池)去获取unit列表信息,以用时最长的一个线程 的时间为最终消耗时间。【这个场景需要获取线程执行后的返回值,所以使用 实现 Callable<T>的方式,如果想要传入参数,需要为这个类添加成员属性,和构造方法,call 方法中才可以使用这个变量】
package com.ags.meper.examples.task.queryTask;
import com.ags.meper.examples.service.ISoOrderFetchService;
import com.ags.meperframework.core.spring.BeanManager;
import com.ags.meperframework.suites.mes.sdk.entity.dto.WIPWrapper;
import com.ags.meperframework.suites.mes.sdk.service.IUnitService;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
public class StartedQueryTask implements Callable<List<WIPWrapper>> {
private Long workStationId;
public StartedQueryTask(Long workStationId) {
this.workStationId = workStationId;
}
@Override
public List<WIPWrapper> call() throws Exception {
System.out.println("StartedQueryTask 开始"+new Date());
ISoOrderFetchService soOrderFetchService = BeanManager.getService(ISoOrderFetchService.class);
IUnitService unitService = BeanManager.getService(IUnitService.class);
List<Long> orderList = soOrderFetchService.unClosedWorkOrderList();
List<WIPWrapper> wipWrappers = unitService.listStartedUnitsByWorkStationId(workStationId, orderList);
System.out.println("StartedQueryTask 结束"+new Date());
return wipWrappers;
}
}
package com.ags.meper.examples.task.queryTask;
import com.ags.meper.examples.service.ISoOrderFetchService;
import com.ags.meperframework.core.spring.BeanManager;
import com.ags.meperframework.suites.mes.sdk.entity.dto.WIPWrapper;
import com.ags.meperframework.suites.mes.sdk.service.IUnitService;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
public class WaittingQueryTask implements Callable<List<WIPWrapper>> {
private Long workStationId;
public WaittingQueryTask(Long workStationId) {
this.workStationId = workStationId;
}
@Override
public List<WIPWrapper> call() throws Exception {
System.out.println("WaittingQueryTask 开始"+new Date());
ISoOrderFetchService soOrderFetchService = BeanManager.getService(ISoOrderFetchService.class);
IUnitService unitService = BeanManager.getService(IUnitService.class);
List<Long> workOrderIdList = soOrderFetchService.unClosedWorkOrderList();
List<WIPWrapper> wipWrapperList = unitService.listUnitToProductByWorkStationId(workStationId, workOrderIdList, 0, 1000);
System.out.println("WaittingQueryTask 结束"+new Date());
return wipWrapperList;
}
}
public void callableRefreshGrid(){
//创建线程池
ExecutorService executorService=Executors.newCachedThreadPool();
//创建 callable对象
Callable<List<WIPWrapper>> callable=new WaittingQueryTask(stationCookieManager.getCurrentWorkStation().getId());
//创建 callable对象
Callable<List<WIPWrapper>> callable2=new StartedQueryTask(stationCookieManager.getCurrentWorkStation().getId());
//将对象放入同一个线程池中,提交等待执行结果返回Future对象,执行顺序应该是,两个线程同时开始,结束时间看哪个线程执行的速度慢,以耗时最长的线程执行的时间为最终耗时时间。
Future future=executorService.submit(callable);
Future future2=executorService.submit(callable2);
try {
// if(future.isDone()){
List<WIPWrapper> wipWrapperList = (List<WIPWrapper>)future.get();
ListDataProvider<WIPWrapper> ofCollection = DataProvider.ofCollection(wipWrapperList);
this.gridSchedule.setDataProvider(ofCollection);
// }
} catch (Exception e) {
e.printStackTrace();
}
try {
// if(future2.isDone()){
List<WIPWrapper> wipWrapperList = (List<WIPWrapper>)future2.get();
ListDataProvider<WIPWrapper> ofCollection = DataProvider.ofCollection(wipWrapperList);
this.gridInStep.setDataProvider(ofCollection);
// }
} catch (Exception e) {
e.printStackTrace();
}
}
将对象放入同一个线程池中,提交等待执行结果返回Future对象,执行顺序应该是,两个线程同时开始,结束时间看哪个线程执行的速度慢,以耗时最长的线程执行的时间为最终耗时时间。
线程执行结果: (放在同一个线程池中的各个线程同时开始,以最后结束的时间为耗时时间)
WaittingQueryTask 开始Mon Aug 07 16:56:02 CST 2023
StartedQueryTask 开始Mon Aug 07 16:56:02 CST 2023
StartedQueryTask 结束Mon Aug 07 16:56:03 CST 2023
WaittingQueryTask 结束Mon Aug 07 16:56:03 CST 2023