1 int pageSize = 10000; 2 int totalCount = doctorDAO.selectDatasByMapCount2(jsonArray, false, null); 3 int m = totalCount % pageSize; 4 int pageCount = m == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1); 5 6 List<DoctorDO> resultList = new ArrayList<>(); 7 BlockingQueue<Future<List<DoctorDO>>> queue = new LinkedBlockingQueue<>(); 8 9 for (int i = 0; i < pageCount; i++) { 10 Thread.sleep(0); 11 Future<List<DoctorDO>> future; 12 future = queryDatas(jsonArray, i * pageSize, pageSize); 13 queue.add(future); 14 } 15 16 int queueSize = queue.size(); 17 logger.debug("queue size:" + queueSize); 18 for (int i = 0; i < queueSize; i++) { 19 List<DoctorDO> subAttendList = queue.take().get(); 20 if (!CollectionUtils.isEmpty(subAttendList)) { 21 resultList.addAll(subAttendList); 22 } 23 }
1 @Async 2 public Future<List<DoctorDO>> queryDatas(JSONArray jsonArray, int start, int size) { 3 List<DoctorDO> subAttendList = doctorDAO.selectDatasByMap2(start, size, jsonArray, true, null); 4 logger.info("完成任务"); 5 return new AsyncResult<>(subAttendList); 6 }
1 import org.slf4j.Logger; 2 import org.slf4j.LoggerFactory; 3 import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.scheduling.annotation.AsyncConfigurer; 6 import org.springframework.scheduling.annotation.EnableAsync; 7 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 8 9 import java.lang.reflect.Method; 10 import java.util.concurrent.Executor; 11 12 13 /** 14 * 多线程配置类 15 */ 16 @Configuration 17 @EnableAsync 18 public class ThreadConfig implements AsyncConfigurer { 19 private static Logger logger = LoggerFactory.getLogger(ThreadConfig.class); 20 21 /** 22 * The {@link Executor} instance to be used when processing async 23 * method invocations. 24 */ 25 @Override 26 public Executor getAsyncExecutor() { 27 ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); 28 executor.setCorePoolSize(15); 29 executor.setMaxPoolSize(25); 30 executor.setQueueCapacity(25); 31 executor.initialize(); 32 return executor; 33 } 34 35 /** 36 * The {@link AsyncUncaughtExceptionHandler} instance to be used 37 * when an exception is thrown during an asynchronous method execution 38 * with {@code void} return type. 39 */ 40 @Override 41 public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { 42 return new SpringAsyncExceptionHandler(); 43 } 44 45 class SpringAsyncExceptionHandler implements AsyncUncaughtExceptionHandler { 46 @Override 47 public void handleUncaughtException(Throwable throwable, Method method, Object... obj) { 48 throwable.printStackTrace(); 49 } 50 } 51 52 public static void main(String[] args) { 53 logger.info("123"); 54 logger.info("123"); 55 logger.info("123"); 56 } 57 }
原文:https://www.cnblogs.com/yangxiaobo-blog/p/11539637.html