使用通义灵码写了一个多线程工具类,通义灵码处于什么水平

简介: 当方法间无依赖需提升执行效率时,可采用并行执行。示例通过`MultiThreadTaskExecutor`类实现多线程并发,其中`executeParallelDynamicMethods`方法接收一系列`Callable`任务并返回所有任务的结果列表。测试显示,四个耗时方法并行执行仅需4秒,相较于串行执行的12秒显著提升效率。该代码展示了良好的多线程编程实践。

1、场景

有多个的方法,在串行执行的时候效率比较慢,在保证方法不相互依赖的情况下想要提高效率就需要并行执行

2、通义灵码开始工作

根据场景总结相关问题并咨询同义灵码,如:帮我写一个多个方法可并行执行并返回对应数据的工具类

3、通义灵码返回

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

public class MultiThreadTaskExecutor {
   
    private final ExecutorService executor;

    public MultiThreadTaskExecutor(int threadCount) {
   
        this.executor = Executors.newFixedThreadPool(threadCount);
    }

    /**
     * 并行执行多个方法并返回它们的结果。
     *
     * @param tasks 方法列表,每个方法都封装在Callable中
     * @return 一个Future数组,包含所有方法的返回结果
     */
    public <T> List<Future<T>> executeParallel(List<? extends Callable<T>> tasks) throws InterruptedException {
   
        return executor.invokeAll(tasks);
    }

    /**
     * 关闭executor服务
     */
    public void shutdown() {
   
        executor.shutdown();
    }

    /**
     * 执行一组动态方法(表示为Callable),并收集结果。
     * @param callables 可调用对象列表,每个对象代表一个动态方法
     * @param <T> 方法返回值的泛型
     * @return 包含所有方法执行结果的列表
     */
    public  <T> List<T> executeParallelDynamicMethods(List<Callable<T>> callables) {
   
        List<CompletableFuture<T>> futures = new ArrayList<>();
        for (Callable<T> callable : callables) {
   
            CompletableFuture<T> future = CompletableFuture.supplyAsync(() ->
                    {
   
                        try {
   
                            return callable.call();
                        } catch (Exception e) {
   
                            System.out.println("supplyAsync, 运行异常:" + e.getMessage());
                        }
                        return null;
                    }
                    , executor);
            futures.add(future);
        }

        // 等待所有任务完成
        CompletableFuture<Void> allDoneFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));

        try {
   
            allDoneFuture.get(); // 这里会阻塞直到所有任务完成
        } catch (InterruptedException | ExecutionException e) {
   
            System.out.println("executeParallelDynamicMethods, 运行异常:" + e.getMessage());
        }

        // 收集所有结果
        List<T> resultList = futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
        // 关闭线程
        this.shutdown();
        return resultList;
    }
AI 代码解读

4、调用测试

import org.junit.Test;

import java.util.*;
import java.util.concurrent.Callable;

public class methodTest {
   
