{"id":1965,"date":"2019-09-29T08:30:34","date_gmt":"2019-09-29T00:30:34","guid":{"rendered":"https:\/\/coderbee.net\/?p=1965"},"modified":"2019-09-29T08:55:05","modified_gmt":"2019-09-29T00:55:05","slug":"threadlocal-inheritablethreadlocal-%e8%ae%be%e8%ae%a1%e4%b8%8e%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20190929\/1965","title":{"rendered":"ThreadLocal\/InheritableThreadLocal \u8bbe\u8ba1\u4e0e\u6e90\u7801\u5206\u6790"},"content":{"rendered":"<p>ThreadLocal \u63d0\u4f9b\u4e86\u7ebf\u7a0b\u672c\u5730\u7684\u53d8\u91cf\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u53ea\u80fd\u901a\u8fc7 <code>get\/set<\/code> \u65b9\u6cd5\u8bbf\u95ee\u81ea\u5df1\u7684\u53d8\u91cf\u3002\u6b64\u7c7b\u7684\u5b9e\u4f8b\u901a\u5e38\u58f0\u660e\u4e3a\u7c7b\u7684 <code>private static<\/code> \u5c5e\u6027\u3001\u7528\u6765\u628a\u72b6\u6001\uff08\u6bd4\u5982\u4e8b\u52a1ID\uff09\u5173\u8054\u5230\u7ebf\u7a0b\u4e0a\u3002<\/p>\n<p>InheritableThreadLocal \u6269\u5c55\u4e86 ThreadLocal\uff0c\u4e3a\u5b50\u7ebf\u7a0b\u63d0\u4f9b\u4ece\u7236\u7ebf\u7a0b\u90a3\u91cc\u7ee7\u627f\u7684\u503c\uff1a\u5728<strong>\u521b\u5efa\u5b50\u7ebf\u7a0b\u65f6<\/strong>\uff0c\u5b50\u7ebf\u7a0b\u4f1a\u63a5\u6536\u6240\u6709\u53ef\u7ee7\u627f\u7684\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\u7684\u521d\u59cb\u503c\uff0c\u4ee5\u83b7\u5f97\u7236\u7ebf\u7a0b\u6240\u5177\u6709\u7684\u503c\u3002<\/p>\n<h1>1. \u5b9e\u73b0\u601d\u8def<\/h1>\n<p>\u5982\u679c\u81ea\u884c\u5b9e\u73b0\u4e00\u4e2a ThreadLocal\uff0c\u76f4\u63a5\u601d\u8def\u53ef\u80fd\u662f\uff1aThreadLocal \u5185\u7ef4\u62a4\u4e00\u4e2a Map\uff0c\u4ee5 \u7ebf\u7a0b\u5bf9\u8c61\u4e3a key\uff0cvalue \u4e3a\u53d8\u91cf\u3002<\/p>\n<p>\u8fd9\u4e2a\u601d\u8def\u7684\u95ee\u9898\u6709\uff1a<br \/>\n1. \u5f53\u7ebf\u7a0b\u7ec8\u6b62\u3001JVM \u8fdb\u884c\u5783\u573e\u56de\u6536\u65f6\uff0c\u8fd9\u4e2a Map \u8fd8\u6301\u6709\u5bf9\u7ebf\u7a0b\u7684\u5f15\u7528\u800c\u6ca1\u6cd5\u56de\u6536\u7ebf\u7a0b\u7684\u8d44\u6e90\uff1b\u5982\u679c JVM \u8981\u80fd\u56de\u6536\uff0c\u90a3\u4e48\u5fc5\u987b\u77e5\u9053\u6709\u591a\u5c11 ThreadLocal \u5b9e\u4f8b\u6301\u6709\u5bf9\u7ebf\u7a0b\u7684\u5f15\u7528\uff0c\u8fd9\u4f1a\u7ed9 JVM \u5e26\u6765\u8d1f\u62c5\u3002<br \/>\n2. \u4e3a\u4e86\u5b9e\u73b0 InheritableThreadLocal \u65f6\uff0c\u5728\u521b\u5efa\u65f6\u8fd8\u5fc5\u987b\u627e\u51fa\u6240\u6709\u7684 InheritableThreadLocal\uff0c\u5224\u65ad\u7236\u7ebf\u7a0b\u662f\u5426\u6709\u8bbe\u7f6e\u53d8\u91cf\uff0c\u6709\u7684\u5219\u8fdb\u884c\u62f7\u8d1d\u53d8\u91cf\u3002<\/p>\n<p>\u4ece\u4e0a\u8ff0\u95ee\u9898\u6765\u770b\uff0c\u5b9e\u73b0\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u81f3\u5c11\u5e94\u8be5\u8003\u8651\uff1a<br \/>\n1. \u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u4e0d\u5e94\u8be5\u76f4\u63a5\u6301\u6709\u5bf9 Thread \u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u907f\u514d\u7ed9 JVM \u56de\u6536 Thread \u5e26\u6765\u989d\u5916\u7684\u5f00\u9500\uff1b<br \/>\n2. \u4e3a\u5b9e\u73b0 InheritableThreadLocal\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5728\u54ea\u4e9b InheritableThreadLocal \u91cc\u8bbe\u7f6e\u4e86\u53d8\u91cf\u5e94\u8be5\u6709\u4e2a\u96c6\u4e2d\u5f0f\u7684\u5b58\u50a8\uff0c\u8fd9\u6837\u624d\u65b9\u4fbf\u628a\u7236\u7ebf\u7a0b\u7684\u53ef\u7ee7\u627f\u672c\u5730\u53d8\u91cf\u62f7\u8d1d\u5230\u5b50\u7ebf\u7a0b\u7684\u3002<br \/>\n3. \u53ef\u80fd\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9 ThreadLocal \u8fdb\u884c\u8bbe\u7f6e\u53d8\u91cf\uff0c\u90a3\u4e48\u5bf9 Map \u7684\u8bbf\u95ee\u5e94\u5f53\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<p>\u518d\u6765\u770b\u4e0b\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u6d89\u53ca\u54ea\u4e9b\u53c2\u4e0e\u8005\uff1aThreadLocal \u3001Thread\u3001\u53d8\u91cf\u503c\u3002<\/p>\n<p>\u4e00\u4e2a ThreadLocal \u53ef\u4ee5\u6301\u6709\u591a\u4e2a Thread \u7684\u53d8\u91cf\uff0c\u4e00\u4e2a Thread \u4e5f\u53ef\u4ee5\u5728\u591a\u4e2a ThreadLocal \u4e0a\u8bbe\u7f6e\u53d8\u91cf\u3002\u56e0\u6b64\u4e00\u4e2a (ThreadLocal, Thread) \u7684\u7ec4\u5408\u624d\u80fd\u552f\u4e00\u786e\u5b9a\u4e00\u4e2a\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u503c\u3002<\/p>\n<p>Map \u53ea\u80fd\u653e\u5728 (ThreadLocal, Thread) \u4e2d\u7684\u4e00\u4e2a\uff0c\u524d\u9762\u4e5f\u8bf4\u4e86\u653e\u5728 ThreadLocal \u4e0a\u662f\u4e0d\u5408\u9002\u7684\u3002\u518d\u6765\u770b\u770b\u653e\u5728 Thread \u4e0a\u5982\u4f55\u3002<\/p>\n<p>\u6bcf\u4e2a Thread \u7684 Map \u5c5e\u6027\u7684 key \u662f ThreadLocal \u5bf9\u8c61\uff0cvalue \u662f\u53d8\u91cf\u503c\uff0c\u770b\u6765\u4e5f\u80fd\u5b9e\u73b0\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u3002<\/p>\n<p>\u8fd9\u6837\u53cd\u8f6c\u4e4b\u540e\uff0cThreadLocal \u4e0d\u4f1a\u6301\u6709\u7ebf\u7a0b\u7684\u5f15\u7528\uff0c\u7ebf\u7a0b\u56de\u6536\u4e0d\u5b58\u5728\u95ee\u9898\uff0c\u7ebf\u7a0b\u7684 Map \u4e5f\u53ef\u4ee5\u5728\u7ebf\u7a0b\u56de\u6536\u65f6\u8fdb\u884c\u56de\u6536\uff0cMap \u91cc\u9762\u4fdd\u5b58\u7684\u53d8\u91cf\u503c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u56de\u6536\u3002<\/p>\n<p>\u53ef\u7ee7\u627f\u7684\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u53ef\u4ee5\u7528\u53e6\u4e00\u4e2a Map \u6765\u7ef4\u62a4\uff0c\u8d77\u5230\u4e86\u96c6\u4e2d\u5b58\u50a8\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u53ea\u8bbf\u95ee\u81ea\u5df1\u7684 Map\uff0c\u81ea\u7136\u6ca1\u6709\u5e76\u53d1\u7684\u7ade\u4e89\u3002<\/p>\n<p>\u5b8c\u7f8e\uff01JDK \u4ece 1.3 \u5f00\u59cb\u5c31\u662f\u6309\u8fd9\u4e2a\u601d\u8def\u53bb\u5b9e\u73b0\u7684\u3002<\/p>\n<p><!--more--><\/p>\n<h1>2. JDK \u91cc\u7684\u5b9e\u73b0<\/h1>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u662f JDK 1.8.0_40 \u7684\u3002<\/p>\n<p>ThreadLocalMap \u662f\u4e00\u4e2a\u5b9a\u5236\u7684\u3001\u53ea\u9002\u5408\u7ef4\u62a4\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u7684 ThreadLocal \u7684\u5185\u90e8\u7c7b\u3002<\/p>\n<p>Thread \u7c7b\u58f0\u660e\u4e86\u4e24\u4e2a <code>ThreadLocal.ThreadLocalMap<\/code> \u7c7b\u578b\u7684\u53d8\u91cf <code>threadLocals<\/code> \u548c <code>inheritableThreadLocals<\/code> \uff0c\u5206\u522b\u7528\u4e8e\u4fdd\u5b58\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u548c\u53ef\u7ee7\u627f\u7684\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u3002<\/p>\n<p>\u8fd9\u6837\u5b9e\u73b0 <code>InheritableThreadLocal<\/code> \u5c31\u975e\u5e38\u7b80\u5355\uff0c\u5728\u521d\u59cb\u5316\u5b50\u7ebf\u7a0b\u65f6\u7528\u7236\u7ebf\u7a0b\u7684 <code>inheritableThreadLocals<\/code>  \u521d\u59cb\u5b50\u7ebf\u7a0b\u7684 <code>inheritableThreadLocals<\/code> \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<pre><code class=\"java\">public class Thread implements Runnable {\n    \/\/ \u5305\u7ea7\u8bbf\u95ee\u7ea7\u522b\n    ThreadLocal.ThreadLocalMap threadLocals = null;\n\n    ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;\n\n    private void init(ThreadGroup g, Runnable target, String name,\n                      long stackSize, AccessControlContext acc) {\n        \/\/ \u7701\u7565\u4e86\u5176\u4ed6\u521d\u59cb\u5316\u4ee3\u7801\n        Thread parent = currentThread();\n\n        if (parent.inheritableThreadLocals != null)\n            this.inheritableThreadLocals =\n                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);\n\n        \/\/ \u7701\u7565\u4e86\u5176\u4ed6\u521d\u59cb\u5316\u4ee3\u7801\n    }\n}\n\n\npublic class ThreadLocal&lt;T&gt; {\n    public T get() {\n        Thread t = Thread.currentThread();\n        ThreadLocalMap map = getMap(t);\n        if (map != null) {\n            ThreadLocalMap.Entry e = map.getEntry(this);\n            if (e != null) {\n                @SuppressWarnings(\"unchecked\")\n                T result = (T)e.value;\n                return result;\n            }\n        }\n        return setInitialValue();\n    }\n\n    public void set(T value) {\n        Thread t = Thread.currentThread();\n        ThreadLocalMap map = getMap(t);\n        if (map != null)\n            map.set(this, value);\n        else\n            createMap(t, value);\n    }\n\n     public void remove() {\n         ThreadLocalMap m = getMap(Thread.currentThread());\n         if (m != null)\n             m.remove(this);\n     }\n\n    ThreadLocalMap getMap(Thread t) {\n        return t.threadLocals;\n    }\n}\n\npublic class InheritableThreadLocal&lt;T&gt; extends ThreadLocal&lt;T&gt; {\n    ThreadLocalMap getMap(Thread t) {\n       return t.inheritableThreadLocals;\n    }\n\n    void createMap(Thread t, T firstValue) {\n        t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);\n    }\n}\n<\/code><\/pre>\n<p>InheritableThreadLocal \u53ea\u9700\u91cd\u5199\u4e86 ThreadLocal \u7684 <code>getMap(Thread)<\/code> \u65b9\u6cd5\u5c31\u53ef\u4ee5\u3002<\/p>\n<h1>3. ThreadLocalMap<\/h1>\n<h2>3.1 \u6570\u636e\u6a21\u578b<\/h2>\n<p>ThreadLocalMap \u662f\u5b9a\u5236\u7684\u3001\u53ea\u9002\u7528\u4e8e\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u7684\u5b9e\u73b0\u3002<\/p>\n<p>ThreadLocalMap \u4ee5\u6570\u7ec4\u6765\u5b58\u653e\u6240\u6709\u7684\u952e\u503c\u5bf9 Entry\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u662f 2 \u7684 N \u6b21\u65b9\uff0c\u8fd9\u6837\u8ba1\u7b97\u67d0\u4e2a\u54c8\u5e0c\u503c\u7684\u5b58\u653e\u4f4d\u7f6e\u65f6\u53ef\u4ee5\u7528\u4f4d\u64cd\u4f5c\u6765\u53d6\u4ee3\u6c42\u6a21<code>%<\/code>\u8fd0\u7b97\uff0c\u6709\u52a9\u4e8e\u63d0\u5347\u6027\u80fd\u3002\u6269\u5bb9\u7684\u65f6\u5019\u6309\u539f\u6765\u7684 2 \u500d\u8fdb\u884c\u6269\u5bb9\u3002<\/p>\n<p>\u6570\u7ec4\u91cc\u5b58\u653e\u7684 Entry \u8d85\u8fc7\u4e00\u5b9a\u7684\u9608\u503c\u65f6\u5c31\u4f1a\u8fdb\u884c resize\uff0c\u4ee5\u4fdd\u8bc1\u6570\u7ec4\u91cc\u603b\u662f\u6709\u7a7a\u95f2\u7684\u4f4d\u7f6e\u53ef\u4ee5\u5b58\u653e\u65b0\u7684 Entry \u3002<\/p>\n<p>\u5bf9\u4e8e\u54c8\u5e0c\u51b2\u7a81\uff0c\u91c7\u7528\u5f00\u653e\u5730\u5740\u6cd5\u6765\u5904\u7406\uff0c\u4ece\u9884\u671f\u4f4d\u7f6e\u5f00\u59cb\u5411\u540e\u7ebf\u6027\u63a2\u6d4b\uff0c\u5230\u4e86\u6570\u7ec4\u672b\u5c3e\u5219\u4ece\u4e0b\u6807 0 \u7ee7\u7eed\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807 Entry \u6216\u53ef\u5b58\u653e\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u91c7\u7528\u7684\u54c8\u5e0c\u51fd\u6570\u662f\u6590\u6ce2\u90a3\u5951\uff08Fibonacci\uff09\u6563\u5217\u6cd5\uff0c\u8be5\u6563\u5217\u7ed3\u5408\u524d\u9762\u7684\u4f4d\u64cd\u4f5c\u80fd\u5728\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u4e0a\u6709\u5f88\u597d\u7684\u6548\u679c\u3002\u5177\u4f53\u53ef\u4ee5\u7f51\u4e0a\u627e\u627e\u8d44\u6599\u3002<\/p>\n<p>\u76f8\u5173\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">public class ThreadLocal&lt;T&gt; {\n    \/\/ \u53d6\u4e0b\u4e00\u4e2a \u6590\u6ce2\u90a3\u5951 \u6563\u5217\u503c\n    private final int threadLocalHashCode = nextHashCode();\n\n    \/\/ \u5206\u914d\u4e0b\u4e00\u4e2a hash code \u7684\u539f\u5b50\u6574\u578b\u53d8\u91cf\n    private static AtomicInteger nextHashCode = new AtomicInteger();\n\n    \/\/ \u6590\u6ce2\u90a3\u5951\uff08Fibonacci\uff09\u6563\u5217\u6cd5\u5728 32 \u4f4d\u7684\u6709\u7b26\u53f7\u4e58\u6570\n    private static final int HASH_INCREMENT = 0x61c88647;\n\n    private static int nextHashCode() {\n        return nextHashCode.getAndAdd(HASH_INCREMENT);\n    }\n\n    \/\/ \u7701\u7565\u5176\u4ed6\u4ee3\u7801\n}\n\n\nstatic class ThreadLocalMap {\n    static class Entry extends WeakReference&lt;ThreadLocal&lt;?&gt;&gt; {\n        Object value;\n\n        Entry(ThreadLocal&lt;?&gt; k, Object v) {\n            super(k);\n            value = v;\n        }\n    }\n\n    \/\/ \u521d\u59cb\u5316\u5bb9\u91cf \n    private static final int INITIAL_CAPACITY = 16;\n\n    \/\/ \u5b58\u653e\u6761\u76ee\u7684\u6570\u7ec4\uff0c\u5176\u957f\u5ea6\u5fc5\u987b\u662f 2 \u7684N\u6b21\u65b9\u3002\u5728\u9700\u8981\u65f6\u8fdb\u884c resize\n    private Entry[] table;\n\n    \/\/ \u8868\u91cc\u6761\u76ee\u7684\u6570\u91cf\n    private int size = 0;\n\n    \/\/ The next size value at which to resize.\n    private int threshold; \/\/ Default to 0\n\n    \/\/ \u6839\u636e\u957f\u5ea6\u8bbe\u7f6e\u9608\u503c\n    private void setThreshold(int len) {\n        threshold = len * 2 \/ 3;\n    }\n}\n<\/code><\/pre>\n<p>\u4e3a\u5565 Entry \u8981\u7ee7\u627f\u81ea WeakReference \uff1f<\/p>\n<blockquote>\n<p>\u56e0\u4e3a\u5f53 ThreadLocal \u4e0d\u518d\u88ab\u5f15\u7528\u3001\u53ef\u4ee5\u88ab\u5783\u573e\u56de\u6536\u65f6\uff0c\u5982\u679c Thread \u7684 ThreadLocalMap \u5c5e\u6027\u4ecd\u7136\u6301\u6709\u5bf9 ThreadLocal  \u7684\u5f3a\u5f15\u7528\uff0c\u90a3\u4e48\u5c31\u4f1a\u5bfc\u81f4 ThreadLocal \u5bf9\u8c61\u7684\u5185\u5b58\u6cc4\u6f0f\u3002\u5982\u679c\u7528 WeakReference \u6765\u5f15\u7528 ThreadLocal\uff0c\u90a3\u4e48 ThreadLocal \u88ab\u56de\u6536\u65f6\uff0cWeakReference \u91cc\u7684\u5f15\u7528\u88ab\u8bbe\u7f6e\u4e3a null \u3002\u8fd9\u65f6\u4e5f\u8868\u793a\u8fd9\u4e2a Entry \u53ef\u5783\u573e\u56de\u6536\u3002ThreadLocalMap \u53ef\u4ee5\u5728\u7279\u5b9a\u7684\u65f6\u5019\u56de\u6536\u8fd9\u4e2a Entry\uff0c\u9632\u6b62\u5176\u4ea7\u751f\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/blockquote>\n<h2>3.2 \u8bfb\u53d6<\/h2>\n<p>\u8bfb\u53d6\u64cd\u4f5c\u7684\u4e00\u4e2a\u4f18\u5316\u662f\u9996\u5148\u5c1d\u8bd5\u5feb\u901f\u8def\u5f84\u8bbf\u95ee\uff0c\u4e5f\u5c31\u662f\u6839\u636e ThreadLocal \u7684\u54c8\u5e0c\u503c threadLocalHashCode \u8ba1\u7b97\u9884\u671f\u7684\u5b58\u653e\u4f4d\u7f6e\uff0c\u5982\u679c\u547d\u4e2d\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5219\u9700\u8981\u8fdb\u884c\u7ebf\u6027\u904d\u5386\u3002<\/p>\n<p>\u5982\u679c\u80fd\u76f4\u63a5\u547d\u4e2d\uff0c\u8bf4\u660e put \u64cd\u4f5c\u65f6\u6ca1\u6709\u51b2\u7a81\u3002<\/p>\n<pre><code class=\"java\">private Entry getEntry(ThreadLocal&lt;?&gt; key) {\n    \/\/ \u9996\u5148\u5c1d\u8bd5\u5feb\u901f\u8bbf\u95ee\uff0c\u5982\u679c\u51b2\u7a81\u4e0d\u591a\uff0c\u90a3\u4e48\u6548\u7387\u4f1a\u5f88\u9ad8\u3002\n    int i = key.threadLocalHashCode &amp; (table.length - 1); \/\/ \u76f8\u5f53\u4e8e mod \n    Entry e = table[i];\n    if (e != null &amp;&amp; e.get() == key)\n        return e;\n    else\n        return getEntryAfterMiss(key, i, e);\n        \/\/ \u5728\u9884\u671f\u4f4d\u7f6e\u6ca1\u6709\u547d\u4e2d\uff0c\u8bf4\u660e\u6709\u51b2\u7a81\uff0c\u9700\u8981\u904d\u5386\u6765\u67e5\u627e\n}\n\nprivate Entry getEntryAfterMiss(ThreadLocal&lt;?&gt; key, int i, Entry e) {\n    Entry[] tab = table;\n    int len = tab.length;\n\n    while (e != null) {\n        ThreadLocal&lt;?&gt; k = e.get();\n        if (k == key)\n            return e;\n        if (k == null)\n            expungeStaleEntry(i);\n        else\n            i = nextIndex(i, len);\n        e = tab[i];\n    }\n    return null;\n}\n<\/code><\/pre>\n<h2>3.3 put<\/h2>\n<pre><code class=\"java\">private void set(ThreadLocal&lt;?&gt; key, Object value) {\n    \/\/ We don't use a fast path as with get() because it is at\n    \/\/ least as common to use set() to create new entries as\n    \/\/ it is to replace existing ones, in which case, a fast\n    \/\/ path would fail more often than not.\n    \/\/ \u6b64\u65b9\u6cd5\u4e0d\u50cf get \u65b9\u6cd5\u90a3\u6837\u4f7f\u7528\u5feb\u901f\u8def\u5f84\u662f\u56e0\u4e3a\uff1a\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Entry \u4e0e\u66ff\u6362\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u662f\u4e00\u6837\u666e\u904d\u7684\u3002\n    \/\/ \u5728\u8fd9\u6837\u7684\u60c5\u51b5\u4e0b\uff0c\u5feb\u901f\u8def\u5f84\u66f4\u5bb9\u6613\u5931\u8d25\u3002\n\n    Entry[] tab = table;\n    int len = tab.length;\n    int i = key.threadLocalHashCode &amp; (len-1);\n\n    \/\/ \u603b\u662f\u5b58\u5728 tab[n] == null\n    for (Entry e = tab[i];\n         e != null;\n         e = tab[i = nextIndex(i, len)]) {\n        ThreadLocal&lt;?&gt; k = e.get();\n\n        if (k == key) {\n            \/\/ \u547d\u4e2d\uff0c\u8986\u76d6\u65e7\u503c\n            e.value = value;\n            return;\n        }\n\n        if (k == null) {\n            \/\/ \u78b0\u5230\u4e00\u4e2a\u53ef\u56de\u6536\u7684 Entry\uff0c\u5c1d\u8bd5\u56de\u6536\u53ef\u56de\u6536\u7684Entry\u5e76\u91cd\u7528\u8fd9\u4e2a\u4f4d\u7f6e\n            replaceStaleEntry(key, value, i);\n            return;\n        }\n    }\n\n    \/\/ i \u4f4d\u7f6e\u6ca1\u6709\u5b58\u653e Entry\n    tab[i] = new Entry(key, value);\n    int sz = ++size;\n\n    \/\/ \u5224\u7b49\u9700\u8981\u9700\u8981 rehash\uff0c\u4fdd\u8bc1\u603b\u662f\u5b58\u5728 table[n] == null\n    if (!cleanSomeSlots(i, sz) &amp;&amp; sz &gt;= threshold)\n        rehash();\n}\n\nprivate void replaceStaleEntry(ThreadLocal&lt;?&gt; key, Object value, int staleSlot) {\n    Entry[] tab = table;\n    int len = tab.length;\n    Entry e;\n\n    \/\/ Back up to check for prior stale entry in current run.\n    \/\/ We clean out whole runs at a time to avoid continual\n    \/\/ incremental rehashing due to garbage collector freeing\n    \/\/ up refs in bunches (i.e., whenever the collector runs).\n    int slotToExpunge = staleSlot;\n    for (int i = prevIndex(staleSlot, len);\n         (e = tab[i]) != null;\n         i = prevIndex(i, len))\n        if (e.get() == null)\n            slotToExpunge = i;\n\n    \/\/ Find either the key or trailing null slot of run, whichever\n    \/\/ occurs first\n    for (int i = nextIndex(staleSlot, len);\n         (e = tab[i]) != null;\n         i = nextIndex(i, len)) {\n        ThreadLocal&lt;?&gt; k = e.get();\n\n        \/\/ If we find key, then we need to swap it\n        \/\/ with the stale entry to maintain hash table order.\n        \/\/ The newly stale slot, or any other stale slot\n        \/\/ encountered above it, can then be sent to expungeStaleEntry\n        \/\/ to remove or rehash all of the other entries in run.\n        if (k == key) {\n            e.value = value;\n\n            tab[i] = tab[staleSlot];\n            tab[staleSlot] = e;\n\n            \/\/ Start expunge at preceding stale entry if it exists\n            if (slotToExpunge == staleSlot)\n                slotToExpunge = i;\n            cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);\n            return;\n        }\n\n        \/\/ If we didn't find stale entry on backward scan, the\n        \/\/ first stale entry seen while scanning for key is the\n        \/\/ first still present in the run.\n        if (k == null &amp;&amp; slotToExpunge == staleSlot)\n            slotToExpunge = i;\n    }\n\n    \/\/ If key not found, put new entry in stale slot\n    tab[staleSlot].value = null;\n    tab[staleSlot] = new Entry(key, value);\n\n    \/\/ If there are any other stale entries in run, expunge them\n    if (slotToExpunge != staleSlot)\n        cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);\n}\n<\/code><\/pre>\n<h2>3.4 rehash\/resize<\/h2>\n<p>\u9996\u5148\u5c1d\u8bd5\u56de\u6536\u6240\u6709\u53ef\u56de\u6536\u7684 Entry\uff0c\u56de\u6536\u540e entry \u6570\u91cf\u4ecd\u7136\u9ad8\u4e8e\u9608\u503c\u5219\u8fdb\u884c resize \u3002<\/p>\n<pre><code class=\"java\">private void rehash() {\n    expungeStaleEntries();\n\n    \/\/ Use lower threshold for doubling to avoid hysteresis\n    \/\/ \n    if (size &gt;= threshold - threshold \/ 4)\n        resize();\n}\n\nprivate void resize() {\n    Entry[] oldTab = table;\n    int oldLen = oldTab.length;\n    int newLen = oldLen * 2;\n    Entry[] newTab = new Entry[newLen];\n    int count = 0;\n\n    for (int j = 0; j &lt; oldLen; ++j) {\n        Entry e = oldTab[j];\n        if (e != null) {\n            ThreadLocal&lt;?&gt; k = e.get();\n            if (k == null) {\n                e.value = null; \/\/ Help the GC\n            } else {\n                int h = k.threadLocalHashCode &amp; (newLen - 1);\n                while (newTab[h] != null)\n                    h = nextIndex(h, newLen);\n                newTab[h] = e;\n                count++;\n            }\n        }\n    }\n\n    setThreshold(newLen);\n    size = count;\n    table = newTab;\n}\n<\/code><\/pre>\n<h2>3.5 \u56de\u6536 entry<\/h2>\n<pre><code class=\"java\">\/\/ \u56de\u6536\u6240\u6709\u7684\u53ef\u56de\u6536 entry\nprivate void expungeStaleEntries() {\n    Entry[] tab = table;\n    int len = tab.length;\n    for (int j = 0; j &lt; len; j++) {\n        Entry e = tab[j];\n        if (e != null &amp;&amp; e.get() == null)\n            expungeStaleEntry(j);\n    }\n}\n\n\/\/ \u4ece staleSlot \u4f4d\u7f6e\u5f00\u59cb\u56de\u6536 entry\uff0c\n\/\/ \u76f4\u5230\u9047\u5230 tab[i] == null \u65f6\u7ed3\u675f\nprivate int expungeStaleEntry(int staleSlot ) {\n    Entry[] tab = table;\n    int len = tab.length;\n\n    \/\/ expunge entry at staleSlot\n    tab[staleSlot].value = null;\n    tab[staleSlot] = null;\n    size--;\n\n    \/\/ Rehash until we encounter null\n    Entry e;\n    int i;\n    for (i = nextIndex(staleSlot, len);\n         (e = tab[i]) != null;\n         i = nextIndex(i, len)) {\n        ThreadLocal&lt;?&gt; k = e.get();\n        if (k == null) {\n            \/\/ \u627e\u5230\u4e86\u4e00\u4e2a\u53ef\u56de\u6536\u7684entry\n            e.value = null;\n            tab[i] = null;\n            size--;\n        } else {\n            int h = k.threadLocalHashCode &amp; (len - 1);\n            if (h != i) {\n                \/\/ k \u4e0d\u5728\u5b83\u7684\u6700\u4f73\u4f4d\u7f6e\uff0c\u628a\u5b83\u5f80\u6700\u4f73\u4f4d\u7f6e\u9760\u8fd1\uff0c\n                \/\/ \u51cf\u5c11\u540e\u7eed\u67e5\u627e\u5b83\u65f6\u9700\u8981\u904d\u5386\u7684\u4f4d\u7f6e\uff0c\u63d0\u5347\u6548\u7387\n                tab[i] = null;\n\n                \/\/ Unlike Knuth 6.4 Algorithm R, we must scan until\n                \/\/ null because multiple entries could have been stale.\n                while (tab[h] != null)\n                    h = nextIndex(h, len);\n                tab[h] = e;\n            }\n        }\n    }\n    return i;\n}\n<\/code><\/pre>\n<h1>4. \u603b\u7ed3<\/h1>\n<h2>4.1 ThreadLocal \u5c0f\u7ed3<\/h2>\n<ol>\n<li>\u5982\u679c\u5728 ThreadLocal \u91cc\u7ef4\u62a4\u7ebf\u7a0b\u5230\u53d8\u91cf\u7684\u6620\u5c04 Map\uff0c\u90a3\u4e48\uff1a\u8fd9\u4e2a Map \u9700\u8981\u63d0\u4f9b\u7ebf\u7a0b\u5b89\u5168\u6027\u8bbf\u95ee\u80fd\u529b\uff1b\u4e3a\u4e86\u9632\u6b62\u7ebf\u7a0b\u7ec8\u6b62\u65f6\u6ca1\u6cd5\u88ab\u56de\u6536\uff0c\u8fd9\u4e2a Map \u5e94\u8be5\u628a\u7ebf\u7a0b\u5c01\u88c5\u5728 <code>WeakReference<\/code> \u91cc\u3002<\/li>\n<li>\u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0 <code>InheritableThreadLocal<\/code> \u8bed\u4e49\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5728\u54ea\u4e9b  <code>InheritableThreadLocal<\/code> \u91cc\u8bbe\u7f6e\u4e86\u53d8\u91cf\u5e94\u8be5\u6709\u4e2a\u96c6\u4e2d\u5b58\u653e\u7684\u5730\u65b9\u3002<\/li>\n<li>\u7efc\u4e0a2\u70b9\uff0cJDK \u91c7\u7528\u5728\u7ebf\u7a0b\u91cc\u7ef4\u62a4 <code>ThreadLocal<\/code> \u5230\u53d8\u91cf\u6620\u5c04\u7684\u4e24\u4e2a Map\uff0c\u5206\u522b\u7528\u4e8e\u5b58\u653e  <code>ThreadLocal<\/code> \u548c  <code>InheritableThreadLocal<\/code> \u5230\u53d8\u91cf\u7684\u6620\u5c04\u3002\u8fd9\u4e24\u4e2a Map \u662f\u7ebf\u7a0b\u79c1\u6709\u7684\uff0c\u5929\u7136\u5c31\u5177\u6709\u7ebf\u7a0b\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n<h2>4.2 ThreadLocalMap \u5c0f\u7ed3<\/h2>\n<p>ThreadLocalMap \u662f\u5b9a\u5236\u7684\u3001\u53ea\u9002\u7528\u4e8e ThreadLocal \u7684\u4e00\u4e2a Map \u5b9e\u73b0\u3002<br \/>\n\u91c7\u7528 \u6570\u7ec4\u3001\u6590\u6ce2\u90a3\u5951\u6563\u5217\u6cd5\u751f\u6210 ThreadLocal \u7684\u54c8\u5e0c\u503c\u518d\u5bf9\u6570\u7ec4\u6c42\u6a21\u4f5c\u4e3a\u54c8\u5e0c\u51fd\u6570\u3001\u5f00\u653e\u5730\u5740\u6cd5\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002<\/p>\n<p>Entry \u7ee7\u627f WeakReference\uff0c\u628a key ThreadLocal \u5c01\u88c5\u5728 WeakReference \u7684\u91cc\uff0c\u5f31\u5f15\u7528\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u65f6\u8868\u793a\u8be5 entry \u53ef\u56de\u6536\uff0c\u79f0\u4e3a stale entry\u3002<\/p>\n<p>\u5bf9\u8be5 map \u5b9e\u73b0\u8fdb\u884c put \u64cd\u4f5c\u65f6\u4f1a\u89e6\u53d1\u56de\u6536 stale entry \u3002<\/p>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ThreadLocal \u63d0\u4f9b\u4e86\u7ebf\u7a0b\u672c\u5730\u7684\u53d8\u91cf\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u53ea\u80fd\u901a\u8fc7 get\/set  &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20190929\/1965\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[327,325,326],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1965"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1965"}],"version-history":[{"count":6,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1965\/revisions"}],"predecessor-version":[{"id":1971,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1965\/revisions\/1971"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}