/** * Priority queue represented as a balanced binary heap: the two * children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The * priority queue is ordered by comparator, or by the elements' * natural ordering, if comparator is null: For each node n in the * heap and each descendant d of n, n <= d. The element with the * lowest value is in queue[0], assuming the queue is nonempty. * 优先级队列代表着一个平衡的二叉堆:队列[n]的两个孩子分别是队列[2*n+1]和队列[2*(n+1)]。 *先级队列的元素根据构造队列时提供的 Comparator 进行排序,或者按照其自然顺序进行排序: *如果比较器为空,则堆中的每一个结点n和它的的子节点d,有n<=d。 *如果队列不为空的话,则最小值元素是队列[0]。 */ private transient Object[] queue;
/** * The number of elements in the priority queue. * 优先队列中的元素个数 */ private int size = 0;
/** * The comparator, or null if priority queue uses elements' * natural ordering. * 优先队列的比较器,如果优先队列按自然排序,则为空 */ private final Comparator super E> comparator;
/** * The number of times this priority queue has been * structurally modified. See AbstractList for gory details. * 该优先队列结构改变的次数,要获取更多细节,请看AbstractList。 */ private transient int modCount = 0;
/** * Creates a {@code PriorityQueue} with the default initial * capacity (11) that orders its elements according to their * {@linkplain Comparable natural ordering}. * 使用默认的初始容量(11)创建一个 PriorityQueue,并根据其自然顺序对元素进行排序。 */ public PriorityQueue() { this(DEFAULT_INITIAL_CAPACITY, null); }
/** * Creates a {@code PriorityQueue} with the specified initial * capacity that orders its elements according to their * {@linkplain Comparable natural ordering}. ** 使用指定的初始容量创建一个 优先队列,并根据其自然顺序对元素进行排序 * @param initialCapacity the initial capacity for this priority queue * 初始化的容量 * @throws IllegalArgumentException if {@code initialCapacity} is less * than 1 * 如果容量小于1,则跑出异常IllegalArgumentException */ public PriorityQueue(int initialCapacity) { this(initialCapacity, null); }
/** * Creates a {@code PriorityQueue} with the specified initial capacity * that orders its elements according to the specified comparator. *使用指定的初始容量创建一个 PriorityQueue,并根据指定的比较器对元素进行排序。 * @param initialCapacity the initial capacity for this priority queue * 初始化的容量 * @param comparator the comparator that will be used to order this * priority queue. If {@code null}, the {@linkplain Comparable * natural ordering} of the elements will be used. * 用于对此优先级队列进行排序的比较器。如果该参数为 null,则将使用元素的自然顺序
* @throws IllegalArgumentException if {@code initialCapacity} is * less than 1 * 如果容量小于1,则抛出异常IllegalArgumentException */ public PriorityQueue(int initialCapacity, Comparator super E> comparator) { // Note: This restriction of at least one is not actually needed, // but continues for 1.5 compatibility if (initialCapacity < 1) throw new IllegalArgumentException(); this.queue = new Object[initialCapacity]; this.comparator = comparator; }
/** * Creates a {@code PriorityQueue} containing the elements in the * specified collection. If the specified collection is an instance of * a {@link SortedSet} or is another {@code PriorityQueue}, this * priority queue will be ordered according to the same ordering. * Otherwise, this priority queue will be ordered according to the * {@linkplain Comparable natural ordering} of its elements. *创建包含指定 collection 中元素的 PriorityQueue。如果指定的 collection 是 SortedSet 的一个实例 *或者是另一个 PriorityQueue,那么此优先级队列将根据相同顺序进行排序。否则,此优先级队列 *将根据元素的自然顺序进行排序。 * @param c the collection whose elements are to be placed * into this priority queue * 其元素要置于此优先级队列中
* @throws ClassCastException if elements of the specified collection * cannot be compared to one another according to the priority * queue's ordering * ClassCastException -如果根据 c 的顺序无法比较 c 中的各个元素 * @throws NullPointerException if the specified collection or any * of its elements are null * NullPointerException-如果指定 collection 或任何元素为 null
*/ public PriorityQueue(Collection extends E> c) { initFromCollection(c); if (c instanceof SortedSet) comparator = (Comparator super E>) ((SortedSet extends E>)c).comparator(); else if (c instanceof PriorityQueue) comparator = (Comparator super E>) ((PriorityQueue extends E>)c).comparator(); else { comparator = null; heapify(); } }
/** * Creates a {@code PriorityQueue} containing the elements in the * specified priority queue. This priority queue will be * ordered according to the same ordering as the given priority * queue. *创建包含指定优先级队列元素的 PriorityQueue。此优先级队列将根据与给定优先级队列 *相同的顺序进行排序。 * into this priority queue * 有序 set,其元素将置于此优先级队列中
* @throws ClassCastException if elements of {@code c} cannot be * compared to one another according to {@code c}'s * ordering * ClassCastException -如果根据 c 的顺序无法比较 c 中的各个元素
* @throws NullPointerException if the specified priority queue or any * of its elements are null * NullPointerException-如果指定优先级队列或任何元素为 null
*/ public PriorityQueue(PriorityQueue extends E> c) { comparator = (Comparator super E>)c.comparator(); initFromCollection(c); }
/** * Creates a {@code PriorityQueue} containing the elements in the * specified sorted set. This priority queue will be ordered * according to the same ordering as the given sorted set. *创建包含指定有序 set 元素的 PriorityQueue。此优先级队列将根据与给定有序 set 相同的顺序进行排序。
参数:
* @param c the sorted set whose elements are to be placed * into this priority queue * 有序 set,其元素将置于此优先级队列中
* @throws ClassCastException if elements of the specified sorted * set cannot be compared to one another according to the * sorted set's ordering * ClassCastException - 如果根据有序 set 的顺序无法比较该有序 set 中的各个元素 * @throws NullPointerException if the specified sorted set or any * of its elements are null * NullPointerException - 如果指定有序 set 或任何元素为 null
*/ public PriorityQueue(SortedSet extends E> c) { comparator = (Comparator super E>)c.comparator(); initFromCollection(c); }
2.主要方法的实现(举几个例子,仅供分析。就不一一分析了。)
/** *将指定的元素插入此优先级队列。 / public boolean offer(E e) { //如果加入的元素为空,则抛出异常 if (e == null) throw new NullPointerException(); modCount++; int i = size; //如果要插入的坐标大于队列长度,队列长度加1 if (i >= queue.length) grow(i + 1); size = i + 1; //插入元素 if (i == 0) queue[0] = e; else siftUp(i, e); return true; } //获取并移除此队列的头,如果此队列为空,则返回 null。 public E poll() { if (size == 0) return null; int s = --size; modCount++; E result = (E) queue[0]; E x = (E) queue[s]; queue[s] = null; if (s != 0) siftDown(0, x); return result; } // 从此队列中移除指定元素的单个实例(如果存在)。 private E removeAt(int i) { assert i >= 0 && i < size; modCount++; int s = --size; if (s == i) // removed last element(移除最后一个元素) queue[i] = null; else { E moved = (E) queue[s]; queue[s] = null; siftDown(i, moved); if (queue[i] == moved) { siftUp(i, moved); if (queue[i] != moved) return moved; } } return null; }……
我的个娘啊,真是翻译不完了,暂告一阶段……=
[size=medium]参考资料:
1.JDK API
2.[url]http://blog.csdn.net/a352193394/article/details/7210435[/url]
3.[url]http://www.cxyclub.cn/n/12556/[/url][/size]
首先看看 WeakReference
wiki 上 Weak reference 的一个例子:
public class ReferenceTest {
public static void main(String[] args) throws InterruptedException {
WeakReference r = new Wea
有一个线上环境使用的是c3p0数据库,为外部提供接口服务。最近访问压力增大后台tomcat的日志里面频繁出现
com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResou
https://weblogs.java.net/blog/mriem/archive/2013/11/22/jsf-tip-45-create-composite-component-custom-namespace
When you developed a composite component the namespace you would be seeing would
一、复本集为什么要加入Arbiter这个角色 回答这个问题,要从复本集的存活条件和Aribter服务器的特性两方面来说。 什么是Artiber? An arbiter does
not have a copy of data set and
cannot become a primary. Replica sets may have arbiters to add a
# include <stdio.h>
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
//a 是数组的名字 5是表示数组元素的个数,并且这五个元素分别用a[0], a[1]...a[4]
int i;
for (i=0; i<5; ++i)
printf("%d\n",