标签:多线程 java linkedlist arraylist thread
上上一篇对linkedList和arrayList的源码对比:http://blog.csdn.net/aaashen/article/details/44925181
上一篇对linkedList和arrayList的各种方法进行单线程的对比:http://blog.csdn.net/aaashen/article/details/45011365
本篇用多线程对比,之对比add方法,插10000000条数据。
其中arrayList add 数据花费11615,linkedlist add数据花费4395。
import java.util.ArrayList; import java.util.LinkedList; /** * Created by aaashen on 2015/4/8. */ public class list_Runnable implements Runnable{ private ArrayList alist = new ArrayList<Integer>(); private LinkedList llist = new LinkedList<Integer>(); public int n = 10000000; //同步 private void addAlist() { while(n>0){ synchronized(alist){ alist.add(n); n--; } // System.out.println(Thread.currentThread().getName()+" of "+Thread.currentThread().getThreadGroup().getName()+"add"+n); } } private void addLlist() { while(n>0){ synchronized(llist){ llist.add(n); n--; } // System.out.println(Thread.currentThread().getName()+" of "+Thread.currentThread().getThreadGroup().getName()+"add"+n); } } @Override public void run() { addAlist(); // addLlist(); } public static void main(String args[]){ long costTime = 0; long startTime = 0; long endTime = 0; //创建一个线程组group,加入3个线程,分别为每一个线程命名 list_Runnable ar= new list_Runnable(); ThreadGroup theGroup=new ThreadGroup("group1"); Thread thread0 = new Thread(theGroup,ar,"thread0"); Thread thread1 = new Thread(theGroup,ar,"thread1"); Thread thread2 = new Thread(theGroup,ar,"thread2"); //开始计时 startTime = System.currentTimeMillis(); thread0.start(); thread1.start(); thread2.start(); while(true){ // System.out.println("theGroup.activeCount number: "+theGroup.activeCount()); if(theGroup.activeCount()==0){ endTime = System.currentTimeMillis();//结束计时 break; } } System.out.println("共耗时"+(endTime-startTime)); } }
多线程对比linkedList和arrayList的add方法
标签:多线程 java linkedlist arraylist thread
原文地址:http://blog.csdn.net/aaashen/article/details/45275973