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

原文地址:http://www.cnblogs.com/likeloves/p/16818038.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性