• java并发请求多个接口,顺序返回


    最近有个需求,从一个api拿数据,但是api时间参数又有范围限制,因此需要自己将时间分成多段,多次请求api,并且最终返回的数据需要保持原有的顺序

    代码如下:

    package com.test001.www.threadpool;
    
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Vector;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    public class ThreadPoolTest {
    
        public static void main(String[] args){
            ArrayThreadPool<User> pool = new ArrayThreadPool<>();
            for (int i = 5; i > 0; i--){
                Integer data = i;
                pool.submit(()->{
             //这个地方可以写一个函数,参数可以从外层传入 System.out.println(
    "网络操作开始"+data); Thread.sleep(data*1000); System.out.println("网络操作结束"+data); return new User(data); }); } List<User> users = pool.get(); for (User u :users){ System.out.println(u); } pool.stop(); } } class User{ private int i; public User(int a){ i=a; } @Override public String toString() { return "user="+i; } } class ArrayThreadPool<T>{ private Vector<Future<T>> futureList = new Vector<>(); ExecutorService executor = Executors.newCachedThreadPool(); public ArrayThreadPool submit(Callable<T> task){ Future<T> result = executor.submit(task); futureList.add(result); return this; } public List<T> get(){ List<T> result = new LinkedList<>(); for (Future<T> future : futureList) { try { result.add(future.get()); }catch (Exception e){ result.add(null); } } return result; } public void stop(){ executor.shutdown(); } }

    使用java自带线程池实现

  • 相关阅读:
    NIS server on centOS 7
    fabric-python-基于centos 7
    玩玩Jenkins
    身份认证技术性能对比
    Linux时区设置和时间同步-基于CentOS 6(最小安装)
    openldap+phpadmin的最简安装和配置
    tomcat8.5的网页管理(远程)配置、SSL证书配置-基于Debian 9
    tomcat添加为service服务-基于Debian 9
    安装、配置MySQL5.8基于Debian 9(用apt-get install 默认安装结果是mariadb)
    下载并配置jdk环境-基于debain 9
  • 原文地址:https://www.cnblogs.com/mengxingxinqing/p/10691923.html
Copyright © 2020-2023  润新知