`
aijuans
  • 浏览: 1547017 次
社区版块
存档分类
最新评论

ArrayList VS Vector(ArrayList和Vector的区别)_面试的时候经常出现

 
阅读更多
数据增长:当需要增长时,Vector默认增长为原来一培,而ArrayList却是原来的一半
同步性:Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步的 

这个我们可以看看他们的源码就可以知道:

ArrayList的size()方法:

1 /**
2      * Returns the number of elements in this list.
3      *
4      * @return the number of elements in this list
5      */
6     public int size() {
7         return size;
8     }

Vector的size()方法:

1 /**
2      * Returns the number of components in this vector.
3      *
4      * @return  the number of components in this vector
5      */
6     public synchronized int size() {
7         return elementCount;
8     }

其实Vector的其他很多方法都是同步的:

 1 /**
 2      * Sets the size of this vector. If the new size is greater than the
 3      * current size, new {@code null} items are added to the end of
 4      * the vector. If the new size is less than the current size, all
 5      * components at index {@code newSize} and greater are discarded.
 6      *
 7      * @param  newSize   the new size of this vector
 8      * @throws ArrayIndexOutOfBoundsException if the new size is negative
 9      */
10     public synchronized void setSize(int newSize) {
11         modCount++;
12         if (newSize > elementCount) {
13             ensureCapacityHelper(newSize);
14         } else {
15             for (int i = newSize ; i < elementCount ; i++) {
16                 elementData[i] = null;
17             }
18         }
19         elementCount = newSize;
20     }
21 
22     /**
23      * Returns the current capacity of this vector.
24      *
25      * @return  the current capacity (the length of its internal
26      *          data array, kept in the field {@code elementData}
27      *          of this vector)
28      */
29     public synchronized int capacity() {
30         return elementData.length;
31     }
32 
33     /**
34      * Returns the number of components in this vector.
35      *
36      * @return  the number of components in this vector
37      */
38     public synchronized int size() {
39         return elementCount;
40     }
41 
42     /**
43      * Tests if this vector has no components.
44      *
45      * @return  {@code true} if and only if this vector has
46      *          no components, that is, its size is zero;
47      *          {@code false} otherwise.
48      */
49     public synchronized boolean isEmpty() {
50         return elementCount == 0;
51     }

那现在我们应该设计一个方案来研究我们的这些结论的正确性!!

下面给出ArrayList线程不安全证明方案:

 1 /**
 2  * 
 3  */
 4 package com.b510;
 5 
 6 import java.util.ArrayList;
 7 import java.util.Collections;
 8 import java.util.List;
 9 
10 /**
11  * @date 2013-5-2
12  * @author xhw
13  * 
14  */
15 public class HongtenArrayList implements Runnable {
16     //线程不安全
17      List<String> list = new ArrayList<String>(1);
18     //线程安全
19     //List<String> list = Collections.synchronizedList(new ArrayList<String>());
20     public void run() {
21         try {
22             Thread.sleep((int) (Math.random() * 2));
23         } catch (InterruptedException e) {
24             e.printStackTrace();
25         }
26         System.out.println(Thread.currentThread().getName());
27         list.add(Thread.currentThread().getName());
28     }
29 
30     public static void main(String[] args) throws InterruptedException {
31         ThreadGroup group = new ThreadGroup("hongtenGroup");
32         HongtenArrayList t = new HongtenArrayList();
33         for (int i = 0; i < 10000; i++) {
34             Thread th = new Thread(group, t, String.valueOf(i));
35             th.start();
36         }
37         while (group.activeCount() > 0) {
38             Thread.sleep(10);
39         }
40         System.out.println("result=============");
41         //如果线程安全,那么结果是10000,如果线程不安全,则结果不可预测
42         System.out.println(t.list.size()); 
43     }
44 
45 }

下面贴出部分运行结果:

线程不安全:

当然线程安全下面会出现:(运行结果都是10000)

9982
9988
9990
9979
9981
9977
9965
9971
9848
9846
9989
9993
9991
9678
9995
9992
9998
9994
9996
9997
9999
result=============
10000

对于增长,我们可以看看ArrayList的源码:

ArrayList:grow()

 1 /**
 2      * Increases the capacity to ensure that it can hold at least the
 3      * number of elements specified by the minimum capacity argument.
 4      *
 5      * @param minCapacity the desired minimum capacity
 6      */
 7     private void grow(int minCapacity) {
 8         // overflow-conscious code
 9         int oldCapacity = elementData.length;
10         int newCapacity = oldCapacity + (oldCapacity >> 1);
11         if (newCapacity - minCapacity < 0)
12             newCapacity = minCapacity;
13         if (newCapacity - MAX_ARRAY_SIZE > 0)
14             newCapacity = hugeCapacity(minCapacity);
15         // minCapacity is usually close to size, so this is a win:
16         elementData = Arrays.copyOf(elementData, newCapacity);
17     }

Vector:grow()

 1 private void grow(int minCapacity) {
 2         // overflow-conscious code
 3         int oldCapacity = elementData.length;
 4         int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
 5                                          capacityIncrement : oldCapacity);
 6         if (newCapacity - minCapacity < 0)
 7             newCapacity = minCapacity;
 8         if (newCapacity - MAX_ARRAY_SIZE > 0)
 9             newCapacity = hugeCapacity(minCapacity);
10         elementData = Arrays.copyOf(elementData, newCapacity);
11     }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics