当前位置: 首页 > news >正文

帝国建站模板中国500强企业排名表

帝国建站模板,中国500强企业排名表,网站霸屏怎么做,网站正在建设中 html时隔两年#xff0c;重新读ArrayList源码#xff0c;轻松了很多#xff0c;以问题的方式记录一下收获 装饰器模式 注释中提到ArrayList本身不是线程安全的#xff0c;注释如下#xff1a; * pstrongNote that this implementation is not synchronized.重新读ArrayList源码轻松了很多以问题的方式记录一下收获 装饰器模式 注释中提到ArrayList本身不是线程安全的注释如下 * pstrongNote that this implementation is not synchronized./strong* If multiple threads access an ttArrayList/tt instance concurrently,* and at least one of the threads modifies the list structurally, it* imust/i be synchronized externally. (A structural modification is* any operation that adds or deletes one or more elements, or explicitly* resizes the backing array; merely setting the value of an element is not* a structural modification.) This is typically accomplished by* synchronizing on some object that naturally encapsulates the list.** If no such object exists, the list should be wrapped using the* {link Collections#synchronizedList Collections.synchronizedList}* method. This is best done at creation time, to prevent accidental* unsynchronized access to the list:pre* List list Collections.synchronizedList(new ArrayList(...));/pre如果要想做到线程安全需要对某个对象加锁的方式来实现实现应当如下 synchronized(ojb) {list.add(item); }如果没有这么做可以使用Collections.synchronizedList对ArrayList包装并且最好是在一开始定义list的时候就进行包装避免有的地方使用了未包装的原始list代码如下 List list Collections.synchronizedList(new ArrayList(...));类签名里既然继承了AbstractList为什么还要写implements List public class ArrayListE extends AbstractListEimplements ListE, RandomAccess, Cloneable, java.io.Serializable {应该是作者写错了后来没改回来只是觉得没有必要且保持和旧版本的一致了。参考博客 以及 stackOverFlow问答 DEFAULTCAPACITY_EMPTY_ELEMENTDATA 这是在无参构造函数使用的存储数据默认不分配数组且空数组也复用这内存节省到极致了值得学习。 /*** Shared empty array instance used for default sized empty instances. We* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when* first element is added.*/private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA {};/*** Constructs an empty list with an initial capacity of ten.*/public ArrayList() {this.elementData DEFAULTCAPACITY_EMPTY_ELEMENTDATA;}存储结构Object[] elementData 为什么使用Object这个是java对于泛型的使用上有一些约束。如果直接创建T[]数组会报错因为编译器会进行类型擦除并不能知道这个T类型是什么。所以干脆创建Object[]数组。这个参考自 ArrayList 解析 鉴于泛型擦除list只能做编译期的类型校验运行时是无法校验的除非有类型强转。 public static void main(String[] args) {ListInteger list new ArrayList();list.add(1);List list1 list;list1.add(xx);System.out.println(list);}输出[1, xx]elementData前的transient关键字 意思是序列化时忽略writeObject和readObject单独实现。这两个方法必须声明为private在java.io.ObjectStreamClass#getPrivateMethod()方法中通过反射获取到writeObject()这个方法。 elementData定义为transient的优势自己根据size序列化真实的元素而不是根据数组的长度序列化元素减少了空间占用。ensureExplicitCapacity直接进行了modCount我觉得不妥 源码如下其实下面的if语句为false的时候grow不会执行也就不会对list进行修改所以modCount理论上不应该增加。 结合add和remove方法来看这么写是因为add和remove方法会调用ensureExplicitCapacity所以将modCount的动作下沉了。 但是public方法ensureCapacity也调用了ensureExplicitCapacity而不一定会产生结构修改除非size需要调整。所以这里的语义不太合理了。 /*** 对外提供的方法可以通过调用这个方法在要写入大批数据之前进行容量保障避免出现频繁扩容**/public void ensureCapacity(int minCapacity) {int minExpand (elementData ! DEFAULTCAPACITY_EMPTY_ELEMENTDATA)// any size if not default element table? 0// larger than default for default empty table. Its already// supposed to be at default size.: DEFAULT_CAPACITY;if (minCapacity minExpand) {ensureExplicitCapacity(minCapacity);}}/*** 内部私有实现**/private void ensureExplicitCapacity(int minCapacity) {modCount;// overflow-conscious codeif (minCapacity - elementData.length 0)grow(minCapacity);}MAX_ARRAY_SIZE Integer.MAX_VALUE - 8 最大大小设置的是int最大值-8一堆讨论为什么要减8的以及实际上容量也能设置为int最大值的这个暂时跳过。 想到了另外一个问题size的返回结果是int那超出int怎么办为什么不能设置为long不能设置为long的原因很好理解因为没必要一般不会这么大而且用long就会占用更多内存。那么超出int怎么办没找到方法或许自行设计一个复杂结构扩容是newCapacity oldCapacity (oldCapacity 1)每次扩50%rangeCheckForAdd 针对add和addAll方法会多校验一下index0为什么remove不需要校验呢不太理解代码风格a方法调用了b方法b方法写在a方法后面更容易阅读。fastRemove方法 去掉了index校验只供内部使用真的是太细了。对性能压榨到了极致 /*** 公共方法删除指定index的元素有range校验**/public E remove(int index) {rangeCheck(index);modCount;E oldValue elementData(index);int numMoved size - index - 1;if (numMoved 0)System.arraycopy(elementData, index1, elementData, index,numMoved);elementData[--size] null; // clear to let GC do its workreturn oldValue;}/*** 公共方法删除指定对象在查找到对象之后获取其index通过调用fastRemove进行删除**/public boolean remove(Object o) {if (o null) {for (int index 0; index size; index)if (elementData[index] null) {fastRemove(index);return true;}} else {for (int index 0; index size; index)if (o.equals(elementData[index])) {fastRemove(index);return true;}}return false;}/*** 私有方法删除指定index的元素只供内部使用因此没有做range校验**/private void fastRemove(int index) {modCount;int numMoved size - index - 1;if (numMoved 0)System.arraycopy(elementData, index1, elementData, index,numMoved);elementData[--size] null; // clear to let GC do its work}batchRemove用了读写双指针来实现数据删除过程中的就地挪动 有时候做算法题就会看到一些双指针解决的问题jdk源码里就有相应实现 /*** 删除给定集合的元素它调用了batchRemove方法**/public boolean removeAll(Collection? c) {Objects.requireNonNull(c);return batchRemove(c, false);}/*** 保留给定集合的元素它调用了batchRemove方法**/public boolean retainAll(Collection? c) {Objects.requireNonNull(c);return batchRemove(c, true);}/*** 批量删除方法可以通过complement来控制传入的集合中的原始是需要删除还是保留* r、w双指针实现就地修改**/private boolean batchRemove(Collection? c, boolean complement) {final Object[] elementData this.elementData;int r 0, w 0;boolean modified false;try {for (; r size; r)if (c.contains(elementData[r]) complement)elementData[w] elementData[r];} finally {// Preserve behavioral compatibility with AbstractCollection,// even if c.contains() throws.if (r ! size) {System.arraycopy(elementData, r,elementData, w,size - r);w size - r;}if (w ! size) {// clear to let GC do its workfor (int i w; i size; i)elementData[i] null;modCount size - w;size w;modified true;}}return modified;}modcount是一个体系化的事情是保证遍历的快速失败。需要保证每个影响正确性的地方都修改到那么怎么保证呢根据注释来看是所有会导致list产生结构性变化的地方都需要修改modcount。然后确定方法中是否需要修改modCount就有根据了。
http://www.hkea.cn/news/14450084/

