浅谈Synchronized和Vector
在学习集合源码时,我们不可避免的都会遇到synchronized
关键字,正是有了这个关键字才让我们的Vector实现线程安全,而且Vector大部分的源码也和ArrayList的源码比较相似。
Vector 类实现了一个动态数组。和 ArrayList 很相似,但是两者是不同的:
- Vector 是同步访问的。
- Vector 包含了许多传统的方法,这些方法不属于集合框架。
Vector 主要用在事先不知道数组的大小,或者只是需要一个可以改变大小的数组的情况。
首先我们来说一下synchronized的作用:
1.原子性:那么所谓的原子性是指一个或多个操作要么全部执行,要么全部不被执行。而被synchronized
修饰的类或对象都是原子的,因为操作之前都要先获取类或对象的锁,直到执行完毕才能被释放。
2.可见性:可见性是指多个线程访问一个资源时,该资源的状态、值等信息对于其他的线程都是可见的
3.有序性:有序性值程序的执行顺序按照代码先后执行, synchronized和volatile都具有有序性,Java允许编译器和处理器对指令进行重排,但是指令重排并不会影响单线程的顺序,它影响的是多线程并发执行的顺序性。
Vector添加元素:
/**
* 将指定的元素附加到Vector的末尾。
*
* @param e 元素要附加到这个Vector
* @return {@code true} (as specified by {@link Collection#add})
* @since 1.2
*/
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
/**
* Inserts the specified object as a component in this vector at the
* specified {@code index}. Each component in this vector with
* an index greater or equal to the specified {@code index} is
* shifted upward to have an index one greater than the value it had
* previously.
*
* <p>The index must be a value greater than or equal to {@code 0}
* and less than or equal to the current size of the vector. (If the
* index is equal to the current size of the vector, the new element
* is appended to the Vector.)
*
* <p>This method is identical in functionality to the
* {@link #add(int, Object) add(int, E)}
* method (which is part of the {@link List} interface). Note that the
* {@code add} method reverses the order of the parameters, to more closely
* match array usage.
*
* @param obj the component to insert
* @param index where to insert the new component
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index > size()})
将指定的对象作为组件插入到此向量中指定的{@code索引}处。该向量中索引大于或等于指定的{@code index}的每个组件的索引都向上移动,使其索引值比之前的值大1。
*/
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}
删除元素:
/**如果Vector中不包含该元素,则保持不变。更正式地说,删**除索引i最低的元素,使(o==null ?Get (i)==null: **o.equals(Get (i)))(如果存在这样的元素)。如果Vector中**包含指定的元素,则返回:true
**/
public boolean remove(Object o) {
return removeElement(o);
}
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}
获取元素:
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
插入元素:
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}