测评一下使用多线程、线程池和单线程情况下的效率
public class Main{
public static void main(String[] args) throws InterruptedException {
List<Integer> l = new LinkedList<>();
ThreadPoolExecutor tp = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
Random r = new Random();
long startTime = System.currentTimeMillis();
for(int i=0;i<20000;i++){
tp.execute(new Runnable() {
@Override
public void run() {
l.add(r.nextInt());
}
});
}
tp.shutdown();
try{
tp.awaitTermination(1, TimeUnit.DAYS);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("thread pool time:"+(System.currentTimeMillis() - startTime) + " size " + l.size());
l.clear();
startTime = S