{"id":137662,"date":"2020-07-15T18:29:52","date_gmt":"2020-07-15T10:29:52","guid":{"rendered":"http:\/\/4563.org\/?p=137662"},"modified":"2020-07-15T18:29:52","modified_gmt":"2020-07-15T10:29:52","slug":"%e6%84%9f%e8%a7%89-lambda-%e5%8a%a0%e4%b8%8a%e9%80%92%e5%bd%92%e8%b0%83%e7%94%a8%ef%bc%8c%e5%b0%b1%e6%9c%89%e7%82%b9%e6%90%9e%e4%b8%8d%e6%87%82%e8%bf%90%e8%a1%8c%e8%bf%87%e7%a8%8b%e4%ba%86","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=137662","title":{"rendered":"\u611f\u89c9 Lambda \u52a0\u4e0a\u9012\u5f52\u8c03\u7528\uff0c\u5c31\u6709\u70b9\u641e\u4e0d\u61c2\u8fd0\u884c\u8fc7\u7a0b\u4e86"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u611f\u89c9 Lambda \u52a0\u4e0a\u9012\u5f52\u8c03\u7528\uff0c\u5c31\u6709\u70b9\u641e\u4e0d\u61c2\u8fd0\u884c\u8fc7\u7a0b\u4e86               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>https:\/\/bugs.openjdk.java.net\/browse\/JDK-8240162<\/p>\n<p>\u8fd9\u4e2a ConcurrentHashMap \u7684 bug\uff0c\u8fd0\u884c\u5b83\u7684\u7528\u4f8b\uff0c\u4f1a\u4ea7\u751f\u4e00\u4e2a bug\uff0c\u5c31\u662f\u660e\u660e map \u4e2d\u6709\u6620\u5c04\uff0c\u4f46 size \u4e3a 0 \u3002<\/p>\n<pre><code>import java.util.concurrent.*;  public class test5 {     public static void main(String[] args) {          final ConcurrentHashMap&lt;String, String&gt; map = new ConcurrentHashMap&lt;&gt;();          final String key = \"key\";         final String value = \"value\";          map.put(\"anotherKey\", \"anotherValue\");         map.put(key, value);          System.out.println(\"size=\" + map.size());          map.computeIfPresent(key, (aKey, aValue) -&gt; {             map.computeIfPresent(key, (bKey, bValue) -&gt; null);             return null;         });          System.out.println(\"size=\" + map.size());\/\/\u8fd9\u53e5\u8fd4\u56de 0         map.put(key, value);\/\/\u8fd9\u53e5\u6253\u65ad\u70b9         System.out.println(\"size=\" + map.size());     } } <\/code><\/pre>\n<p> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/07\/20200723_5f19822b376b1.png\" alt=\"\u611f\u89c9 Lambda \u52a0\u4e0a\u9012\u5f52\u8c03\u7528\uff0c\u5c31\u6709\u70b9\u641e\u4e0d\u61c2\u8fd0\u884c\u8fc7\u7a0b\u4e86\" \/> <\/p>\n<p>\u53ef\u4ee5\u53d1\u73b0\u660e\u660e map \u4e2d\u6709\u6620\u5c04\uff0c\u4f46 size \u4e3a 0 \u3002<\/p>\n<ol>\n<li>\u4e3a\u4ec0\u4e48\u6ca1\u6709\u4ea7\u751f\u65e0\u9650\u9012\u5f52\uff0c\u770b\u8d77\u6765\u4f1a\u65e0\u9650\u9012\u5f52\u554a\uff1f\u611f\u89c9\u8c03\u7528\u8fc7\u7a0b\u597d\u96be\u7406\u89e3<\/li>\n<li>\u4e3a\u5565\u6267\u884c\u5b8c\uff0csize \u8fd4\u56de\u9519\u8bef\u7684\u503c\uff1f<\/li>\n<\/ol>\n<p>\u8fd0\u884c\u73af\u5883 jdk8<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2590784\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u597d\u5427\uff0c\u770b\u9519\u4e86\uff0c\u4e0d\u4f1a\u4ea7\u751f\u65e0\u9650\u9012\u5f52\uff0c\u4e0d\u8fc7\u548b\u4e00\u770b\u8fd9\u4e2a\u8fc7\u7a0b\u786e\u5b9e\u6709\u70b9\u641e\u4e0d\u6e05\u695a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2590785\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 312ybj <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u53ef\u4ee5\u5c1d\u8bd5\u6539\u53d8 debug \u7684\u7c7b\u578b\uff0c\u53f3\u51fb\u4e00\u4e0b\u77ed\u70b9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2590786\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yanshenxian <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ConcurrentHashMap \u7684 size \u65b9\u6cd5\u672c\u6765\u5c31\u662f\u4e0d\u7cbe\u786e\u7684<br \/>\u8fd9\u4e2a\u4f8b\u5b50\u4e5f\u6ca1\u6709\u9012\u5f52\u64cd\u4f5c\u554a computeIfPresent \u91cc\u7684 lambda \u8fd4\u56de\u7a7a\u76f8\u5f53\u4e8e remove \u8fd9\u4e2a key \u4e86\uff0c\u5185\u90e8\u64cd\u4f5c count -1<br \/>\u8fd9\u5c31\u76f8\u5f53\u4e8e\u64cd\u4f5c\u4e86\u4e24\u6b21 -1 \u6700\u540e\u8ba1\u7b97\u7684 size \u5c31\u662f 0<\/p>\n<p>\u5982\u679c\u6362\u6210 HashMap \u8fd9\u91cc\u5c31\u4f1a\u629b\u51fa ConcurrentModificationException                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2590787\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : no1xsyzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u6709\u54ea\u4e2a\u51fd\u6570\u8c03\u7528\u4e86\u81ea\u8eab\uff0c\u53ea\u662f\u5b58\u5728\u67d0\u5904\u56de\u8c03\u4e2d\u518d\u6b21\u8c03\u7528\u540c\u6837\u7684\u5916\u90e8\u51fd\u6570\uff0c\u4f60\u53ef\u80fd\u628a\u5b83\u8bef\u770b\u4f5c\u662f\u7684\u9012\u5f52\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2590788\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @no1xsyzy <br \/>\u662f\u7684\uff0c\u5e76\u4e0d\u662f\u9012\u5f52                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u611f\u89c9 Lambda \u52a0\u4e0a\u9012\u5f52\u8c03\u7528\uff0c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/137662"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=137662"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/137662\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=137662"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=137662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}