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