import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* @author :yanjun.hou
* @description :
* @create :2022-04-17 20:23:00
*/
public class ListTest {
//普通list---会产生多线程操作的时候大小不是设想的 可能会减少
static List<String> nomalList = new ArrayList<>();
//使用集合工具类 转换list
static List<String> colList = Collections.synchronizedList(new ArrayList<>());
//可读写的list 推荐使用
static List<String> copyOnWriteList = new CopyOnWriteArrayList<>();
/**
* 测试高并发下 List使用
*/
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new TestList());
Thread t2 = new Thread(new TestList());
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("【nomalList】:"+nomalList.size());
System.out.println("【colList】:"+colList.size());
System.out.println("【copyOnWriteList】:"+copyOnWriteList.size());
}
public static class TestList implements Runnable {
@Override
public void run() {
for (int i = 0; i < 3000; i++) {
nomalList.add(UUID.randomUUID().toString().substring(0, 5));
colList.add(UUID.randomUUID().toString().substring(0, 5));
copyOnWriteList.add(UUID.randomUUID().toString().substring(0, 5));
}
}
}
}