import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class ArrayTest {
public static List<Integer> mArrayList = new ArrayList<Integer>();
public static Vector<Integer> mVector = new Vector<Integer>();
public static Count count;
static Count mainCount = new Count();
public ArrayTest() {
mArrayList = new ArrayList<Integer>();
mVector = new Vector<Integer>();
}
public static synchronized void addList() {
CountThread current = (CountThread) Thread.currentThread();
count = current.count;
int i = 0;
long oldTimes = System.nanoTime();
while (i <= 99) {
mArrayList.add(100);
i++;
}
long nowTimes = System.nanoTime();
count.countAdd += nowTimes - oldTimes;
// System.out.println("add "+(nowTimes-oldTimes));
}
public static synchronized void getList() {
CountThread current = (CountThread) Thread.currentThread();
count = current.count;
long oldTimes = System.nanoTime();
Iterator<Integer> iterator = mArrayList.iterator();
while (iterator.hasNext()) {
iterator.next();
}
long nowTimes = System.nanoTime();
count.countGet += nowTimes - oldTimes;
// System.out.println("get "+(nowTimes-oldTimes));
}
public static synchronized void remove() {
CountThread current = (CountThread) Thread.currentThread();
count = current.count;
int i = 0;
long oldTimes = System.nanoTime();
while (i <= 99) {
mArrayList.removeAll(mArrayList);
i++;
}
long nowTimes = System.nanoTime();
count.countRemove += nowTimes - oldTimes;
// System.out.println("remove "+(nowTimes-oldTimes));
}
public static void remove1() {
CountThread current = (CountThread) Thread.currentThread();
count = current.count;
int i = 0;
long oldTimes = System.nanoTime();
while (i <= 99) {
mVector.removeAllElements();
i++;
}
long nowTimes = System.nanoTime();
count.countmRemove += nowTimes - oldTimes;
// System.out.println("mVector remove "+(nowTimes-oldTimes));
}
public static void addList1() {
CountThread current = (CountThread) Thread.currentThread();
count = current.count;
int i = 0;
long oldTimes = System.nanoTime();
while (i <= 99) {
mVector.add(100);
i++;
}
long nowTimes = System.nanoTime();
count.countmAdd += nowTimes - oldTimes;
// System.out.println("mVector add "+(nowTimes-oldTimes));
}
public static void getList1() {
CountThread current = (CountThread) Thread.currentThread();
count = current.count;
long oldTimes = System.nanoTime();
Iterator<Integer> iterator = mVector.iterator();
while (iterator.hasNext()) {
iterator.next();
}
long nowTimes = System.nanoTime();
count.countmGet += nowTimes - oldTimes;
// System.out.println("mVector get "+(nowTimes-oldTimes));
}
public static void main(String[] args) throws InterruptedException {
ArrayList arrayList = new ArrayList();
CountThread t1 = new CountThread();
t1.start();
arrayList.add(t1);
CountThread t2 = new CountThread();
t2.start();
arrayList.add(t2);
CountThread t3 = new CountThread();
t3.start();
arrayList.add(t3);
CountThread t4 = new CountThread();
t4.start();
arrayList.add(t4);
CountThread t5 = new CountThread();
t5.start();
arrayList.add(t5);
CountThread t6 = new CountThread();
t6.start();
arrayList.add(t6);
CountThread t7 = new CountThread();
t7.start();
arrayList.add(t7);
CountThread t8 = new CountThread();
t8.start();
arrayList.add(t8);
CountThread t9 = new CountThread();
t9.start();
arrayList.add(t9);
CountThread t10 = new CountThread();
t10.start();
arrayList.add(t10);
t10.join();
for (int i = 0; i <= 9; i++) {
Count count;
CountThread th;
th = (CountThread) arrayList.get(i);
count = th.getCount();
mainCount.countAdd += count.countAdd;
mainCount.countGet += count.countGet;
mainCount.countRemove += count.countRemove;
mainCount.countmAdd += count.countmAdd;
mainCount.countmGet += count.countmGet;
mainCount.countmRemove += count.countmRemove;
}
System.out.println("array add " + mainCount.countAdd);
System.out.println("array get " + mainCount.countGet);
System.out.println("array remove " + mainCount.countRemove);
System.out.println("mVector add " + mainCount.countmAdd);
System.out.println("mVector get " + mainCount.countmGet);
System.out.println("mVector remove " + mainCount.countmRemove);
}
}
public class Count {
public long countAdd = 0;
public long countGet = 0;
public long countRemove = 0;
public long countmAdd = 0;
public long countmGet = 0;
public long countmRemove = 0;
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class CountThread extends Thread {
public Count count;
public CountThread(){
count = new Count();
}
@Override
public void run() {
ArrayTest.addList();
ArrayTest.getList();
ArrayTest.remove();
ArrayTest.addList1();
ArrayTest.getList1();
ArrayTest.remove1();
}
public Count getCount() {
return count;
}
}
array add 684246
array get 2458674
array remove 1117612
mVector add 221985
mVector get 528815
mVector remove 165668
注意,如果多个线程对同一个List操作,可能会把不同对象放置到同一个位置,所以需要对这个List同步,方法是
synchronized(arraylistA) {
arraylistA.add(new SomeClass());
}
当然还有一个巧妙得方法,就是利用
List listA = Collections.synchronizedList(new ArrayList());
生成一个同步化List,但是注意 使用Iterator遍訪物件時,您仍必須實作同步化,因為這樣的List使用iterator()方法返回的Iterator物件,並沒有保證執行緒安全(Thread-safe):
List list = Collections.synchronizedList(new ArrayList());
synchronized(list) {
Iterator i = list.iterator();
while (i.hasNext()) {......}
}
2.单纯的读方法不需要synchronized关键字