Java代码:用定时任务执行任务,现在有20000条数据,我想每500条数据交给一个线程处理,要注意异常处理。请用ThreadPoolExecutor实现
碰到的问题
- 有些bean比如mapper不知道怎么传入task:可以从构造器传入
- 任务处理写得有问题,发到线上中断不了
- 异常处理:对于风险语句要try住,比如用了catchDownLatch之后减一操作一定要咋finally里执行。不要阻塞住
public class Main {
public static void main(String[] args) {
List<String> dataList = createDataList();
int batchSize = 500; // 划分的每个小任务包含的数据条数
int nThreads = (dataList.size() + batchSize - 1) / batchSize;
TaskExecutor taskExecutor = new TaskExecutor(nThreads);
List<Runnable> tasks = new ArrayList<>();
for (int i = 0; i < nThreads; i++) {
int start = i * batchSize;
int end = Math.min((i + 1) * batchSize, dataList.size());
List<String> subList = dataList.subList(start, end);
Runnable task = new DataProcessTask(subList);
tasks.add(task);
}
taskExecutor.executeTasks(tasks);
taskExecutor.shutdown();
}
private static List<String> createDataList() {
List<String> dataList = new ArrayList<>();
for (int i = 0; i < 20000; i++) {
dataList.add("data-" + i);
}
return dataList;
}
}
class DataProcessTask implements Runnable {
private final List<String> data;
public DataProcessTask(List<String> data) {
this.data = data;
}
@Override
public void run() {
for (String d : data) {
try {
// 处理数据
Thread.sleep(10);
System.out.println(Thread.currentThread().getName() + " process data: " + d);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
// 处理任务执行异常
}
}
}
}
class TaskExecutor {
private final ExecutorService executorService;
public TaskExecutor(int nThreads) {
this.executorService = Executors.newFixedThreadPool(nThreads);
}
public void executeTasks(List<Runnable> tasks) {
List<Future<?>> futures = new ArrayList<>();
for (Runnable task : tasks) {
Future<?> future = executorService.submit(task);
futures.add(future);
}
for (Future<?> future : futures) {
try {
future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
// 处理任务执行异常
}
}
}
public void shutdown() {
executorService.shutdown();
}
}