Processing in multi-threaded collection - Problems to add unsuccessful after threading

Yesterday had a colleague (I came in, I was responsible) asked me that the multi-threaded process did not add a collection?

His source code is not up, I will directly solve the solution.

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.tianbo.tcp.TcpBootstrap;
import com.tianbo.tcp.vo.EquVo;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.concurrent.ListenableFutureCallback;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

/**
* @author jianyongchao
* @date: 2021-11-16 09:21:58
* @company: tianbo
* @description:
*/
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = TcpBootstrap.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ThreadPoolTaskExecutorTest {


private static final Integer CORE_SIZE = 20;
private static final Integer MAX_SIZE = 50;
private static final Integer QUEUE_CAPACITY = 2000;
private static final Integer KEEP_ALIVE = 300;



public ThreadPoolTaskExecutor threadPoolTaskExecutor() {

// ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("tb-cloud-pool-%d").build();
// ThreadPoolExecutor.CallerRunsPolicy handler = new ThreadPoolExecutor.CallerRunsPolicy();
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.initialize();
executor.setCorePoolSize(CORE_SIZE);
executor.setMaxPoolSize(MAX_SIZE);
executor.setKeepAliveSeconds(KEEP_ALIVE);
executor.setQueueCapacity(QUEUE_CAPACITY);
// executor.setThreadFactory(threadFactory);
// executor.setRejectedExecutionHandler(handler);

return executor;
}

@Test
public void test() {
ThreadPoolTaskExecutorTest test = new ThreadPoolTaskExecutorTest();
ThreadPoolTaskExecutor executor = test.threadPoolTaskExecutor();

List<EquVo> lists = Collections.synchronizedList(new ArrayList<>());

//CountDownLatchEnable a thread after waiting for another thread to complete their respective work,
// Continue again. Implement with a counter. The counter initial value is the number of threads.
// When each thread completes its own task, the value of the counter will be reduced.
// The value of the counter is0When all the threads have completed some tasks,
// and thenCountDownLatchWaiting for threads can resume the execution of the next task
long start = System.currentTimeMillis();
int threadCount = 10000;
CountDownLatch latch = new CountDownLatch(threadCount);
for(int i=0;i<threadCount;i++){
executor.submitListenable(() -> {
EquVo equVo = new EquVo();

equVo.setEquId(1L);
return equVo;
}).addCallback(new ListenableFutureCallback<EquVo>() {
@Override
public void onSuccess(EquVo equVo) {
// lists.add(equVo);
latch.countDown();
lists.add(equVo);
}

@Override
public void onFailure(Throwable throwable) {
latch.countDown();
}
});
}


try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.err.println(lists.size());
System.out.println(end - start);//200
}
}

Handle 10000, spending time 200 or you can happen
-------------------------------------

package com.tianbo.tcp;

import com.alibaba.fastjson.JSONObject;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

public class Main {
public static void main(String[] args) throws InterruptedException {
//Vector list = new Vector();
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();


List<Object> list = Collections.synchronizedList(new ArrayList<>());

//CountDownLatchEnable a thread after waiting for another thread to complete their respective work,
// Continue again. Implement with a counter. The counter initial value is the number of threads.
// When each thread completes its own task, the value of the counter will be reduced.
// When the counter is0When all threads have completed some tasks,
// Then inCountDownLatchWaiting for threads can resume the execution of the next task
long start = System.currentTimeMillis();
int threadCount = 100;
CountDownLatch latch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
int finalI = i;
new Thread(new Runnable() {
JSONObject json = new JSONObject();

@Override
public void run() {
json.put("i", finalI);
list.add(json);
//Counter minus one
latch.countDown();
}
}).start();
}
//Block the current thread until the value of the counter is0
latch.await();
long end = System.currentTimeMillis();
System.err.println(list.size());
System.out.println(end-start);
}
}

I hope everyone has helpful, writing is not easy.