第一次写博客 ,,略略有点小灰机哈 。。。。。。
因为不知道江湖规矩,我还是先来点鸡汤做铺垫把,像我这种 非计算机专业的(自学菜鸟)也只能干点菜鸟的活 今天确实也是萌发奇想,想总结一下看过的东西,因为以前都没怎么总结然后自己想看找起来也麻烦,所以还是自己总结这样 也找起来方便嘛 ,, 哈哈 废话不多说先过一下我今天要说的索引。
**1 集合范谈之ArrayList(这个比较特别–当然还有更特别的还没来得及 看,下次在补上—-嘻嘻) ,
2 我对gc 的理解(我看ArrayList时候看到一个付null来引用gc 后想起来的),
3 线程并发浅谈,**首先我来和大神们扯一下集合的概念 :现阶段有很多分发我这里采用比较传统的分发,也就是Collection And Map 这两个啦 ,———–都是接口(在java.util包下) ,在这里我把集合分为这两类 1 Collection : 他的子接口大概这么多(根据jdk1.6)BeanContext, BeanContextServices, BlockingDeque,Deque, List, NavigableSet, , Set, SortedSet
BlockingDeque 是java 1.6 才出来的用于并发编程的阻塞队列,他可以以阻塞的方式解决请求的并发处理也就是线程的并发访问,他的take ()方法可以以阻塞的方式从共享数据区域拿资源(在线程的世界叫临界区—-并发线程的一种模式而已,除此之外还有writeAndRead,哲学家用餐等主要的并发模型)如果队列空那么久阻塞等带资源的出现,反之他的input()方法可以以阻塞的方式向共享区域插入数据,如果队列已满那么就阻塞等待队列腾出时间哈,,,,,,
Deque 队列(FIFO容器)即 先进先出 这个jvm的值栈结构(FILO)相反,相信大家应该能拼出来我这里就不扯英文缩写了哈,,,,,,嘻嘻 , 另外我记得老师说过队列是一个无处不在的美妙概念,它提供了一种简单又可靠的方式将资源分发给处理单元(也可以说是将工作单元分配给待处理的资源,这取决于你看待问题的方式)。总之是个好东西,而实现中的并发编程模型很多都依赖队列来实现,因为它可以在线程之间传递工作单元。
java 7以后有个替换版本trasferQueue —- (–==。。。。) 这个是以非阻塞的方式来完成并发操作,原理和刚刚那个差不多只是在比如同样的方法take()那么只要有可以处理数据的线程在等待就不会阻塞直到之前处理的那个线程出现 。 然后应该到了本章的重点了 list 这是个子接口 ,所以不能实例化 ,但可以实例化其实现类 例如我今天要扯的ArrayList(); 废话不多说先来点干货: List:有顺序,准确应该说有储存顺序~~,并且可重复和Set相反但这不是重点哈! ArrayList: 底层是数组结构,查找速度快,增删速度慢,(它的底层是数组结构,它为什么查找速度快,为什么增删速度慢, 一会咱就来一个底层大揭密哈 ,,, LinkedList:底层是链表结构,查找速度慢,增删速度快 这个有空在分析,,
Vector: 底层结构也是数组,只不过Java在刚推出vector(等等还有很多类),的时候,考虑了线程安全问题,所以就降低了性能,现在不用考虑线程安全问题(说白了就是线程安全的和同期出的HashTable相似java1.2 出的) 先贴点源码分析一下
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
//也就是说这个是一个数组缓存区,白化点就是代表一坨翔 (但是这一坨翔是大小是可变的--嘿嘿!)
//因为归根还是容器之身
private transient Object[] elementData;
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
//迎合上面的这个应该就是翔的尺寸了 ---哈哈哈
private int size;
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
//就是把指定元素贴在集合的后面
---这里我想说个题外话集合虽然这里说明存的是Element但是实际存的是索引指向
相应的元素罢了
//总结一下就是为什么ArrayList增删慢,每当数组空间不足的时候,
//继续添加对象,ArrayList会创建一个新的数组,再将原数组中的数组再拷贝新的数组
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true; 、、添加成功返回
}
/**
* Increases the capacity of this <tt>ArrayList</tt> instance, if
* necessary, to ensure that it can hold at least the number of elements
* specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
public void ensureCapacity(int minCapacity) {
modCount++; //至于这个说明的是迭代次数的标识,每执行一次方法此
数+1
int oldCapacity = elementData.length;//得到当前集合的长度
if (minCapacity > oldCapacity) { //求大于供 (----要爆了)
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1; //增加供的量----固定算法
if (newCapacity < minCapacity)//判断如果还是求大于供
newCapacity = minCapacity; //把球负给供
// minCapacity is usually close to size, so this is a win:
//得到需求的最后的容积newCapacity
//此方法在下面
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
@param original the array to be copied //要copy的数组
@param newLength the length of the copy to be returned //copy后副本返回的长度
@return a copy of the original array, truncated or padded with nulls
to obtain the specified length
//返回的是COPY后的数组其大小可变
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
/*
* @param original the array to be copied
* @param newLength the length of the copy to be returned
* @param newType the class of the copy to be returned
* @return a copy of the original array, truncated or padded with nulls
* to obtain the specified length
*/
//返回的是副本的类型
@param newType the class of the copy to be returned
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
/*
下面这个方法源码我就不贴了,再贴可能就有点多了我大概讲一下
//原数组
@param src the source array.
// 从原数组的那个地方开始nopy 起
@param srcPos starting position in the source array.
//目标数组
@param dest the destination array.
//从目标的那个地方开始贴起
@param destPos starting position in the destination data.
//完成后数组的元素集长度
@param length the number of array elements to be copied.
@exception IndexOutOfBoundsException if copying would cause
access of data outside array bounds.
*/
有兴趣的同学可以自己去研究一哈 -00- 哈
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy; //返回的就是copy完的对象
}
/**
* Removes the element at the specified position in this list.
移除在指定位置的元素
* Shifts any subsequent elements to the left (subtracts one from their
* indices).//移动随后的元素(即删除每个元素之后的所有元素)向左移动---指标减一
这个有点难理解咱们往下看
*
* @param index the index of the element to be removed
* @return the element that was removed from the list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
//咱先判断其参数是否大于集合最大值,不符合抛IndexOutOfBoundsException
//源码比较简单我就不贴了
RangeCheck(index);
modCount++; //迭代指标曾一
E oldValue = (E) elementData[index];//获得要删除的元素用于最后返回
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work ---
//上面几行差不多是固定算法
//核心是把原来index位置的元素给覆盖了
//我画个图可能就好理解了
return oldValue;
}
先写到这里吧 !!没时间了 下次补上续集 !!