Skip to content

Instantly share code, notes, and snippets.

@ThomasLau
ThomasLau / gist:5c5c80094ba63a3dce23
Last active August 29, 2015 14:17
an interesting quote in JDK(8).HashTable
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
@ThomasLau
ThomasLau / gist:fbee7468e57d9952fe4a
Last active August 29, 2015 14:17
HashMap中的一段方法
static Class<?> comparableClassFor(Object x) {
if (x instanceof Comparable) {
Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
if ((c = x.getClass()) == String.class) // bypass checks
return c;
if ((ts = c.getGenericInterfaces()) != null) {
for (int i = 0; i < ts.length; ++i) {
if (((t = ts[i]) instanceof ParameterizedType) &&
((p = (ParameterizedType)t).getRawType() ==
Comparable.class) &&
@ThomasLau
ThomasLau / gist:42ad9baea9d3cb463459
Created March 20, 2015 00:56
introduction to Hashmap's resize
/**
* Initializes or doubles table size. If null, allocates in
* accord with initial capacity target held in field threshold.
* Otherwise, because we are using power-of-two expansion, the
* elements from each bin must either stay at same index, or move
* with a power of two offset in the new table.
*
* @return the table
*/
final Node<K,V>[] resize() {
@ThomasLau
ThomasLau / gist:cf553fb004a004ea29b4
Created March 20, 2015 01:14
how HashMap returns the proper-size,which is a power of two size,according to the initial input size
/**
* Returns a power of two size for the given target capacity.
*/
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
@ThomasLau
ThomasLau / gist:b2b9102a7dd7df1c8705
Created March 20, 2015 01:36
pieces on java.util.IdentityHashMap
java.util.IdentityHashMap类利用哈希表实现 Map 接口,比较键(和值)时使用引用相等性代替对象相等性。
换句话说,在 IdentityHashMap 中,当且仅当 (k1==k2) 时,才认为两个键 k1 和 k2 相等(在正常 Map 实现(如 HashMap)中,
当且仅当满足下列条件时才认为两个键 k1 和 k2 相等:(k1==null ? k2==null : e1.equals(e2)))。
此类不是 通用 Map 实现!此类实现 Map 接口时,它有意违反 Map 的常规协定,
该协定在比较对象时强制使用 equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。 此类的典型用法是拓扑保留对象图形转换,如序列化或深层复制。
要执行这样的转换,程序必须维护用于跟踪所有已处理对象引用的“节点表”。
节点表一定不等于不同对象,即使它们偶然相等也如此。此类的另一种典型用法是维护代理对象。
例如,调试设施可能希望为正在调试程序中的每个对象维护代理对象。
此类提供所有的可选映射操作,并且允许 null 值和 null 键。此类对映射的顺序不提供任何保证;特别是不保证顺序随时间的推移保持不变。
此类提供基本操作(get 和 put)的稳定性能,假定系统标识了将桶间元素正确分开的哈希函数 (System.identityHashCode(Object))。
@ThomasLau
ThomasLau / gist:27c9880b5af7e5989ded
Created March 20, 2015 01:54
new function method HashTable.forEach
@SuppressWarnings("unchecked")
@Override
public synchronized void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action); // explicit check required in case
// table is empty.
final int expectedModCount = modCount;
Entry<?, ?>[] tab = table;
for (Entry<?, ?> entry : tab) {
while (entry != null) {
@ThomasLau
ThomasLau / gist:814a4093134a1a19510d
Created March 20, 2015 02:06
pieces on writeObject and readobject methord in HashTable
@Override
public synchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index];
for (Entry<K,V> prev = null; e != null; prev = e, e = e.next) {
@ThomasLau
ThomasLau / gist:c7a2da2c4bcb11539b54
Created March 20, 2015 02:11
How HashMap serialize and deserialize itself and some awesome function in HashMap
private void writeObject(java.io.ObjectOutputStream s)
throws IOException {
int buckets = capacity();
// Write out the threshold, loadfactor, and any hidden stuff
s.defaultWriteObject();
s.writeInt(buckets);
s.writeInt(size);
internalWriteEntries(s);
}
@ThomasLau
ThomasLau / gist:d89e127c04c164ed5616
Created March 27, 2015 01:25
HashMap里的tableSizeFor
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= 1024) ? 1024 : n + 1;
}
这段方法,其实cap大于1024的时候,返回1024,这显然可以先放在开头的时候判断并直接返回的,不知道,hashmap为何要在最后。
@ThomasLau
ThomasLau / gist:8ff178da7bedb968c5bb
Last active August 29, 2015 14:17
Nginx带宽控制-摘自火丁笔记

Nginx带宽控制

有个老项目,通过 Squid 提供文件下载功能,利用 delay_parameters 实现带宽控制,问题是我玩不转 Squid,于是盘算着是不是能在 Nginx 里找到类似的功能。

好消息是 Nginx 提供了 limit_rate 和 limit_rate_after,举个例子来说明一下:

location /download/ {
    limit_rate_after 500k;
    limit_rate 50k;
}