相关文章:

  • 台州平台网站建设福州公司网站建设_
  • 银川微信网站制作wordpress用图床好还是
  • 专门做水果的网站页面升访请广大狼
  • 网站建设中的风险优化设计答案六年级上册语文
  • 建设网站的预期收益app和网站开发语言的区别
  • 广东省石油化工建设集团公司网站网销是做什么的
  • 快递系统查询网站怎么做wordpress duplicator
  • 网站页面关键词都一样免费手机图片编辑器
  • 银川建网站网络营销事件案例
  • 健身器械网站建设案例更新网站 seo
  • 色彩 导航网站做网站需要多少钱 做
  • 大型行业门户网站开发建设方案wordpress右侧的工具栏
  • 戴南做网站公益网站建设 参考文献
  • 做服装外单的网站学会网站建设总结
  • 怎么做多个域名指向一个网站青岛专业制作网站的公司吗
  • 外包做一个网站一般多少钱撰写网站建设技术解决方案
  • 稻香村网站建设网站建设功能需求
  • 信息部网站建设工作计划怎样用jsp做网站登录
  • 内衣网站建设推广用闲置的安卓手机做网站服务器
  • 淄博百度网站深圳网站建设智能 乐云践新
  • 一个网站空间可以做多少个网站单品网站模板
  • 网站的建设与维护步骤佛山营销型网页设计
  • 郑州公司网站2014苏州建设银行招聘网站
  • 哪些网站可以做招商广告酷家乐网站做墙裙教程
  • asp 网站模板如何选择邯郸做网站
  • 做科普网站网站租用服务器多少钱
  • 网站模板后台ui设计培训机构好
  • 搭建网站用什么软件免费空间申请哪个好
  • 自己做一个网站难么加盟投资好项目
  • 冯提莫斗鱼前在哪个网站做直播石家庄正定新区建设局网站