![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
源码看JAVA
eulers
Java全栈工程师。小舟从此逝,江海寄余生。
展开
-
源码看JAVA【四十三】ConcurrentLinkedQueue
ConcurrentLinkedQueue通过使用CAS原子操作保证其相应功能的安全性1、定义原子变量与原子操作,CAS来判断其操作的准确性/** * Throws NullPointerException if argument is null. * * @param v the element */ private static vo...原创 2019-05-09 14:22:47 · 91 阅读 · 0 评论 -
源码看JAVA【三十三】Bits
Bits是位操作的工具类,其中用到了一系列经典的位移操作。1、字节反转,可参考具体的实现。 static short swap(short x) { return Short.reverseBytes(x); } static char swap(char x) { return Character.reverseByte...原创 2019-03-05 23:54:21 · 231 阅读 · 0 评论 -
源码看JAVA【二十六】TreeSet
1、与HashSet类似,也是对TreeMap的封装,底层依然使用TreeMap2、数据存储于默认value值 /** * The backing map. */ private transient NavigableMap<E,Object> m; // Dummy value to associate with an Objec...原创 2019-03-02 18:53:17 · 150 阅读 · 0 评论 -
源码看JAVA【二十五】TreeMap
1、内部类Entry定义树键值对的结构键值对的特征:key-valuetree的特征:left-right-parentcolor:红黑树的设定 private static final boolean RED = false; private static final boolean BLACK = true; /** * Node in...原创 2019-03-02 18:44:17 · 102 阅读 · 0 评论 -
源码看JAVA【二十四】LinkedHashSet
LinkedHashSet具体来说是构造重载了HashSet。使用HashSet在构造方法中添加dummy参数也能实现LinkedHashSet的功能。使用上LinkedHashSet方便了HashSet的构造,并且使得语义更容易理解。 /** * Constructs a new, empty linked hash set with the specified in...原创 2019-03-02 17:55:31 · 114 阅读 · 0 评论 -
源码看JAVA【二十三】LinkedHashMap
publicclass LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>1、继承了HashMap,对HashMap的一个补充,LinkedHashMap添加了元素是有序的存储,具体的实现下面会详细介绍2、内部类Entry继承了HashMap的Node,添加了前一个元素...原创 2019-03-02 17:47:28 · 143 阅读 · 0 评论 -
源码看JAVA【二十二】HashSet
1、成员变量map:底层使用HashMap进行数据保存对于HashMap中的value统一使用空对象:PRESENT 。因此HashSet存储数据实际是一个HashMap,并且HashMap中的每个值都是:PRESENT private transient HashMap<E,Object> map; // Dummy value to associate ...原创 2019-03-02 17:35:10 · 92 阅读 · 0 评论 -
源码看JAVA【二十一】Hashtable
1、内部类Entry,用于保存Hash的值。与HashMap中的Node定义实现相同 /** * Hashtable bucket collision list entry */ private static class Entry<K,V> implements Map.Entry<K,V> { final int ...原创 2019-03-02 17:28:39 · 116 阅读 · 0 评论 -
源码看JAVA【二十】HashMap
说明:HashMap底层的时间存储依然是对象(节点)数组,实现涉及链表、红黑树等数据结构1、常量定义DEFAULT_INITIAL_CAPACITY:默认初始化容量,必须是2的倍数,因此使用位移操作,肯定是2的倍数。初始化值为16MAXIMUM_CAPACITY:最大容量 2^30次方:DEFAULT_LOAD_FACTOR:默认加载因子,在判断是否扩容时,加载因子越小,可能扩容导致的...原创 2019-03-02 17:06:42 · 76 阅读 · 0 评论 -
源码看JAVA【三十四】Buffer
Buffer抽象类定义了缓存池的基本操作,并实现了缓冲池的常用操作。缓冲池的作用减少了必须等待一方写入完成后一方才能读取的问题。可以对缓存区进行边读边写,提高了同步处理的性能。1、成员变量定义mark:标记,用例临时保存position的数据,position操作完成后可重新设置成mark,后续方法会继续说明position,当前操作到的位置limit,缓冲区当前值的大小c...原创 2019-03-06 00:46:47 · 135 阅读 · 0 评论 -
源码看JAVA【二十七】AtomicBoolean
1、定义valueOffet保存了value变量的内存偏移量的值value是AtomicBoolean保存的实际数据。1:true;0:false。因为int默认值为0,所以AtomicBoolean默认值为falsevalue使用了关键字volatile,保证线程间的数据都是最新的。具体的作用可参考:https://mp.csdn.net/postedit/88094147...原创 2019-03-03 22:57:19 · 207 阅读 · 0 评论 -
源码看JAVA【二十八】AtomicInteger
1、定于与构造 // setup to use Unsafe.compareAndSwapInt for updates private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final long valueOffset; static { try { ...原创 2019-03-03 23:07:00 · 91 阅读 · 0 评论 -
源码看JAVA【四十二】Stack
Stack继承Vector,堆栈添加了pop与push操作,并添加synchronized修饰,保证其安全性1、push/pop;插入与取出/** * Pushes an item onto the top of this stack. This has exactly * the same effect as: * <blockquote>&l...原创 2019-05-09 13:52:55 · 234 阅读 · 0 评论 -
源码看JAVA【四十一】Vector
Vector:线程安全List1、变量定义,底层依然使用数组保存数据/** * The array buffer into which the components of the vector are * stored. The capacity of the vector is the length of this array buffer, * and ...原创 2019-05-09 13:50:04 · 108 阅读 · 0 评论 -
源码看JAVA【四十】ArrayDeque
ArrayDeque继承了AbstractCollection的基本集合实现,同时实现了Deque的接口1、变量定义保存在数组对象中,定义了头部位置和尾部位置,最小的数据大小为8 /** * The array in which the elements of the deque are stored. * The capacity of the dequ...原创 2019-05-09 12:18:59 · 117 阅读 · 0 评论 -
源码看JAVA【三十九】Deque
Deque是双向队列,包含Queue的全部功能,同时添加了双向队列的特有实现1、在第一个元素前添加元素:添加失败抛出异常void addFirst(E e);2、在最后的位置添加元素:添加失败抛出异常void addLast(E e);3、调用addFirst方法,添加成功返回true,否则抛出异常boolean offerFirst(E e);4、调用addLast...原创 2019-05-06 17:40:53 · 189 阅读 · 0 评论 -
源码看JAVA【三十八】LinkedBlockingQueue
LinkedBlockingQueue:链表阻塞队列1、定义单向列表static class Node<E> { E item; /** * One of: * - the real successor Node * - this Node, meaning the successor ...原创 2019-04-28 00:42:14 · 137 阅读 · 0 评论 -
源码看JAVA【三十七】BlockingQueue
BlockingQueue接口,重载了Queue中方法的定义,并添加了put/take这组阻塞方法,可用于生产者消费者的实现。put:如果队列已满则阻塞,直到队列可继续添加元素take:如果队列没有元素,则等待,直到有数据时进行返回public interface BlockingQueue<E> extends Queue<E> { /** ...原创 2019-04-28 00:25:55 · 195 阅读 · 0 评论 -
源码看JAVA【三十六】AbstractQueue
AbstractQueue抽象类,实现了Queue接口的部分通用方法。1、add:调用offer方法实现,不存在false的情况,false直接抛出异常 /** * Inserts the specified element into this queue if it is possible to do so * immediately without viol...原创 2019-04-28 00:18:40 · 175 阅读 · 0 评论 -
源码看JAVA【三十五】Queue
add:在队列末尾添加元素,如果添加失败则提示异常(队列没有设置容量则与offer功能一样)offer:在队列末尾添加元素,如果添加失败返回false,添加成功返回true。remove:移除队列第一个元素,如果第一个元素不存在则提示异常。移除成功返回移除的元素poll:移除第一个元素,返回移除的元素,如果元素不存在则返回nullelement:返回一个元素,如果不存在,抛出异常...原创 2019-04-28 00:11:32 · 118 阅读 · 0 评论 -
源码看JAVA【十九】AbstractSet
1、AbstractSet比较简单,无序集合的抽象,继承了AbstractCollection2、equals判断两个集合值是否全部包含,最后判断集合的大小,都一致说明两个集合内容一致。 public boolean equals(Object o) { if (o == this) return true; if (!(o...原创 2019-03-02 15:24:54 · 176 阅读 · 0 评论 -
源码看JAVA【十八】AbstractMap
1、AbstractMap抽象类,实现Map接口,提供了下属Map实现的通用模板,给定了Map实现的框架。后续定义的Map可以很快实现。2、AbstractMap的基础数据操作是基于entrySet的操作。因为实际的数据保存于Node<K,V>[] table;并实际通过entrySet封装的对象进行实际的数据管理。但是也没有具体实现,需要根据实现类来继承。public ...原创 2019-03-02 15:18:45 · 224 阅读 · 0 评论 -
源码看JAVA【十七】Map接口
1、map接口定义了键值对操作的基本常用方法。2、size:key-value的数量 /** * Returns the number of key-value mappings in this map. If the * map contains more than <tt>Integer.MAX_VALUE</tt> element...原创 2019-03-02 14:30:54 · 472 阅读 · 0 评论 -
源码看JAVA【九】Long
1、最大值最小值 /** * A constant holding the minimum value a {@code long} can * have, -2<sup>63</sup>. */ @Native public static final long MIN_VALUE = 0x8000000000000000L...原创 2019-02-27 23:56:35 · 9157 阅读 · 0 评论 -
源码看JAVA【八】Integer
1、最大值最小值 /** * A constant holding the minimum value an {@code int} can * have, -2<sup>31</sup>. */ @Native public static final int MIN_VALUE = 0x80000000; /...原创 2019-02-27 23:44:59 · 9321 阅读 · 0 评论 -
源码看JAVA【七】Float
1、常量定义:SIZE=32;BYTES=4 /** * A constant holding the positive infinity of type * {@code float}. It is equal to the value returned by * {@code Float.intBitsToFloat(0x7f800000)}. ...原创 2019-02-27 23:02:20 · 9261 阅读 · 0 评论 -
源码看JAVA【六】Double
1、最大值 a与b都为0.0时,a为-0则返回b public static double max(double a, double b) { if (a != a) return a; // a is NaN if ((a == 0.0d) && (b == 0.0d) ...原创 2019-02-27 21:08:19 · 9677 阅读 · 0 评论 -
源码看JAVA【五】Byte
1、定义常量,byte取值范围-128~127,位数为8位 /** * A constant holding the minimum value a {@code byte} can * have, -2<sup>7</sup>. */ public static final byte MIN_VALUE = -128;...原创 2019-02-27 20:31:56 · 9568 阅读 · 0 评论 -
源码看JAVA【四】Boolean
1、已定义常量public static final Boolean TRUE = new Boolean(true);public static final Boolean FALSE = new Boolean(false);2、字符串解析Boolean 判断是否为true字符串,不区分大小写 public static boolean parseBoo...原创 2019-02-27 20:14:16 · 9458 阅读 · 0 评论 -
源码看JAVA【三】StringBuffer与StringBuilder
1、AbstractStringBuilder下的append方法 public AbstractStringBuilder append(String str) { if (str == null) return appendNull(); int len = str.length(); ensureCapac...原创 2019-02-27 19:40:19 · 9438 阅读 · 0 评论 -
源码看JAVA【二】String类
1、存储,char字符数组private final char value[];2、初始化与复制 可见创建string是传递对象的引用,char[]拷贝数组的值 public String(String original) { this.value = original.value; this.hash = original.ha...原创 2019-02-27 19:28:08 · 9467 阅读 · 0 评论 -
源码看JAVA【十】Short
1、最大值与最小值 /** * A constant holding the minimum value a {@code short} can * have, -2<sup>15</sup>. */ public static final short MIN_VALUE = -32768; /** ...原创 2019-02-28 00:03:30 · 9335 阅读 · 0 评论 -
源码看JAVA【十一】Thread
1、实现接口Runnable不仅是Thread,实现Runnable的类也可直接通过线程池启动,或者通过Thread的start方法启动public class Thread implements Runnable {}2、注册线程 /* Make sure registerNatives is the first thing <clinit> does. ...原创 2019-02-28 01:33:50 · 9539 阅读 · 0 评论 -
源码看JAVA【二十九】AtomicLong
1、VM_SUPPORTS_LONG_CAS:虚拟机是否支持CAS操作 /** * Records whether the underlying JVM supports lockless * compareAndSwap for longs. While the Unsafe.compareAndSwapLong * method works in ei...原创 2019-03-04 21:43:48 · 220 阅读 · 0 评论 -
源码看JAVA【十六】LinkedList
1、Node节点定义当前元素,前一个元素,后应该元素。 private static class Node<E> { E item; Node<E> next; Node<E> prev; Node(Node<E> prev, E element, Node<E&...原创 2019-03-01 23:20:18 · 88 阅读 · 0 评论 -
源码看JAVA【十五】ArrayList
1、DEFAULT_CAPACITY :默认初始容量当首次扩容时,默认扩容是此常量:10 /** * Default initial capacity. */ private static final int DEFAULT_CAPACITY = 10;2、EMPTY_ELEMENTDATA与DEFAULTCAPACITY_EMPTY_ELEMEN...原创 2019-03-01 22:52:17 · 140 阅读 · 0 评论 -
源码看JAVA【十四】AbstractList
1、indexOf /** * {@inheritDoc} * * <p>This implementation first gets a list iterator (with * {@code listIterator()}). Then, it iterates over the list until the * s...原创 2019-03-01 20:03:43 · 195 阅读 · 0 评论 -
源码看JAVA【十三】AbstractCollection
1、抽象函数定义,子类继承 /** * Returns an iterator over the elements contained in this collection. * * @return an iterator over the elements contained in this collection */ public a...原创 2019-03-01 17:38:04 · 115 阅读 · 0 评论 -
源码看JAVA【十二】Void
1、源码查看/** * The {@code Void} class is an uninstantiable placeholder class to hold a * reference to the {@code Class} object representing the Java keyword * void. * * @author unascribed * @si...原创 2019-03-01 17:37:57 · 194 阅读 · 0 评论 -
源码看JAVA【三十二】Lock
1、public interface LockLock是个接口,提供了锁的相关操作方法抽象。属于行锁,不会锁住整个类,具有较好的性能。与synchronized比较,不会因为一个方法对整个实例或者对象造成影响,具有较高的灵活性与性能。2、lock请求获取当前锁,如果获取不到则进行等待,知道获取该锁。实现中可能由于错误原因,由于获取了锁,但是业务处理失败后未释放锁,导致了死锁。此...原创 2019-03-04 22:51:55 · 137 阅读 · 0 评论