发布于2021-06-12 16:20 阅读(797) 评论(0) 点赞(10) 收藏(4)
1、构造方法
2、常用方法
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
//先加锁,因为ReentrantLock是独占锁,只有一个线程可以访问
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
//拷贝一个新的数组,此时数据的长度是被拷贝数组长度+1
Object[] newElements = Arrays.copyOf(elements, len + 1);
//赋值
newElements[len] = e;
//将新的数组赋值给array
setArray(newElements);
return true;
} finally {
//释放锁
lock.unlock();
}
}
/**
* Sets the array.
* 将新的数组赋值给array
*/
final void setArray(Object[] a) {
array = a;
}
/**
* {@inheritDoc}
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
return get(getArray(), index);
}
/**
* Gets the array. Non-private so as to also be accessible
* from CopyOnWriteArraySet class.
*/
final Object[] getArray() {
return array;
}
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
//先加锁,因为ReentrantLock是独占锁,只有一个线程可以访问
final ReentrantLock lock = this.lock;
lock.lock();
try {
//获取到数组中的元素
Object[] elements = getArray();
//获取到原来的数组中的元素值
E oldValue = get(elements, index);
if (oldValue != element) {//如果新值和旧值不相等
int len = elements.length;
//把数组中的值复制到新的数组中,
Object[] newElements = Arrays.copyOf(elements, len);
//再赋值
newElements[index] = element;
setArray(newElements);
} else {//否则,新值和旧值一样,直接赋值
// Not quite a no-op; ensures volatile write semantics
setArray(elements);
}
return oldValue;
} finally {
lock.unlock();
}
}
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their
* indices). Returns the element that was removed from the list.
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
//先加锁,因为ReentrantLock是独占锁,只有一个线程可以访问
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
//获取数组中元素的值
E oldValue = get(elements, index);
int numMoved = len - index - 1;
//判断需要移除元素的下标在数组的哪个位置,然后进行相应的移除
if (numMoved == 0)//如果是最后个位置
//拷贝一个新的数组,此时数据的长度是被拷贝数组长度-1,
//并将新的数组赋值给array
setArray(Arrays.copyOf(elements, len - 1));
else {//如果不是最后个位置,先进行拷贝,在赋值
Object[] newElements = new Object[len - 1];
System.arraycopy(elements, 0, newElements, 0, index);
System.arraycopy(elements, index + 1, newElements, index,
numMoved);
setArray(newElements);
}
return oldValue;
} finally {
//释放锁
lock.unlock();
}
}
原文链接:https://blog.csdn.net/li1325169021/article/details/117675193
作者:天天在家
链接:http://www.javaheidong.com/blog/article/222574/d3c0a09717b5dc5925d8/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!