异步编排使用

public class TaskOrchestrator<T> {
    // 任务线程池和TTL上下文
    private final ExecutorService executorService;
    private final TransmittableThreadLocal<List<T>> context; // 使用TransmittableThreadLocal存储泛型数据
    private final Map<String, List<String>> dependencyGraph; // 任务依赖图
    private final Map<String, Boolean> taskStatus; // 任务状态
    private final CountDownLatch latch; // 计数器用于等待所有任务完成
    private final Map<String, List<T>> contextStatus; // 上下文状态

    // 构造函数,初始化线程池和TTL上下文
    public TaskOrchestrator(int threadPoolSize, int totalTasks) {
        this.executorService = TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(threadPoolSize));
        this.context = new TransmittableThreadLocal<>();
        this.dependencyGraph = new ConcurrentHashMap<>();
        this.taskStatus = new ConcurrentHashMap<>();
        this.contextStatus = new ConcurrentHashMap<>();
        this.latch = new CountDownLatch(totalTasks); // 初始化计数器
    }

    public TransmittableThreadLocal<List<T>> getContext() {
        return context;
    }

    // 执行任务图中的任务
    public void executeTasks(Map<String, List<String>> taskGraph, Map<String, Runnable> tasks) {
        buildDependencyGraph(taskGraph);
        initializeTaskStatus(taskGraph);

        List<String> rootTasks = new ArrayList<>();

        // 找出所有的无依赖任务(根任务)
        for (String taskId : taskGraph.keySet()) {
            if (dependencyGraph.get(taskId).isEmpty()) {
                rootTasks.add(taskId);
            }
        }

        // 并行提交无依赖任务
        for (String taskId : rootTasks) {
            submitTask(taskId, taskGraph, tasks);
        }

        // 等待所有任务完成
        try {
            latch.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            // 所有任务完成后关闭线程池
            shutdown();
        }
    }

    // 提交任务的方法,用于实际提交任务到线程池执行
    private void submitTask(String taskId, Map<String, List<String>> taskGraph, Map<String, Runnable> tasks) {
        executorService.submit(() -> {
            try {
                tasks.get(taskId).run();


                List<String> executableTasks = new ArrayList<>();
                // 更新任务状态
                synchronized (taskStatus) {
                    taskStatus.put(taskId, true);
                    for (String dependentTaskId : taskGraph.getOrDefault(taskId, Collections.emptyList())) {
                        if (canExecuteTask(dependentTaskId)) {
                            executableTasks.add(dependentTaskId);
                        } else {
                            // 暂存上下文
                            List<T> newContext = context.get();
                            List<T> existingContext = contextStatus.getOrDefault(dependentTaskId, new ArrayList<>());

                            // 使用Set进行去重
                            Set<T> contextSet = new HashSet<>(existingContext);
                            if (newContext != null) {
                                contextSet.addAll(newContext);
                            }

                            // 将Set转换回List
                            List<T> mergedContext = new ArrayList<>(contextSet);
                            contextStatus.put(dependentTaskId, mergedContext);
                        }
                    }
                }

                System.out.println("任务 " + taskId + " 完成:" + Thread.currentThread().getName());

                // 并行执行所有可以执行的任务
                for (String executableTaskId : executableTasks) {
                    // 合并上下文
                    synchronized (taskStatus) {
                        List<T> mergedContext = context.get() == null ? new ArrayList<>() : new ArrayList<>(context.get());

                        // 使用Set进行去重
                        Set<T> contextSet = new HashSet<>(mergedContext);
                        if (contextStatus.get(executableTaskId) != null) {
                            contextSet.addAll(contextStatus.get(executableTaskId));
                        }
                        // 将Set转换回List
                        mergedContext = new ArrayList<>(contextSet);
                        context.set(mergedContext);
                        submitTask(executableTaskId, taskGraph, tasks);
                    }
                }
            } catch (Exception e) {
                System.err.println("执行任务 " + taskId + " 时出错: " + e.getMessage());
                e.printStackTrace();
            } finally {
                // 每个任务完成后减少计数
                latch.countDown();
            }
        });
    }

    // 检查任务是否可以执行
    private boolean canExecuteTask(String taskId) {
        for (String dependency : dependencyGraph.get(taskId)) {
            if (!taskStatus.getOrDefault(dependency, false)) {
                return false;
            }
        }
        return true;
    }

    // 构建任务依赖图
    private void buildDependencyGraph(Map<String, List<String>> taskGraph) {
        for (String taskId : taskGraph.keySet()) {
            dependencyGraph.putIfAbsent(taskId, new ArrayList<>());
            for (String dependentTaskId : taskGraph.get(taskId)) {
                dependencyGraph.putIfAbsent(dependentTaskId, new ArrayList<>());
                dependencyGraph.get(dependentTaskId).add(taskId);
            }
        }
    }

    // 初始化任务状态
    private void initializeTaskStatus(Map<String, List<String>> taskGraph) {
        for (String taskId : taskGraph.keySet()) {
            taskStatus.put(taskId, false);
        }
    }

    // 关闭线程池
    private void shutdown() {
        executorService.shutdown();
        try {
            if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                executorService.shutdownNow();
                if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                    System.err.println("线程池未终止");
                }
            }
        } catch (InterruptedException e) {
            executorService.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }

    // 主函数用于测试
    public static void main(String[] args) {
        // 定义任务有向图
        Map<String, List<String>> taskGraph = new HashMap<>();
        taskGraph.put("A", Arrays.asList("B"));
        taskGraph.put("X", Arrays.asList("B"));
        taskGraph.put("B", Arrays.asList("C","E"));
        taskGraph.put("C", Arrays.asList("D"));
        taskGraph.put("D", Arrays.asList("F"));
        taskGraph.put("E", Arrays.asList("F"));

        // 创建任务编排器并启动任务
        TaskOrchestrator<Integer> orchestrator = new TaskOrchestrator<>(5, taskGraph.size());
        TransmittableThreadLocal<List<Integer>> threadLocal = orchestrator.getContext();
        Random random = new Random(); // 随机数生成器

        // 定义具体的任务实现
        Map<String, Runnable> tasks = new HashMap<>();
        tasks.put("X", () -> test("X", random, threadLocal));
        tasks.put("A", () -> test("A", random, threadLocal));
        tasks.put("B", () -> test("B", random, threadLocal));
        tasks.put("C", () -> test("C", random, threadLocal));
        tasks.put("D", () -> test("D", random, threadLocal));
        tasks.put("E", () -> test("E", random, threadLocal));
        tasks.put("F", () -> test("F", random, threadLocal));

        orchestrator.executeTasks(taskGraph, tasks);
    }

    private static void test(String name, Random random, TransmittableThreadLocal<List<Integer>> threadLocal) {
        List<Integer> integers = threadLocal.get();
        List<Integer> previousSum = new ArrayList<>();
        if (integers != null) {
            previousSum.addAll(integers);
        }
        int randomValue = random.nextInt(100);
        previousSum.add(randomValue);
        threadLocal.set(previousSum);
        try {
            Thread.sleep(1000);
            System.out.println("-----");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("任务 " + name + " 执行, 随机数: " + " + " + randomValue + " = " + previousSum);
    }
}

