{"id":4017,"date":"2025-07-28T21:45:58","date_gmt":"2025-07-28T13:45:58","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=4017"},"modified":"2025-07-28T21:45:58","modified_gmt":"2025-07-28T13:45:58","slug":"%e5%9c%a8%e5%b9%b6%e5%8f%91%e7%8e%af%e5%a2%83%e4%b8%8b%e5%a6%82%e4%bd%95%e4%bc%98%e9%9b%85%e4%bd%bf%e7%94%a8-map-%e7%9a%84%e5%ae%8c%e6%95%b4%e6%a1%88%e4%be%8b%ef%bc%88%e5%a6%82%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/28\/%e5%9c%a8%e5%b9%b6%e5%8f%91%e7%8e%af%e5%a2%83%e4%b8%8b%e5%a6%82%e4%bd%95%e4%bc%98%e9%9b%85%e4%bd%bf%e7%94%a8-map-%e7%9a%84%e5%ae%8c%e6%95%b4%e6%a1%88%e4%be%8b%ef%bc%88%e5%a6%82%e7%bc%93%e5%ad%98\/","title":{"rendered":"\u5728\u5e76\u53d1\u73af\u5883\u4e0b\u5982\u4f55\u4f18\u96c5\u4f7f\u7528 Map \u7684\u5b8c\u6574\u6848\u4f8b\uff08\u5982\u7f13\u5b58\u7a7f\u900f\u3001\u7ebf\u7a0b\u9694\u79bb\uff09"},"content":{"rendered":"\n<p>\u672c\u7ae0\u8282\u63d0\u4f9b\u4e00\u4e2a<strong>\u5e76\u53d1\u73af\u5883\u4e0b\u4f18\u96c5\u4f7f\u7528 Map \u7684\u5b8c\u6574\u5b9e\u6218\u6848\u4f8b<\/strong>\uff0c\u6db5\u76d6\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ebf\u7a0b\u5b89\u5168\u7684\u7f13\u5b58\u8bbe\u8ba1\uff08\u9632\u6b62\u7f13\u5b58\u7a7f\u900f\uff09<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u9694\u79bb\uff08ThreadLocal + Map\uff09<\/li>\n\n\n\n<li>\u7ed3\u5408 <code>ConcurrentHashMap<\/code> \u5b9e\u73b0\u9ad8\u6548\u8bfb\u5199<\/li>\n\n\n\n<li>\u5178\u578b\u4ee3\u7801\u793a\u4f8b\u4e0e\u8bbe\u8ba1\u601d\u8def\u8be6\u89e3<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u80cc\u666f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u9ad8\u5e76\u53d1\u573a\u666f\uff0c\u4f7f\u7528\u666e\u901a <code>HashMap<\/code> \u4f1a\u5f15\u53d1\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u5bfc\u81f4\u6570\u636e\u6df7\u4e71\u6216\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n\n\n\n<li><code>ConcurrentHashMap<\/code> \u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u7ebf\u7a0b\u5b89\u5168\u8bfb\u5199\u80fd\u529b\uff0c\u662f\u5e76\u53d1\u73af\u5883\u9996\u9009 Map \u5b9e\u73b0\u3002<\/li>\n\n\n\n<li>\u4f46\u662f\uff0c\u7f13\u5b58\u7a7f\u900f\u95ee\u9898\u548c\u7ebf\u7a0b\u9694\u79bb\u95ee\u9898\u9700\u8981\u989d\u5916\u8bbe\u8ba1\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u7f13\u5b58\u7a7f\u900f &amp; \u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7f13\u5b58\u7a7f\u900f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bf4\u660e\uff1a\u67e5\u8be2\u4e00\u4e2a\u6570\u636e\u5e93\u4e0d\u5b58\u5728\u7684 key\uff0c\u7f13\u5b58\u6ca1\u547d\u4e2d\u5c31\u53bb\u6570\u636e\u5e93\u67e5\uff0c\u6570\u636e\u5e93\u4e5f\u6ca1\u6709\uff0c\u5bfc\u81f4\u7f13\u5b58\u548c\u6570\u636e\u5e93\u90fd\u6ca1\u6709\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u5728\u9ad8\u5e76\u53d1\u65f6\uff0c\u5982\u679c\u6ca1\u505a\u9632\u62a4\uff0c\u6076\u610f\u8bf7\u6c42\u6216\u5f02\u5e38\u8bf7\u6c42\u4f1a\u6301\u7eed\u6253\u51fb\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7f13\u5b58\u7a7a\u7ed3\u679c<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u5b58\u5728\u7684 key\uff0c\u4e5f\u7f13\u5b58\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff08\u6216\u7279\u6b8a\u6807\u8bb0\uff09\uff0c\u907f\u514d\u6570\u636e\u5e93\u91cd\u590d\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u5e26\u8fc7\u671f\u65f6\u95f4\u7684\u7f13\u5b58<\/strong>\uff0c\u5b9a\u671f\u6e05\u7406\u7a7a\u7f13\u5b58\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u7ebf\u7a0b\u9694\u79bb<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bf4\u660e\uff1a\u67d0\u4e9b\u573a\u666f\u9700\u8981\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u72ec\u7acb\u7684 Map\uff0c\u6bd4\u5982\u7528\u6237\u4e0a\u4e0b\u6587\u3001\u8bf7\u6c42\u4fe1\u606f\u5b58\u50a8\u3002<\/li>\n\n\n\n<li>\u65b9\u6848\uff1a<code>ThreadLocal&lt;Map&lt;Key, Value>><\/code>\uff0c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u4e14\u4e92\u4e0d\u5e72\u6270\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u4ee3\u7801\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Map;\nimport java.util.concurrent.ConcurrentHashMap;\n\npublic class ConcurrentCache&lt;K, V&gt; {\n\n    \/\/ \u771f\u5b9e\u7f13\u5b58\uff0c\u7ebf\u7a0b\u5b89\u5168\n    private final ConcurrentHashMap&lt;K, V&gt; cache = new ConcurrentHashMap&lt;&gt;();\n\n    \/\/ \u7528\u4e8e\u5b58\u50a8\u7a7a\u503c\u6807\u8bb0\uff0c\u907f\u514d\u7f13\u5b58\u7a7f\u900f\n    private final V NULL_VALUE;\n\n    public ConcurrentCache(V nullValue) {\n        this.NULL_VALUE = nullValue;\n    }\n\n    \/**\n     * \u901a\u8fc7\u7f13\u5b58\u83b7\u53d6\u6570\u636e\uff0c\u7f13\u5b58\u672a\u547d\u4e2d\u5219\u4ece\u6570\u636e\u6e90\u52a0\u8f7d\n     * @param key \u67e5\u8be2\u7684 key\n     * @param loader \u6570\u636e\u52a0\u8f7d\u5668\u51fd\u6570\n     * @return value \u6216 nullValue\uff08\u6807\u8bb0\u7a7a\uff09\n     *\/\n    public V get(K key, DataLoader&lt;K, V&gt; loader) {\n        V value = cache.get(key);\n        if (value != null) {\n            \/\/ \u547d\u4e2d\u7f13\u5b58\uff0c\u76f4\u63a5\u8fd4\u56de\n            if (value == NULL_VALUE) {\n                return null; \/\/ \u4ee3\u8868\u7a7a\n            }\n            return value;\n        }\n        \/\/ \u7f13\u5b58\u672a\u547d\u4e2d\uff0c\u4ece\u6570\u636e\u6e90\u52a0\u8f7d\n        V loadedValue = loader.load(key);\n        if (loadedValue == null) {\n            \/\/ \u7f13\u5b58\u7a7a\u503c\uff0c\u9632\u7a7f\u900f\n            cache.put(key, NULL_VALUE);\n            return null;\n        } else {\n            cache.put(key, loadedValue);\n            return loadedValue;\n        }\n    }\n\n    \/**\n     * \u6e05\u7406\u7f13\u5b58\n     *\/\n    public void clear() {\n        cache.clear();\n    }\n\n    \/\/ \u6570\u636e\u52a0\u8f7d\u5668\u63a5\u53e3\n    public interface DataLoader&lt;K, V&gt; {\n        V load(K key);\n    }\n}\n\n\/\/ \u7ebf\u7a0b\u9694\u79bb\u793a\u4f8b\nclass ThreadLocalContext {\n\n    private static final ThreadLocal&lt;Map&lt;String, Object&gt;&gt; threadLocalMap = ThreadLocal.withInitial(ConcurrentHashMap::new);\n\n    public static void put(String key, Object value) {\n        threadLocalMap.get().put(key, value);\n    }\n\n    public static Object get(String key) {\n        return threadLocalMap.get().get(key);\n    }\n\n    public static void clear() {\n        threadLocalMap.get().clear();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u4f7f\u7528\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Demo {\n\n    public static void main(String&#91;] args) {\n        \/\/ \u521b\u5efa\u7f13\u5b58\uff0c\u7a7a\u503c\u6807\u8bb0\u4f7f\u7528\u7279\u6b8a\u5bf9\u8c61\n        ConcurrentCache&lt;String, String&gt; cache = new ConcurrentCache&lt;&gt;((String) \"&lt;NULL&gt;\");\n\n        \/\/ \u6a21\u62df\u6570\u636e\u52a0\u8f7d\u5668\n        ConcurrentCache.DataLoader&lt;String, String&gt; loader = key -&gt; {\n            System.out.println(\"\u4ece\u6570\u636e\u5e93\u52a0\u8f7d key=\" + key);\n            if (\"exist\".equals(key)) {\n                return \"value_for_exist\";\n            }\n            return null; \/\/ \u4e0d\u5b58\u5728\n        };\n\n        System.out.println(cache.get(\"exist\", loader)); \/\/ \u8f93\u51fa value_for_exist\n        System.out.println(cache.get(\"not_exist\", loader)); \/\/ \u8f93\u51fa null\uff0c\u7f13\u5b58\u7a7a\u503c\n\n        \/\/ \u7b2c\u4e8c\u6b21\u8bbf\u95ee\uff0c\u7f13\u5b58\u751f\u6548\uff0c\u65e0\u6570\u636e\u5e93\u52a0\u8f7d\u8f93\u51fa\n        System.out.println(cache.get(\"exist\", loader)); \/\/ \u7f13\u5b58\u547d\u4e2d\n        System.out.println(cache.get(\"not_exist\", loader)); \/\/ \u7f13\u5b58\u547d\u4e2d\u7a7a\u503c\n\n        \/\/ \u7ebf\u7a0b\u9694\u79bb\u793a\u4f8b\n        ThreadLocalContext.put(\"userId\", 12345L);\n        System.out.println(ThreadLocalContext.get(\"userId\")); \/\/ \u8f93\u51fa 12345\n\n        ThreadLocalContext.clear(); \/\/ \u6e05\u7406\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u8bbe\u8ba1\u603b\u7ed3\u4e0e\u6ce8\u610f\u70b9<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8bbe\u8ba1\u70b9<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u7f13\u5b58\u7a7a\u503c\u6807\u8bb0<\/td><td>\u9632\u6b62\u7f13\u5b58\u7a7f\u900f\uff0c\u907f\u514d\u91cd\u590d\u6570\u636e\u5e93\u8bf7\u6c42<\/td><\/tr><tr><td>\u5e76\u53d1\u5b89\u5168<\/td><td>\u4f7f\u7528 <code>ConcurrentHashMap<\/code> \u4fdd\u8bc1\u9ad8\u5e76\u53d1\u5b89\u5168\u6027<\/td><\/tr><tr><td>\u7ebf\u7a0b\u9694\u79bb<\/td><td>\u4f7f\u7528 <code>ThreadLocal<\/code> \u5b9e\u73b0\u7ebf\u7a0b\u7ea7\u522b\u7684\u6570\u636e\u9694\u79bb\uff0c\u9632\u6b62\u5171\u4eab\u51b2\u7a81<\/td><\/tr><tr><td>\u7f13\u5b58\u6dd8\u6c70<\/td><td>\u5b9e\u9645\u5e94\u7528\u4e2d\u5efa\u8bae\u914d\u5408\u8fc7\u671f\u65f6\u95f4\uff08\u5982 Guava Cache \u6216 Caffeine\uff09\u5b9e\u73b0\u81ea\u52a8\u6dd8\u6c70<\/td><\/tr><tr><td>\u8d1f\u8f7d\u5747\u8861\u4e0e\u964d\u7ea7<\/td><td>\u914d\u5408\u9650\u6d41\u3001\u964d\u7ea7\u7b56\u7565\u9632\u6b62\u7cfb\u7edf\u5d29\u6e83<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u63a8\u8350\u5de5\u5177\u5e93<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5de5\u5177\u5e93<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>Guava Cache<\/td><td>\u652f\u6301\u81ea\u52a8\u52a0\u8f7d\u3001\u8fc7\u671f\u6dd8\u6c70\u3001\u7ebf\u7a0b\u5b89\u5168\u7f13\u5b58<\/td><\/tr><tr><td>Caffeine<\/td><td>\u9ad8\u6027\u80fd\u7f13\u5b58\u5e93\uff0c\u652f\u6301\u5f02\u6b65\u5237\u65b0\u3001\u6743\u91cd\u6dd8\u6c70<\/td><\/tr><tr><td>Redisson<\/td><td>\u57fa\u4e8e Redis \u7684\u5206\u5e03\u5f0f\u7f13\u5b58\u548c\u9501<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. \u76f8\u5173\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/ConcurrentHashMap.html\">Java ConcurrentHashMap \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/google\/guava\/wiki\/CachesExplained\">Guava Cache \u4f7f\u7528\u6307\u5357<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/ThreadLocal.html\">ThreadLocal \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li>\u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b \u2014 Brian Goetz<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u8282\u63d0\u4f9b\u4e00\u4e2a\u5e76\u53d1\u73af\u5883\u4e0b\u4f18\u96c5\u4f7f\u7528 Map \u7684\u5b8c\u6574\u5b9e\u6218\u6848\u4f8b\uff0c\u6db5\u76d6\u4ee5\u4e0b\u5185\u5bb9\uff1a 1. \u80cc\u666f 2. \u7f13\u5b58\u7a7f\u900f &#038;amp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4018,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,86],"tags":[],"class_list":["post-4017","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/4017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=4017"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/4017\/revisions"}],"predecessor-version":[{"id":4019,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/4017\/revisions\/4019"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/4018"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=4017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=4017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=4017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}