    public Integer timeConsumingMethod1() {
   
        long startTime= System.currentTimeMillis();
        try {
   
            Thread.sleep(3000);
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
        long endTime= System.currentTimeMillis();
        return (int) ((endTime-startTime)/1000);
    }
    public Integer timeConsumingMethod2() {
   
        long startTime= System.currentTimeMillis();
        try {
   
            Thread.sleep(3000);
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
        long endTime= System.currentTimeMillis();
        return (int) ((endTime-startTime)/1000);
    }
    public Integer timeConsumingMethod3() {
   
        long startTime= System.currentTimeMillis();
        try {
   
            Thread.sleep(2000);
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
        long endTime= System.currentTimeMillis();
        return (int) ((endTime-startTime)/1000);
    }
    public  Integer timeConsumingMethod4() {
   
        long startTime= System.currentTimeMillis();
        try {
   
            Thread.sleep(4000);
        } catch (InterruptedException e) {
   
            e.printStackTrace();
        }
        long endTime= System.currentTimeMillis();
        return (int) ((endTime-startTime)/1000);
    }

    @Test
    public void parallelTest() {
   
        long startTime= System.currentTimeMillis();
        MultiThreadTaskExecutor executor = new MultiThreadTaskExecutor(4);
        List<Callable<Object>> callables = new ArrayList<>();
        callables.add(() -> timeConsumingMethod1());
        callables.add(() -> timeConsumingMethod2());
        callables.add(() -> timeConsumingMethod3());
        callables.add(() -> timeConsumingMethod4());
        List<Object> futures = executor.executeParallelDynamicMethods(callables);
        long endTime= System.currentTimeMillis();
        System.out.println("并行耗时:"+ ((endTime-startTime)/1000));
    }

    @Test
    public void serialTest() {
   
        long startTime= System.currentTimeMillis();
        timeConsumingMethod1();
        timeConsumingMethod2();
        timeConsumingMethod3();
        timeConsumingMethod4();
        long endTime= System.currentTimeMillis();
        System.out.println("串行耗时:"+ ((endTime-startTime)/1000));
    }
AI 代码解读

serialTest()运行结果

串行耗时:12


parallelTest()运行结果

并行耗时:4

5.总结

根据通义灵码提供的多线程方法测试发现,效率明显有很大提升,根据代码水平你们说一下目前的通义灵码处于什么水平呢?

目录
打赏
0
0
0
0
15
分享
相关文章
技术赋能新维度,灵码进化新突破:通义灵码2.5新功能尝鲜及深度评测
通义灵码是阿里云推出的基于通义大模型的智能编程助手,作为首款全栈智能辅助的国产编码工具,它为开发者提供“第二大脑”,并重构团队协作效能。2.5版本新增智能体模式,支持Qwen3系列模型,具备自主决策、工程感知和记忆能力,集成3000+MCP工具。其优势包括多模式对话体验、上下文增强、全流程工具链支持及个性化记忆功能,但仍存在上下文管理、权限控制和语言支持等方面的改进空间。此次更新标志着AI辅助开发进入全链路智能化新纪元,成为开发者真正的“结对编程伙伴”。
915 36
通义灵码正式上线 Qwen3,编程智能体马上来了!
Qwen3正式发布并开源8款「混合推理模型」,包括两款MoE模型(Qwen3-235B-A22B、Qwen3-30B-A3B)和六个Dense模型。旗舰模型Qwen3-235B-A22B在多项测试中表现出色,竞争力强。Qwen3支持两种思考模式(思考与非思考),涵盖119种语言,增强Agent能力,在BFCL评测中创纪录。通义灵码已上线相关插件,助力开发者体验AI编码能力。
665 11
蔚来汽车智能座舱接入通义大模型,并使用通义灵码全面提效
为加速AI应用在企业市场落地,4月9日,阿里云在北京召开AI势能大会。阿里云智能集团资深副总裁、公共云事业部总裁刘伟光发表主题演讲,大模型的社会价值正在企业市场释放,阿里云将坚定投入,打造全栈领先的技术,持续开源开放,为AI应用提速。
通义灵码 2.5 版发布上线,支持 Qwen3
示例中展示了通义灵码创建贪食蛇游戏的过程,包括代码优化、Bug修复和功能改进(如游戏结束后提示重新开始)。并通过AI总结了工具的核心能力,如实时续写、自然语言生码、单元测试生成等,帮助开发者高效编码并提升代码质量。
181 10
通义灵码 AI IDE 上线,第一时间测评体验
通义灵码 AI IDE 重磅上线,开启智能编程新纪元!无需插件,开箱即用,依托通义千问大模型,实现高效、智能的编程体验。支持 MCP 工具链,可快速调用多种服务(如12306余票查询、高德地图标注等),大幅提升开发效率。结合 Qwen3 强大的 Agent 能力,开发者可通过自然语言快速构建功能,如智能选票系统、地图可视化页面等。行间代码预测、AI 规则定制、记忆能力等功能,让 AI 更懂你的编码习惯。Lingma IDE 不仅是工具,更是开发者身边的智能助手,助力 AI 编程落地实践。立即下载体验,感受未来编程的魅力!
260 17
用户说 | 手把手体验通义灵码 2.0:AI 程序员如何让我从“调参侠”进阶“架构师”?
通义灵码 2.0 是强大的 AI 编程工具,助力开发者从“调参侠”进阶为“架构师”。它支持跨语言开发、智能单元测试生成和图生代码等功能,显著提升开发效率。新增 QwQ 模型具备“代码脑补”能力,可推荐性能优化策略。尽管功能强大,但仍需注意环境隔离与代码审查,避免过度依赖。通义灵码 2.0 不仅是工具,更是开发者的“外接大脑”,帮助应对全栈开发挑战。
113 0
通义灵码 AI IDE 上线!智能体+MCP 从手动调用工具过渡到“AI 主动调度资源”
编程智能体与 MCP 的结合,不只是“工具+助手”,而是一次范式上的跃迁——从“手动调用工具”过渡到“AI 主动调度资源”。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等