首页 > 其他分享 >使用多线程优化for循环请求http接口

使用多线程优化for循环请求http接口

时间:2022-10-23 10:35:19浏览次数:51  
标签:map http String list System 接口 test new 多线程

package com.test.list;

import com.alibaba.fastjson.JSON;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

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

public class OneWanListRepeat {

private static ExecutorService executor = new ThreadPoolExecutor(20, 20, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), new ThreadFactoryBuilder().build(), new ThreadPoolExecutor.AbortPolicy());

private static int page = 1;
private static int size = 100;
private static int total = 100000;

public static void main(String[] args) {
// 添加参数
long startTime1 = System.currentTimeMillis();
List<ElementTest> list = new ArrayList<>();
for (int i = 0; i < total; i++) {
ElementTest test = new ElementTest();
test.setName("name_" + i);
test.setId("id_" + i);
list.add(test);

if (i == 999) {
ElementTest test999 = new ElementTest();
test999.setName("name_" + i);
test999.setId("id_" + i);
list.add(test999);
}

if (i == 8888) {
ElementTest test8888 = new ElementTest();
test8888.setName("name_" + i);
test8888.setId("id_" + i);
list.add(test8888);
}
}
long endTime1 = System.currentTimeMillis();
System.out.println("time1=" + (endTime1 - startTime1));

// 使用多线程模拟分页查询http接口,并获取返回值
long startTime3 = System.currentTimeMillis();
int maxPage = total % size == 0 ? total / size : total / size + 1;
CountDownLatch downLatch = new CountDownLatch(maxPage);
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap();
try {
for (int i = 1; i <= maxPage; i++) {
Integer integer = new Integer(i);
System.out.println("This is ==" + i + "==");
Callable thread = () -> {
String uuid = httpRequest();
downLatch.countDown();
return uuid;
};
Future<String> futureUUid = (Future<String>) executor.submit(thread);
map.put(integer, futureUUid.get());
}
downLatch.await();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
downLatch.countDown();
executor.shutdown();
}
long endTime3 = System.currentTimeMillis();
System.out.println("This is main 主线程");
System.out.println("mapSize=" + map.size());
System.out.println("map==========" + map);
System.out.println("time3=" + (endTime3 - startTime3));

long startTime2 = System.currentTimeMillis();
List<ElementTest> elementTests = getRepeat(list);
long endTime2 = System.currentTimeMillis();
System.out.println("time2=" + (endTime2 - startTime2));
System.out.println("result=" + JSON.toJSON(elementTests));
}

// 模拟获取重复元素
private static List<ElementTest> getRepeat(List<ElementTest> list) {
List<ElementTest> result = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();
int size = list.size();
for (int i = 0; i < size; i++) {
ElementTest test = list.get(i);
if (map.get(test.getId()) == null) {
map.put(test.getId(), 1);
} else if (map.get(test.getId()) == 1) {
result.add(test);
map.put(test.getId(), 2);
} else {
map.put(test.getId(), 2);
}
}
return result;
}

public static String httpRequest() {
//模拟http耗时
try {
TimeUnit.SECONDS.toMillis(1000);
} catch (Exception e) {
e.printStackTrace();
}
return UUID.randomUUID().toString().substring(0, 3);
}
}


class ElementTest {
private String name;
private String id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}

标签:map,http,String,list,System,接口,test,new,多线程
From: https://www.cnblogs.com/likeloves/p/16818038.html

相关文章

  • 抽象类(abstract class)和接口(interface)相同点及不同点说明
    转自:http://www.java265.com/JavaMianJing/202110/16353726031598.html下文是笔者收集的抽象类及接口的相同及不同之处,如下所示:抽象类和接口的相同及关联之处1.抽......
  • 使用eolink优雅地进行API接口管理
    为什么使用eolink?我们都知道在一个项目团队中是由很多角色组成的,例如:业务>产品>设计>前端>后端>测试等。每个角色各司其职,一起合作完成项目的生命周期。而前端与后端的沟通......
  • Java多线程(2):线程关键字
    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~ Java中和线程相关的关键字就两:volatile和synchronized。volatile以前用得较少,以后会用得更少(后面解释)。它是一种非常轻......
  • java线程例题-类/对象/实例化/声明/多线程/同步
    packageA_ShangGuiGu.Thread.ThreadTest;importjava.util.concurrent.locks.ReentrantLock;////////////////////////////classzhanghu{//账户类,定义一个余额属性。......
  • 拙见:接口
    后端是怎么连接到数据库的?这问题白痴得就像是:电视机怎么才能通电。然而我就是个白痴。 电视机有个插头,墙上有个插座。插头插上插座,电视机就有电了。 后端连接数据......
  • postman接口测试-时间戳和MD5加密
    在使用postman工具进行接口测试的时候,通过鉴权方式,有的接口字段需要获取当前或者最近时间段内时间戳,有的字段还需要进行MD5加密,这个时候我们就面临怎么获取时间戳和对相关......
  • 郁金香 -多线程创建
    #include<stdio.h>#include<Windows.h>//创建线程函数//开辟线程//BUG解决让这两个线程可以长期存在免得无法观察DWORDWINAPI线程函数1(LPVOIDarg){whil......
  • C# HTTP POST AND GET json or xml
    usingSystem.Net;usingSystem.Net.Cache;usingSystem.IO;stringHttpPost(stringstrUrl,stringstrPostData){s......
  • 计算机组成与设计 硬件软件接口 第五版 RISC-V 电子书 pdf
    作者:[美]JohnL.Hennessy/[美]DavidA.Patterson出版社:机械工业出版社副标题:硬件/软件接口原作名:ComputerOrganizationandDesign:TheHardware/Software......
  • Golang:3.7、Golang接口 interface
    3.7、Golang接口interfacego语言中的接口,是一种新的类型定义,它把所有的具有共性的方法定义在一起,任何其他类型制药实现了这些方法就是实现了这个接口语法//定义接口ty......