Java有多种方式可以实现异步编排,以下是几种常用的方式: 1. CompletableFuture Java 8引入了CompletableFuture类,它是一个可以用于异步编排的Future实现。通过CompletableFuture可以将多个异步任务串联起来,实现异步编排。 示例代码: ```java CompletableFuture.supplyAsync(() -> { // 异步任务1 return 1; }).thenApplyAsync(result1 -> { // 异步任务2,result1为异步任务1的结果 return result1 + 2; }).thenAcceptAsync(result2 -> { // 异步任务3,result2为异步任务2的结果 System.out.println(result2); }); ``` 2. Reactive Streams Reactive Streams是一种异步编程范式,它采用发布-订阅模式实现异步数据流处理。在Java中,Reactive Streams规范由Flow接口和相关类组成,可以用于实现异步编排。 示例代码: ```java Flow.Publisher<Integer> publisher = subscriber -> { subscriber.onNext(1); subscriber.onNext(2); subscriber.onNext(3); subscriber.onComplete(); }; Flowable.fromPublisher(publisher) .map(result1 -> result1 + 2) .subscribe(result2 -> System.out.println(result2)); ``` 3. Spring WebFlux Spring WebFlux是Spring框架的异步编程模块,它提供了基于Reactive Streams的异步编排能力。通过使用WebFlux,可以实现非阻塞的Web应用程序和服务。 示例代码: ```java Mono.just(1) .map(result1 -> result1 + 2) .subscribe(result2 -> System.out.println(result2)); ``` 以上是几种常用的Java异步编排方式,选择适合自己的方式可以提高程序的性能和并发处理能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值