{"id":2727,"date":"2018-04-29T17:51:40","date_gmt":"2018-04-29T09:51:40","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2727"},"modified":"2018-04-29T17:52:08","modified_gmt":"2018-04-29T09:52:08","slug":"threadlocal-%e5%92%8c-threadlocalmap-%e5%8e%9f%e7%90%86%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2727","title":{"rendered":"Threadlocal \u548c ThreadLocalMap \u539f\u7406\u89e3\u6790"},"content":{"rendered":"<p>\n\tjava \u4e2d\u63d0\u4f9b\u4e86\u4e24\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u64cd\u4f5c\u65b9\u5f0f\uff0c\u4e00\u4e2a\u662f Threadlocal \uff0c\u4e00\u4e2a\u662fThreadLocalMap\u3002\u672c\u6587\u4ecb\u7ecd\u4ed6\u4eec\u7684\u533a\u522b\u548c\u5b9e\u73b0\u539f\u7406\u3002\n<\/p>\n<h2>\n\tThreadlocal<br \/>\n<\/h2>\n<p>\n\tThreadlocal \u7684\u5c5e\u6027\u5305\u62ec threadLocalHashCode\uff0cgetMap(Thread t) \u5230\u5f53\u524d\u7ebf\u7a0b\u53bb\u83b7\u53d6\u4e00\u4e2a ThreadLocalMap \u7684\u5185\u90e8\u7c7b\u5bf9\u8c61\uff0c\u5b83\u7684\u5c5e\u6027\u6709\u4e00\u4e2a entry \u6570\u7ec4\uff0c\u6570\u7ec4\u4e0b\u6807\u662f\u5f53\u524d Threadlocal \u5bf9\u5e94\u7684hash\u503c\uff0centry \u5bf9\u8c61\u662f\u4e00\u4e2a WeakReference \u5bf9\u8c61\uff0c\u5b58\u653e\u5bf9\u5e94 Threadlocal \u9700\u8981\u4fdd\u5b58\u7684\u53d8\u91cf\u503c value\u3002\n<\/p>\n<p>\n\tThreadLocal \u7528\u4e8e\u63d0\u4f9b\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\uff0c\u901a\u8fc7\u5b83\u7684 get() \u548c set() \u65b9\u6cd5\u8bbf\u95ee\u67d0\u4e2a\u53d8\u91cf\u76f8\u5e94\u7ebf\u7a0b\u81ea\u5df1\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u4f46\u5728\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u662f\u901a\u8fc7\u5404\u4e2a\u7ebf\u7a0b\u81ea\u5df1\u521b\u5efa\u5bf9\u8c61\u7136\u540e\u8c03\u7528\u8fd9\u91cc\u7684set\u65b9\u6cd5\u8bbe\u7f6e\u8fdb\u53bb\uff0c\u800c\u4e0d\u662f\u7531 ThreadLocal \u81ea\u5df1\u6765\u521b\u5efa\u53d8\u91cf\u7684\u526f\u672c\u7684\u3002\n<\/p>\n<h2>\n\tThreadLocalMap<br \/>\n<\/h2>\n<p>\n\t\u901a\u8fc7Entry\u6570\u7ec4\u4fdd\u5b58\u5c40\u90e8\u53d8\u91cf\u3002\u901a\u8fc7key\uff08ThreadLocal\u7c7b\u578b\uff09\u7684hashcode\u6765\u8ba1\u7b97\u6570\u7ec4\u5b58\u50a8\u7684\u7d22\u5f15\u4f4d\u7f6ei\u3002\u5982\u679ci\u4f4d\u7f6e\u5df2\u7ecf\u5b58\u50a8\u4e86\u5bf9\u8c61\uff0c\u90a3\u4e48\u5c31\u5f80\u540e\u632a\u4e00\u4e2a\u4f4d\u7f6e\u4f9d\u6b21\u7c7b\u63a8\uff0c\u76f4\u5230\u627e\u5230\u7a7a\u7684\u4f4d\u7f6e\uff0c\u518d\u5c06\u5bf9\u8c61\u5b58\u653e\u3002\u53e6\u5916\uff0c\u5728\u6700\u540e\u8fd8\u9700\u8981\u5224\u65ad\u4e00\u4e0b\u5f53\u524d\u7684\u5b58\u50a8\u7684\u5bf9\u8c61\u4e2a\u6570\u662f\u5426\u5df2\u7ecf\u8d85\u51fa\u4e86\u9608\u503c\uff08threshold\u7684\u503c\uff09\u5927\u5c0f\uff0c\u5982\u679c\u8d85\u51fa\u4e86\uff0c\u9700\u8981\u91cd\u65b0\u6269\u5145\u5e76\u5c06\u6240\u6709\u7684\u5bf9\u8c61\u91cd\u65b0\u8ba1\u7b97\u4f4d\u7f6e\u3002\n<\/p>\n<p>\n\t\u7ebf\u7a0b\u4fdd\u5b58ThreadLocalMap\u5bf9\u8c61\uff0c\u5bf9\u8c61\u4e3b\u8981\u901a\u8fc7Entry[]\u6570\u7ec4\u5b58\u653e\u952e{threadlocal}\u503c\uff0c\u901a\u8fc7threadlocal\u7684threadLocalHashCode\u5b9a\u4f4d\u5b58\u653e\u6570\u7ec4\u4f4d\u7f6e\uff0cEntry extendsWeakReference&lt;ThreadLocal&gt; \u7684value\u4fdd\u5b58\u53d8\u91cf\u526f\u672c\uff0c\u901a\u8fc7Entry.get\u83b7\u53d6threadlocal\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u8fd9\u91cc\u4f7f\u7528\u666e\u901a\u7684key-value\u5f62\u5f0f\u6765\u5b9a\u4e49\u5b58\u50a8\u7ed3\u6784\uff0c\u5b9e\u8d28\u4e0a\u5c31\u4f1a\u9020\u6210\u8282\u70b9\u7684\u751f\u547d\u5468\u671f\u4e0e\u7ebf\u7a0b\u5f3a\u7ed1\u5b9a\uff0c\u53ea\u8981\u7ebf\u7a0b\u6ca1\u6709\u9500\u6bc1\uff0c\u90a3\u4e48\u8282\u70b9\u5728GC\u5206\u6790\u4e2d\u4e00\u76f4\u5904\u4e8e\u53ef\u8fbe\u72b6\u6001\uff0c\u6ca1\u529e\u6cd5\u88ab\u56de\u6536\uff0c\u800c\u7a0b\u5e8f\u672c\u8eab\u4e5f\u65e0\u6cd5\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u6e05\u7406\u8282\u70b9\u3002\u5f31\u5f15\u7528\u662fJava\u4e2d\u56db\u6863\u5f15\u7528\u7684\u7b2c\u4e09\u6863\uff0c\u6bd4\u8f6f\u5f15\u7528\u66f4\u52a0\u5f31\u4e00\u4e9b\uff0c\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u6ca1\u6709\u5f3a\u5f15\u7528\u94fe\u53ef\u8fbe\uff0c\u90a3\u4e48\u4e00\u822c\u6d3b\u4e0d\u8fc7\u4e0b\u4e00\u6b21GC\u3002\u5f53\u67d0\u4e2aThreadLocal\u5df2\u7ecf\u6ca1\u6709\u5f3a\u5f15\u7528\u53ef\u8fbe\uff0c\u5219\u968f\u7740\u5b83\u88ab\u5783\u573e\u56de\u6536\uff0c\u5728ThreadLocalMap\u91cc\u5bf9\u5e94\u7684Entry\u7684\u952e\u503c\u4f1a\u5931\u6548\uff0c\u8fd9\u4e3aThreadLocalMap\u672c\u8eab\u7684\u5783\u573e\u6e05\u7406\u63d0\u4f9b\u4e86\u4fbf\u5229\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"ThreadLocal\" src=\"https:\/\/lark-assets-prod.oss-cn-hangzhou.aliyuncs.com\/2018\/png\/11db4636-8305-4289-ae09-3f1182a19786.png\" style=\"width: 1430px;height: 840px\" \/>\n<\/p>\n<p>\n\tThreadLocalMap\u7ef4\u62a4\u4e86Entry\u73af\u5f62\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u5143\u7d20Entry\u7684\u903b\u8f91\u4e0a\u7684key\u4e3a\u67d0\u4e2aThreadLocal\u5bf9\u8c61\uff08\u5b9e\u9645\u4e0a\u662f\u6307\u5411\u8be5ThreadLocal\u5bf9\u8c61\u7684\u5f31\u5f15\u7528\uff09\uff0cvalue\u4e3a\u4ee3\u7801\u4e2d\u8be5\u7ebf\u7a0b\u5f80\u8be5ThreadLoacl\u53d8\u91cf\u5b9e\u9645\u585e\u5165\u7684\u503c\u3002\n<\/p>\n<p>\n\t\u4eceThreadLocal\u8bfb\u4e00\u4e2a\u503c\u53ef\u80fd\u9047\u5230\u7684\u60c5\u51b5\uff1a\u6839\u636e\u5165\u53c2threadLocal\u7684threadLocalHashCode\u5bf9\u8868\u5bb9\u91cf\u53d6\u6a21\u5f97\u5230 index\n<\/p>\n<ul>\n<li>\n\t\t\u5982\u679cindex\u5bf9\u5e94\u7684slot\u5c31\u662f\u8981\u8bfb\u7684threadLocal\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\n\t<\/li>\n<li>\n\t\t\u8c03\u7528getEntryAfterMiss\u7ebf\u6027\u63a2\u6d4b\uff0c\u8fc7\u7a0b\u4e2d\u6bcf\u78b0\u5230\u65e0\u6548slot\uff0c\u8c03\u7528expungeStaleEntry\u8fdb\u884c\u6bb5\u6e05\u7406\uff1b\u5982\u679c\u627e\u5230\u4e86key\uff0c\u5219\u8fd4\u56de\u7ed3\u679centry\n\t<\/li>\n<li>\n\t\t\u6ca1\u6709\u627e\u5230key\uff0c\u8fd4\u56denull\n\t<\/li>\n<\/ul>\n<p>\n\tThreadLocal\u7684set\u65b9\u6cd5\u53ef\u80fd\u4f1a\u6709\u7684\u60c5\u51b5\u3002\n<\/p>\n<ul>\n<li>\n\t\t\u63a2\u6d4b\u8fc7\u7a0b\u4e2dslot\u90fd\u4e0d\u65e0\u6548\uff0c\u5e76\u4e14\u987a\u5229\u627e\u5230key\u6240\u5728\u7684slot\uff0c\u76f4\u63a5\u66ff\u6362\u5373\u53ef\n\t<\/li>\n<li>\n\t\t\u63a2\u6d4b\u8fc7\u7a0b\u4e2d\u53d1\u73b0\u6709\u65e0\u6548slot\uff0c\u8c03\u7528replaceStaleEntry\uff0c\u6548\u679c\u662f\u6700\u7ec8\u4e00\u5b9a\u4f1a\u628akey\u548cvalue\u653e\u5728\u8fd9\u4e2aslot\uff0c\u5e76\u4e14\u4f1a\u5c3d\u53ef\u80fd\u6e05\u7406\u65e0\u6548slot<\/p>\n<ul>\n<li>\n\t\t\t\t\u5728replaceStaleEntry\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u627e\u5230\u4e86key\uff0c\u5219\u505a\u4e00\u4e2aswap\u628a\u5b83\u653e\u5230\u90a3\u4e2a\u65e0\u6548slot\u4e2d\uff0cvalue\u7f6e\u4e3a\u65b0\u503c\n\t\t\t<\/li>\n<li>\n\t\t\t\t\u5728replaceStaleEntry\u8fc7\u7a0b\u4e2d\uff0c\u6ca1\u6709\u627e\u5230key\uff0c\u76f4\u63a5\u5728\u65e0\u6548slot\u539f\u5730\u653eentry\n\t\t\t<\/li>\n<\/ul>\n<\/li>\n<li>\n\t\t\u63a2\u6d4b\u6ca1\u6709\u53d1\u73b0key\uff0c\u5219\u5728\u8fde\u7eed\u6bb5\u672b\u5c3e\u7684\u540e\u4e00\u4e2a\u7a7a\u4f4d\u7f6e\u653e\u4e0aentry\uff0c\u8fd9\u4e5f\u662f\u7ebf\u6027\u63a2\u6d4b\u6cd5\u7684\u4e00\u90e8\u5206\u3002\u653e\u5b8c\u540e\uff0c\u505a\u4e00\u6b21\u542f\u53d1\u5f0f\u6e05\u7406\uff0c\u5982\u679c\u6ca1\u6e05\u7406\u51fa\u53bbkey\uff0c\u5e76\u4e14\u5f53\u524dtable\u5927\u5c0f\u5df2\u7ecf\u8d85\u8fc7\u9608\u503c\u4e86\uff0c\u5219\u505a\u4e00\u6b21rehash\uff0crehash\u51fd\u6570\u4f1a\u8c03\u7528\u4e00\u6b21\u5168\u91cf\u6e05\u7406slot\u65b9\u6cd5\u4e5f\u5373expungeStaleEntries\uff0c\u5982\u679c\u5b8c\u4e86\u4e4b\u540etable\u5927\u5c0f\u8d85\u8fc7\u4e86threshold &#8211; threshold \/ 4\uff0c\u5219\u8fdb\u884c\u6269\u5bb92\u500d\n\t<\/li>\n<\/ul>\n<p>\n\tJava \u4e2d \u8fd8\u6709\u4e00\u4e2a&nbsp;InheritableThreadLocal \u3002\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u4e0b\u53bb\u7814\u7a76\u4e00\u4e0b\uff0c\u6211\u8fd9\u91cc\u5c31\u4e0d\u4ecb\u7ecd\u4e86\u3002\u8fd8\u6709&nbsp;ThreadLocal \u548c&nbsp;ThreadLocalMap \u7684\u6e90\u7801\uff0c\u4e5f\u5efa\u8bae\u5927\u5bb6\u770b\u770b\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2727\">Threadlocal \u548c ThreadLocalMap \u539f\u7406\u89e3\u6790<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u63d0\u4f9b\u4e86\u4e24\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u64cd\u4f5c\u65b9\u5f0f\uff0c\u4e00\u4e2a\u662f Threadlocal \uff0c\u4e00\u4e2a\u662fThreadLocalMap [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,565,567,618,532],"class_list":["post-2727","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-java-","tag-java8","tag-threadlocal"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2727"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2727\/revisions"}],"predecessor-version":[{"id":2728,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2727\/revisions\/2728"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}