{"id":3966,"date":"2024-05-16T08:00:27","date_gmt":"2024-05-16T00:00:27","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9aconcurrenthashmap%e4%b8%ad%e7%9a%84size%e6%96%b9%e6%b3%95%e5%a6%82%e4%bd%95%e5%b7%a5%e4%bd%9c%ef%bc%9f%e5%ae%83%e7%9a%84%e7%bb%93%e6%9e%9c%e6%98%af%e5%90%a6\/"},"modified":"2024-08-02T17:26:49","modified_gmt":"2024-08-02T09:26:49","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9aconcurrenthashmap%e4%b8%ad%e7%9a%84size%e6%96%b9%e6%b3%95%e5%a6%82%e4%bd%95%e5%b7%a5%e4%bd%9c%ef%bc%9f%e5%ae%83%e7%9a%84%e7%bb%93%e6%9e%9c%e6%98%af%e5%90%a6","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9aconcurrenthashmap%e4%b8%ad%e7%9a%84size%e6%96%b9%e6%b3%95%e5%a6%82%e4%bd%95%e5%b7%a5%e4%bd%9c%ef%bc%9f%e5%ae%83%e7%9a%84%e7%bb%93%e6%9e%9c%e6%98%af%e5%90%a6\/","title":{"rendered":"\u9762\u8bd5\u5b98\uff1aConcurrentHashMap\u4e2d\u7684size()\u65b9\u6cd5\u5982\u4f55\u5de5\u4f5c\uff1f\u5b83\u7684\u7ed3\u679c\u662f\u5426\u603b\u662f\u51c6\u786e\u7684\uff1f"},"content":{"rendered":"<div class=\"wxsyncmain\">\n<section style=\"margin-bottom: 24px; margin-top: 0px;\"><strong><span style=\"font-size: 20px;\">Java 7 \u53ca\u4e4b\u524d\u7684\u7248\u672c<\/span><\/strong><\/section>\n<section style=\"text-align: justify; margin-bottom: 16px;\"><span style=\"font-size: 16px; letter-spacing: normal;\">ConcurrentHashMap \u4e2d\u7684 size()\u65b9\u6cd5\u7684\u5de5\u4f5c\u673a\u5236\u65e8\u5728\u63d0\u4f9b\u4e00\u4e2a\u5c3d\u53ef\u80fd\u51c6\u786e\u7684\u5143\u7d20\u6570\u91cf\u4f30\u8ba1\uff0c\u5373\u4f7f\u5728\u591a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u8fd9\u4e2a\u96c6\u5408\u8fdb\u884c\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u57fa\u672c\u5de5\u4f5c\u539f\u7406\uff1a<\/span><\/section>\n<ol class=\"list-paddingleft-1\" data-v-md-line=\"3\">\n<li style=\"letter-spacing: normal;\">\n<p style=\"text-align: justify;\" data-v-md-line=\"3\"><span style=\"letter-spacing: normal;\"><strong><span style=\"font-size: 16px;\">\u904d\u5386 Segment<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u5728 Java 7 \u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0cConcurrentHashMap \u662f\u57fa\u4e8e\u5206\u6bb5\u9501\uff08Segment\uff09\u5b9e\u73b0\u7684\u3002\u6bcf\u4e2a Segment \u72ec\u7acb\u5730\u7ef4\u62a4\u4e86\u4e00\u90e8\u5206\u6620\u5c04\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u6709\u81ea\u5df1\u7684\u9501\u3002\u8c03\u7528 size()\u65b9\u6cd5\u65f6\uff0c\u4f1a\u904d\u5386\u6240\u6709\u7684 Segment\u3002<\/span><\/span><\/p>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"text-align: justify; margin-top: 16px;\"><span style=\"letter-spacing: normal;\"><strong><span style=\"font-size: 16px;\">\u7d2f\u52a0\u8ba1\u6570\u5668<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u6bcf\u4e2a Segment \u7ef4\u62a4\u4e86\u4e00\u4e2a count \u53d8\u91cf\uff0c\u8868\u793a\u8be5 Segment \u7684\u5143\u7d20\u6570\u91cf\u3002size()\u65b9\u6cd5\u5c06\u901a\u8fc7\u904d\u5386\u6240\u6709 Segment\uff0c\u7d2f\u52a0\u8fd9\u4e9b count \u53d8\u91cf\u6765\u5f97\u5230\u603b\u5927\u5c0f\u3002<\/span><\/span><\/section>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"text-align: justify; margin-top: 16px;\"><span style=\"letter-spacing: normal;\"><strong><span style=\"font-size: 16px;\">\u91cd\u8bd5\u673a\u5236<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u56e0\u4e3a\u5176\u4ed6\u7ebf\u7a0b\u53ef\u80fd\u6b63\u5728\u5e76\u53d1\u4fee\u6539 ConcurrentHashMap\uff0c\u6240\u4ee5 size \u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e0d\u4e00\u81f4\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u521d\u6b21\u8ba1\u7b97\u540e\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u7ed3\u6784\u6027\u4fee\u6539\uff0c\u4f1a\u91cd\u65b0\u5c1d\u8bd5\u83b7\u53d6\u5927\u5c0f\u3002<\/span><\/span><\/section>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"text-align: justify; margin-top: 16px;\"><span style=\"letter-spacing: normal;\"><strong><span style=\"font-size: 16px;\">\u8fd4\u56de\u5927\u5c0f<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u5982\u679c\u6ca1\u6709\u68c0\u6d4b\u5230\u5e76\u53d1\u4fee\u6539\uff0c\u5219\u8fd4\u56de\u7d2f\u52a0\u540e\u7684\u5927\u5c0f\uff1b\u5982\u679c\u5728\u5c1d\u8bd5\u4e86\u82e5\u5e72\u6b21\uff08\u901a\u5e38\u662f\u4e09\u6b21\uff09\u540e\u4ecd\u7136\u68c0\u6d4b\u5230\u5e76\u53d1\u4fee\u6539\uff0c\u5219\u653e\u5f03\u5e76\u8fd4\u56de\u5f53\u524d\u7d2f\u52a0\u503c\u3002<\/span><\/span><\/section>\n<\/li>\n<\/ol>\n<p style=\"margin-top: 24px; margin-bottom: 24px;\"><strong><span style=\"font-size: 20px;\">Java 8 \u53ca\u4e4b\u540e\u7684\u7248\u672c<\/span><\/strong><\/p>\n<section style=\"margin-top: 24px; text-align: justify; margin-bottom: 16px;\"><span style=\"font-size: 16px; letter-spacing: normal;\">\u5728 Java 8 \u53ca\u4e4b\u540e\u7684\u7248\u672c\u4e2d\uff0cConcurrentHashMap \u7684\u5b9e\u73b0\u6452\u5f03\u4e86 Segment \u7684\u6982\u5ff5\uff0c\u8f6c\u800c\u4f7f\u7528\u4e86 Node \u6570\u7ec4\u52a0\u94fe\u8868\u548c\u7ea2\u9ed1\u6811\u7684\u6570\u636e\u7ed3\u6784\u3002\u8fd9\u91cc\u662f Java 8 \u4e2d ConcurrentHashMap \u7684 size()\u65b9\u6cd5\u7684\u4e00\u4e2a\u7b80\u5316\u7248\u672c\u7684\u6e90\u7801\u7247\u6bb5\uff0c\u63cf\u8ff0\u4e86\u5176\u57fa\u672c\u903b\u8f91\uff1a<\/span><\/section>\n<section class=\"code-snippet__fix code-snippet__js\">\n<div>\n<pre>public int size() {\n    long n = sumCount();\n    return ((n &lt; 0L) ? 0 : (n &gt; (long) Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) n);\n}\n\nfinal long sumCount() {\n    CounterCell[] as = counterCells;\n    CounterCell a;\n    long sum = baseCount;\n    if (as != null) {\n        for (int i = 0; i &lt; as.length; ++i) {\n            if ((a = as[i]) != null) sum += a.value;\n        }\n    }\n    return sum;\n}<\/pre>\n<\/div>\n<\/section>\n<p style=\"margin-bottom: 16px; margin-top: 0px;\"><span style=\"font-size: 16px;\">\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff1a<\/span><\/p>\n<ul class=\"list-paddingleft-1\" data-v-md-line=\"38\">\n<li style=\"letter-spacing: normal;\">\n<p style=\"text-align: justify;\"><span style=\"font-size: 16px; letter-spacing: normal;\">baseCount \u662f\u4e00\u4e2a\u957f\u6574\u578b\u53d8\u91cf\uff0c\u7528\u4e8e\u5728\u6ca1\u6709\u7ade\u4e89\u65f6\u5feb\u901f\u7edf\u8ba1\u3002<\/span><\/p>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"text-align: justify; margin-top: 16px;\"><span style=\"font-size: 16px; letter-spacing: normal;\">counterCells \u662f\u4e00\u4e2a CounterCell \u6570\u7ec4\uff0c\u6bcf\u4e2a cell \u5305\u542b\u4e00\u4e2a\u503c\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u64cd\u4f5c\u6570\u7ec4\u4e2d\u4e0d\u540c\u7684 cell \u6765\u51cf\u5c11\u4e89\u7528\u3002<\/span><\/section>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"text-align: justify; margin-top: 16px;\"><span style=\"font-size: 16px; letter-spacing: normal;\">sumCount()\u65b9\u6cd5\u904d\u5386\u6240\u6709\u7684 cells \u6765\u7d2f\u52a0\u8ba1\u6570\uff0c\u52a0\u4e0a baseCount \u4ee5\u5f97\u5230\u603b\u7684\u5143\u7d20\u6570\u3002<\/span><\/section>\n<\/li>\n<\/ul>\n<h3 style=\"margin-top: 24px; margin-bottom: 24px;\" data-v-md-heading=\"\u51c6\u786e\u6027\" data-v-md-line=\"43\"><strong><span style=\"font-size: 20px;\">\u51c6\u786e\u6027<\/span><\/strong><\/h3>\n<p style=\"text-align: justify; margin-bottom: 16px;\"><span style=\"font-size: 16px; letter-spacing: normal;\">size()\u65b9\u6cd5\u8fd4\u56de\u7684\u7ed3\u679c\u901a\u5e38\u662f\u8fd1\u4f3c\u503c\uff0c\u800c\u4e0d\u662f\u7edd\u5bf9\u51c6\u786e\u7684\uff1a<\/span><\/p>\n<ul class=\"list-paddingleft-1\" data-v-md-line=\"47\">\n<li style=\"letter-spacing: normal;\">\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 16px; letter-spacing: normal;\">\u5e76\u53d1\u66f4\u65b0<\/span><\/strong><span style=\"font-size: 16px; letter-spacing: normal;\">\uff1a\u5982\u679c\u5728\u6267\u884csize()\u65b9\u6cd5\u7684\u540c\u65f6\u6709\u5176\u4ed6\u7ebf\u7a0b\u5728\u4fee\u6539ConcurrentHashMap\uff0c\u5219\u8fd4\u56de\u7684\u5927\u5c0f\u53ef\u80fd\u4e0e\u5b9e\u9645\u5927\u5c0f\u4e0d\u7b26\u3002<\/span><\/p>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"text-align: justify; margin-top: 16px;\"><strong><span style=\"font-size: 16px; letter-spacing: normal;\">\u6700\u7ec8\u4e00\u81f4\u6027<\/span><\/strong><span style=\"font-size: 16px; letter-spacing: normal;\">\uff1a\u5c3d\u7ba1\u5b58\u5728\u5e76\u53d1\u66f4\u65b0\u95ee\u9898\uff0c\u4f46\u5728\u6ca1\u6709\u7ebf\u7a0b\u6d3b\u52a8\u4fee\u6539\u6620\u5c04\u65f6\u8c03\u7528size()\uff0c\u6216\u8005\u5728\u6240\u6709\u7ebf\u7a0b\u90fd\u770b\u5230\u6620\u5c04\u72b6\u6001\u4e00\u81f4\u65f6\u8c03\u7528size()\uff0c\u5b83\u5c06\u8fd4\u56de\u51c6\u786e\u7684\u5927\u5c0f\u3002<\/span><\/section>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"text-align: justify; margin-top: 16px;\"><strong><span style=\"font-size: 16px; letter-spacing: normal;\">\u8d8a\u754c\u68c0\u67e5<\/span><\/strong><span style=\"font-size: 16px; letter-spacing: normal;\">\uff1asize()\u65b9\u6cd5\u4f1a\u68c0\u67e5\u8ba1\u7b97\u51fa\u7684\u5927\u5c0f\uff0c\u5982\u679c\u8d85\u8fc7Integer.MAX_VALUE\uff0c\u5219\u8fd4\u56deInteger.MAX_VALUE\u3002\u8fd9\u610f\u5473\u7740\u6781\u5927\u7684ConcurrentHashMap\u5b9e\u4f8b\u7684\u5927\u5c0f\u5c06\u88ab\u622a\u65ad\u3002<\/span><\/section>\n<\/li>\n<\/ul>\n<section style=\"margin-top: 16px; text-align: justify;\"><span style=\"letter-spacing: normal;\"><strong><span style=\"font-size: 16px;\">\u603b\u7ed3<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u7531\u4e8eConcurrentHashMap\u662f\u4e3a\u9ad8\u5e76\u53d1\u8bbe\u8ba1\u7684\uff0c\u5176\u8bbe\u8ba1\u76ee\u6807\u5e76\u4e0d\u662f\u5728\u4efb\u4f55\u65f6\u523b\u90fd\u63d0\u4f9b\u5b8c\u7f8e\u51c6\u786e\u7684\u5927\u5c0f\u8ba1\u6570\uff0c\u800c\u662f\u4fdd\u8bc1\u6570\u636e\u7ed3\u6784\u7684\u5b8c\u6574\u6027\u548c\u7ebf\u7a0b\u5b89\u5168\uff0c\u5e76\u5728\u6b64\u57fa\u7840\u4e0a\u5c3d\u53ef\u80fd\u5730\u63d0\u4f9b\u6b63\u786e\u7684\u5927\u5c0f\u4fe1\u606f\u3002\u56e0\u6b64\uff0c\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0csize()\u65b9\u6cd5\u7684\u7ed3\u679c\u5e94\u8be5\u88ab<\/span><\/span><span style=\"letter-spacing: normal; font-size: 18px;\"><strong>\u89c6\u4e3a\u4e00\u4e2a\u4f30\u8ba1\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u7cbe\u786e\u503c<\/strong><\/span><span style=\"letter-spacing: normal; font-size: 16px;\">\u3002<\/span><\/section>\n<p style=\"display: none;\">\n<blockquote><p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7: \u4e92\u8054\u7f51\u9762\u8bd5\u5c0f\u5e2e\u624b<\/p><\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>Java 7 \u53ca\u4e4b\u524d\u7684\u7248\u672c ConcurrentHashMap \u4e2d\u7684 size()\u65b9\u6cd5\u7684\u5de5\u4f5c\u673a\u5236\u65e8\u5728\u63d0\u4f9b\u4e00\u4e2a\u5c3d\u53ef\u80fd\u51c6\u786e\u7684\u5143\u7d20\u6570\u91cf\u4f30 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":3963,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[30],"tags":[],"class_list":["post-3966","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-30"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144130686-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144130686-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144130686-1024x435.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144130686-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144130686-1024x435.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144130686.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-05-16","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":433,"next_post_id":3970,"next_post_title":"\u9762\u8bd5\u5b98\uff1a\u4e3a\u4ec0\u4e48equals()\u548chashCode()\u9700\u8981\u540c\u65f6\u91cd\u5199\uff1f","previous_post_id":3982,"previous_post_title":"\u9762\u8bd5\u5b98\uff1aTreeMap\u548cHashMap\u5e94\u8be5\u5982\u4f55\u9009\u62e9\uff1f","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3966"}],"version-history":[{"count":2,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3966\/revisions"}],"predecessor-version":[{"id":6481,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/3966\/revisions\/6481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/3963"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}