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

简介: 当方法间无依赖需提升执行效率时,可采用并行执行。示例通过`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;
    }

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));
    }

serialTest()运行结果

串行耗时:12


parallelTest()运行结果

并行耗时:4

5.总结

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

相关文章
|
人工智能 自然语言处理 IDE
技术赋能新维度,灵码进化新突破:通义灵码2.5新功能尝鲜及深度评测
通义灵码是阿里云推出的基于通义大模型的智能编程助手,作为首款全栈智能辅助的国产编码工具,它为开发者提供“第二大脑”,并重构团队协作效能。2.5版本新增智能体模式,支持Qwen3系列模型,具备自主决策、工程感知和记忆能力,集成3000+MCP工具。其优势包括多模式对话体验、上下文增强、全流程工具链支持及个性化记忆功能,但仍存在上下文管理、权限控制和语言支持等方面的改进空间。此次更新标志着AI辅助开发进入全链路智能化新纪元,成为开发者真正的“结对编程伙伴”。
2082 36
|
传感器 人工智能 安全
蔚来汽车智能座舱接入通义大模型,并使用通义灵码全面提效
为加速AI应用在企业市场落地,4月9日,阿里云在北京召开AI势能大会。阿里云智能集团资深副总裁、公共云事业部总裁刘伟光发表主题演讲,大模型的社会价值正在企业市场释放,阿里云将坚定投入,打造全栈领先的技术,持续开源开放,为AI应用提速。
|
人工智能 自然语言处理 程序员
通义灵码 2.5 版发布上线,支持 Qwen3
示例中展示了通义灵码创建贪食蛇游戏的过程,包括代码优化、Bug修复和功能改进(如游戏结束后提示重新开始)。并通过AI总结了工具的核心能力,如实时续写、自然语言生码、单元测试生成等,帮助开发者高效编码并提升代码质量。
479 10
|
9月前
|
人工智能 自然语言处理 IDE
模型微调不再被代码难住!PAI和Qwen3-Coder加速AI开发新体验
通义千问 AI 编程大模型 Qwen3-Coder 正式开源,阿里云人工智能平台 PAI 支持云上一键部署 Qwen3-Coder 模型,并可在交互式建模环境中使用 Qwen3-Coder 模型。
1406 109
|
9月前
|
分布式计算 测试技术 Spark
科大讯飞开源星火化学大模型、文生音效模型
近期,科大讯飞在魔搭社区(ModelScope)和Gitcode上开源两款模型:讯飞星火化学大模型Spark Chemistry-X1-13B、讯飞文生音频模型AudioFly,助力前沿化学技术研究,以及声音生成技术和应用的探索。
785 2
|
8月前
|
人工智能 搜索推荐 程序员
当AI学会“跨界思考”:多模态模型如何重塑人工智能
当AI学会“跨界思考”:多模态模型如何重塑人工智能
1220 120

热门文章

最新文章