java多线程异步处理批量数据

6 篇文章 0 订阅

前段时间需要一个批量请求别人接口的模块,由于别人接口反应速度慢导致批量请求耗费时间太多。于是在网上多发查询找到一个批量异步接口切能返回数据。

多线程配置类


import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@ComponentScan
@EnableAsync
// 线程配置类
public class AsyncTaskConfig implements AsyncConfigurer {

    // ThredPoolTaskExcutor的处理流程
    // 当池子大小小于corePoolSize,就新建线程,并处理请求
    // 当池子大小等于corePoolSize,把请求放入workQueue中,池子里的空闲线程就去workQueue中取任务并处理
    // 当workQueue放不下任务时,就新建线程入池,并处理请求,如果池子大小撑到了maximumPoolSize,就用RejectedExecutionHandler来做拒绝处理
    // 当池子的线程数大于corePoolSize时,多余的线程会等待keepAliveTime长时间,如果无请求可处理就自行销毁

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);// 最小线程数
        taskExecutor.setMaxPoolSize(60);// 最大线程数
        taskExecutor.setQueueCapacity(25);// 等待队列

        taskExecutor.initialize();

        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}

异步操作方法:


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.Future;

@Service
// 线程执行任务类
public class AsyncTaskService {
    private static final Log log = LogFactory.getLog(AsyncTaskService.class);
    Random random = new Random();// 默认构造方法

    @Async
    // 表明是异步方法
    // 无返回值
    public void executeAsyncTask(Integer i) throws InterruptedException {
        System.out.println("input is " + i);
        Thread.sleep(2000 * random.nextInt(i));
        System.out.println("执行异步任务:" + i);
    }

    /**
     * 异常调用返回Future
     *异步多线程操作
     * @param i
     * @return
     * @throws InterruptedException
     */
    @Async
    public Future<String> asyncInvokeReturnFuture(int i) throws InterruptedException {
        System.out.println("input is " + i);
        log.info("shuru========"+i);
      //  Thread.sleep(1000 * random.nextInt(i));
        Thread.sleep(1000);

        Future<String> future = new AsyncResult<String>("success:" + i);// Future接收返回值,这里是String类型,可以指明其他类型

        return future;
    }
}

测试类:

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.task.TaskRejectedException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @program: 
 * @description:
 * @author: Mr.JZQ
 * @create: 2022-12-30 17:26
 **/

public class Application {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //testVoid();
        long s = System.currentTimeMillis();
        testReturn();
        long end = System.currentTimeMillis();
        long l = end - s;
        System.out.println("总共耗时时间:"+l/ 1000+"");
    }

    // 测试无返回结果
    private static void testVoid() throws InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AsyncTaskConfig.class);
        AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);


        long s = System.currentTimeMillis();

        // 创建了20个线程
        for (int i = 1; i <= 20; i++) {
            asyncTaskService.executeAsyncTask(i);
        }
        long end = System.currentTimeMillis();
        long l = end - s;
        System.out.println("jiesushijian:"+l+"");
        context.close();
    }

    // 测试有返回结果
    private static void testReturn() throws InterruptedException, ExecutionException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AsyncTaskConfig.class);
        AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);

        List<Future<String>> lstFuture = new ArrayList<Future<String>>();// 存放所有的线程,用于获取结果

        // 创建100个线程
        for (int i = 1; i <= 60; i++) {
            while (true) {
                try {
                    // 线程池超过最大线程数时,会抛出TaskRejectedException,则等待1s,直到不抛出异常为止
                    Future<String> future = asyncTaskService.asyncInvokeReturnFuture(i);
                    lstFuture.add(future);
                    break;
                } catch (TaskRejectedException e) {
                    System.out.println("线程池满,等待1S。");
                    Thread.sleep(1000);
                }
            }
        }

        // 获取值。get是阻塞式,等待当前线程完成才返回值
        for (Future<String> future : lstFuture) {
            System.out.println(future.get());
        }

    }
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值