springboot-java 多线程(多个可同时执行的操作,在同一个返回中)

如果是需要频繁调用,不建议使用此方案的多线程,建议使用线程池,可以重复调用

    public Map<String, Object> tow() throws InterruptedException {
    
    
        Map<String, Object> map = new HashMap<>();
        ExecutorService executor = Executors.newFixedThreadPool(2);//做2个线程
        executor.submit(() -> {
    
    
            map.put("a", this.baseMapper.selectList(null));//操作1
        });
        executor.submit(() -> {
    
    
            map.put("b", this.baseMapper.selectList(null));//操作2
        });
        executor.shutdown();
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);//设置等待时间最大(即为不设置)
        return map;
    }

猜你喜欢

转载自blog.csdn.net/weixin_43329956/article/details/120832921