`
文章列表

读HashMap源码

//先看构造函数 public HashMap() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) ...
//在该集合上的写操作都是在原有的副本上进行的操作。这样可以在大量需要遍历的场景下提升性能。这也是一种读写分离思想的体现。 //先看构造函数 public CopyOnWriteArrayList() { setArray(new Object[0]); } final void setArray(Object[] a) { array = a; } public CopyOnWriteArrayList(Collection<? extends E> c) { Object[] el ...
//这是一个支持双端操作的可阻塞队列 //先看构造函数 public LinkedBlockingDeque() { this(Integer.MAX_VALUE); } public LinkedBlockingDeque(int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; } public LinkedBlockingDeque(Collectio ...
//先看构造方法 public SynchronousQueue() { this(false); } //公平模式或者非公平模式 public SynchronousQueue(boolean fair) { transferer = fair ? new TransferQueue() : new TransferStack(); } //所有的生产和消费都走的这个方法 Object transfer(Object e, boolean timed, long nanos) { ...
//一个基于而为堆的优先级队列,它是无界的。 //先看构造函数: public PriorityBlockingQueue() { this(DEFAULT_INITIAL_CAPACITY, null); } public PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator) { if (initialCapacity < 1) ...
//一个基于二叉堆优先级的延迟取出队列。 //先看构造函数。 public DelayQueue() {} public DelayQueue(Collection<? extends E> c) { this.addAll(c); } //将collection插入到内部的PriorityQueue中。 public boolean addAll(Collection<? extend ...
//先看构造方法。 public PriorityQueue() { this(DEFAULT_INITIAL_CAPACITY, null); } public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) { if (initialCapacity < 1) throw new IllegalArgumentException(); ...
//LinkedBlockQueue //先看构造函数 public LinkedBlockingQueue() { this(Integer.MAX_VALUE); } public LinkedBlockingQueue(int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; last = head = new Node<E>(null); ...
//先看构造函数 //初始化一个给定容量的ArrayBlockingQueue public ArrayBlockingQueue(int capacity) { this(capacity, false); } //通过给定的容量初始化内部的数组和锁以及条件。 public ArrayBlockingQueue(int capacity, boolean fair) { if (capacity <= 0) throw new IllegalArgumentException(); //初始化一个给定 ...
//ReentrantReadWriteLock的WriteLock //WriteLock的lock方法 public void lock() { sync.acquire(1); } public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); } ...
//ReentrantReadWriteLock的ReadLock //ReadLock的lock方法 public void lock() { sync.acquireShared(1); } public final void acquireShared(int arg) { if (tryAcquireShared(arg) < 0) //入队挂起当前线程 doAcquireShared(arg); } protected final int try ...
//awaitNanos方法是用于在给定毫秒值内沉睡,超出时间或者被唤醒则醒过来。 //和await大部分方法一样,只是在内部多维护了一个毫秒值的时间。 public final long awaitNanos(long nanosTimeout) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); Node node = addConditi ...
public final void await() throws InterruptedException { //线程已经中断抛出异常 if (Thread.interrupted()) throw new InterruptedException(); //加入队列同时清理队列中状态不是Node.CONDITION的线程 Node node = addConditionWaiter(); int savedState = fullyRelease(node); ...
//Reentrant的其他方法: //先看lockInterruptibly方法,该方法主要用于如果该线程未被中断则获取锁 public void lockInterruptibly() throws InterruptedException { sync.acquireInterruptibly(1); } public final void acquireInterruptibly(int arg) throws InterruptedException { //如果当前线程已经被中断抛出异常 ...
//ReetrantLock公平锁源码解析: /**公平锁调用的是FairSync中的方法与非公平锁比少了判断当前状态的步骤 也即非公平锁线程可以在此时直接插队进入执行。 */ final void lock() { acquire(1); } //和非公平锁的区别是调用了了hasQueuedPredecessors方法判断队列是否为空 protected final boolean tryAcquire(int acquires) { final Thread current = Thread ...
Global site tag (gtag.js) - Google Analytics