ITPub博客

首页 > 应用开发 > Java > java编写一个任务分片调度器(并发 + 分批 + 重试)

java编写一个任务分片调度器(并发 + 分批 + 重试)

原创 Java 作者:mingtian66 时间:2025-05-30 11:39:48 0 删除 编辑

以下是Java实现的任务分片调度器完整解决方案,包含任务分片、并发执行、分批处理和自动重试机制:


import java.util.*;

import java.util.concurrent.*;

import java.util.function.Function;


public class TaskChunkScheduler<T, R> {

    private final ExecutorService executor;

    private final int batchSize;

    private final int maxRetries;

    private final long retryDelayMs;


    public TaskChunkScheduler(int threadCount, int batchSize, 

                            int maxRetries, long retryDelayMs) {

        this.executor = Executors.newFixedThreadPool(threadCount);

        this.batchSize = batchSize;

        this.maxRetries = maxRetries;

        this.retryDelayMs = retryDelayMs;

    }


    public List<R> execute(List<T> tasks, Function<T, R> processor) 

            throws InterruptedException {

        List<Future<R>> futures = new ArrayList<>();

        List<List<T>> batches = partition(tasks, batchSize);

        

        for (List<T> batch : batches) {

            for (T task : batch) {

                futures.add(executor.submit(() -> 

                    processWithRetry(task, processor)));

            }

        }

        

        return getResults(futures);

    }

    

    private R processWithRetry(T task, Function<T, R> processor) {

        int attempts = 0;

        while (true) {

            try {

                return processor.apply(task);

            } catch (Exception e) {

                if (++attempts > maxRetries) {

                    throw new RuntimeException("Max retries exceeded", e);

                }

                try {

                    Thread.sleep(retryDelayMs);

                } catch (InterruptedException ie) {

                    Thread.currentThread().interrupt();

                    throw new RuntimeException("Retry interrupted", ie);

                }

            }

        }

    }

    

    private static <T> List<List<T>> partition(List<T> list, int size) {

        List<List<T>> partitions = new ArrayList<>();

        for (int i = 0; i < list.size(); i += size) {

            partitions.add(list.subList(i, Math.min(i + size, list.size())));

        }

        return partitions;

    }

    

    private List<R> getResults(List<Future<R>> futures) 

            throws InterruptedException {

        List<R> results = new ArrayList<>();

        for (Future<R> future : futures) {

            try {

                results.add(future.get());

            } catch (ExecutionException e) {

                throw new RuntimeException("Task execution failed", e);

            }

        }

        return results;

    }

    

    public void shutdown() {

        executor.shutdown();

    }

}

电话13388885782 电话13388885782 电话13388885782 电话13388885782 电话13388885782 www.itl222.com 电话13388885782 www.itl5553.com 电话13388885782 www.itl567.com 电话13388885782 www.itl666.com 电话13388885782 www.itl996.com 电话13388885782 www.tl1001.com 电话13388885782 www.tl1002.com 电话13388885782 www.tl1003.com 电话13388885782 www.itl1001.com 电话13388885782 www.itl1003.com 电话13388885782 电话13388885782 电话13388885782 电话13388885782 电话13388885782 电话13388885782 电话13388885782

@FunctionalInterface

public interface TaskProcessor<T, R> {

    R process(T task) throws Exception;

}

public class SchedulerDemo {

    public static void main(String[] args) throws Exception {

        List<Integer> tasks = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        

        TaskChunkScheduler<Integer, String> scheduler = new TaskChunkScheduler<>(

            4,  // 线程数

            3,  // 每批处理数量

            3,  // 最大重试次数

            1000 // 重试间隔(ms)

        );

        

        List<String> results = scheduler.execute(tasks, task -> {

            // 模拟处理逻辑

            if (task % 3 == 0) {

                throw new RuntimeException("模拟失败");

            }

            return "Processed-" + task;

        });

        

        System.out.println(results);

        scheduler.shutdown();

    }

}

代码说明:该调度器实现任务分片处理、并发执行和自动重试机制。主类TaskChunkScheduler提供线程池管理,支持自定义批次大小和重试策略。TaskProcessor定义任务处理接口,SchedulerDemo展示完整使用示例。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70040412/viewspace-3086268/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2024-08-07

  • 博文量
    589
  • 访问量
    602726