{"id":3916,"date":"2019-02-25T15:39:01","date_gmt":"2019-02-25T07:39:01","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3916"},"modified":"2019-02-25T15:39:01","modified_gmt":"2019-02-25T07:39:01","slug":"java-%e4%b8%ad%e7%9a%84-immutable%e3%80%81threadsafe%e3%80%81notthreadsafe%e3%80%81guardedby-%e6%b3%a8%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3916","title":{"rendered":"Java \u4e2d\u7684 @Immutable\u3001@ThreadSafe\u3001@NotThreadSafe\u3001@GuardedBy \u6ce8\u89e3"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\uff0c\u6211\u4eec\u7684\u7535\u5546\u9879\u76ee\u4e2d\u9700\u8981\u4e00\u4e2a\u9650\u6d41\u529f\u80fd\uff0c\u6211\u6253\u7b97\u4f7f\u7528\u4ee4\u724c\u6876\u5b9e\u73b0\u3002\n<\/p>\n<p>\n\t\u7136\u540e\uff0c\u6211\u5c31\u7f51\u4e0a\u67e5\u4e86\u4e00\u4e9b\u5173\u4e8e\u4ee4\u724c\u6876\u7b97\u6cd5\u7684\u6587\u7ae0\uff0c\u7528 Java \u5b9e\u73b0\u4e86\u4e00\u4e2a\u3002\u4f46\u662f\u5176\u4e2d\u6709\u4e00\u70b9\u95ee\u9898\uff0c\u6240\u4ee5\uff0c\u6211\u5c31\u60f3\u53c2\u8003\u4e00\u4e0b guava \u4e2d RateLimiter \u7684\u5b9e\u73b0\u3002\u4f46\u662f\u53d1\u73b0 com.google.common.util.concurrent.RateLimiter \u7c7b\u4e0a\u6709\u4e00\u4e2a @ThreadSafe \u6ce8\u89e3\u3002\u6240\u4ee5\uff0c\u672c\u6587\u6211\u4eec\u5148\u641e\u6e05\u695a @ThreadSafe \u7684\u4f5c\u7528\uff01\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"@Immutable\u3001@ThreadSafe\u3001@NotThreadSafe\u3001@GuardedBy\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g0ipijbemaj20f108lmxt.jpg\" style=\"width: 541px;height: 309px\" \/>\n<\/p>\n<p>\n\t\u6211\u767e\u5ea6\u8c37\u6b4c\u4e86\u5f88\u4e45\uff0c\u90fd\u6ca1\u6709\u5f88\u597d\u7684\u89e3\u91ca\u3002\n<\/p>\n<p>\n\t\u76ee\u524d\u6211\u6240\u77e5\u9053\u7684\u662f\uff0c@Immutable\u3001@ThreadSafe\u3001@NotThreadSafe\u3001@GuardedBy \u90fd\u4e0d\u662f JDK \u7684\u6807\u51c6\u6ce8\u91ca\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u9700\u8981\u5bfc\u5165\u989d\u5916\u7684\u4f9d\u8d56\u9879\uff1ajcip-annotations.jar\u3002\n<\/p>\n<ul>\n<li>\n\t\t@ThreadSafe \u662f\u8868\u793a\u8fd9\u4e2a\u7c7b\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5177\u4f53\u662f\u5426\u771f\u5b89\u5168\uff0c\u90a3\u8981\u770b\u5b9e\u73b0\u8005\u600e\u4e48\u5b9e\u73b0\u7684\u4e86\uff0c\u53cd\u6b63\u6253\u4e0a\u8fd9\u4e2a\u6807\u7b7e\u53ea\u662f\u8868\u793a\u4e00\u4e0b\u3002\u4e0d\u7ebf\u7a0b\u5b89\u5168\u7684\u7c7b\u6253\u4e0a\u8fd9\u4e2a\u6ce8\u89e3\u4e5f\u6ca1\u4e8b\u513f\u3002\n\t<\/li>\n<li>\n\t\t@Immutable \u8868\u793a\uff0c\u7c7b\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5305\u542b\u4e86\u3000@ThreadSafe\u3000\u7684\u610f\u601d\u3002\n\t<\/li>\n<li>\n\t\t@NotThreadSafe \u8868\u793a\u8fd9\u4e2a\u7c7b\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5982\u679c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u975e\u8981\u6253\u4e0a\u8fd9\u4e2a\u6ce8\u89e3\uff0c\u90a3\u4e5f\u4e0d\u4f1a\u62a5\u9519\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u8fd9\u4e09\u4e2a\u6ce8\u89e3\uff0c\u5bf9\u7528\u6237\u548c\u7ef4\u62a4\u8005\u662f\u6709\u76ca\u7684\uff0c\u7528\u6237\u53ef\u4ee5\u7acb\u5373\u770b\u51fa\u6765\u8fd9\u4e2a\u7c7b\u662f\u5426\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u7ef4\u62a4\u8005\u5219\u662f\u53ef\u4ee5\u6839\u636e\u8fd9\u4e2a\u6ce8\u89e3\uff0c\u91cd\u70b9\u68c0\u67e5\u7ebf\u7a0b\u5b89\u5168\u65b9\u9762\u3002\u53e6\u5916\uff0c\u4ee3\u7801\u5206\u6790\u5de5\u5177\u53ef\u80fd\u4f1a\u5229\u7528\u8fd9\u4e2a\u6ce8\u89e3\u3002\n<\/p>\n<p>\n\t\u57df\u6ce8\u89e3\u548c\u65b9\u6cd5\u6ce8\u89e3\u90fd\u662f\u7528@GuardedBy( lock )\u6765\u6807\u8bc6\u3002\u91cc\u9762\u7684Lock\u662f\u544a\u8bc9\u7ef4\u62a4\u8005\uff1a\u8fd9\u4e2a\u72b6\u6001\u53d8\u91cf\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u88ab\u54ea\u4e2a\u9501\u4fdd\u62a4\u7740\u3002\u8fd9\u6837\u53ef\u4ee5\u5f3a\u70c8\u7684\u63d0\u793a\u7c7b\u7684\u7ef4\u62a4\u8005\u6ce8\u610f\u8fd9\u91cc\u3002\n<\/p>\n<ul>\n<li>\n\t\t@GuardedBy( &quot;this&quot; ) \u53d7\u5bf9\u8c61\u5185\u90e8\u9501\u4fdd\u62a4\n\t<\/li>\n<li>\n\t\t@GuardedBy( &quot;fieldName&quot; ) \u53d7 \u4e0efieldName\u5f15\u7528\u76f8\u5173\u8054\u7684\u9501 \u4fdd\u62a4\u3002\n\t<\/li>\n<li>\n\t\t@GuardedBy( &quot;ClassName.fieldName&quot; ) \u53d7 \u4e00\u4e2a\u7c7b\u7684\u9759\u6001field\u7684\u9501 \u4fdd\u5b58\u3002\n\t<\/li>\n<li>\n\t\t@GuardedBy( &quot;methodName()&quot; ) \u9501\u5bf9\u8c61\u662f methodName() \u65b9\u6cd5\u7684\u8fd4\u503c\uff0c\u53d7\u8fd9\u4e2a\u9501\u4fdd\u62a4\u3002\n\t<\/li>\n<li>\n\t\t@GuardedBy( &quot;ClassName.class&quot; ) \u53d7 ClassName\u7c7b\u7684\u76f4\u63a5\u9501\u5bf9\u8c61\u4fdd\u62a4\u3002\u800c\u4e0d\u662f\u8fd9\u4e2a\u7c7b\u7684\u67d0\u4e2a\u5b9e\u4f8b\u7684\u9501\u5bf9\u8c61\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/stackoverflow.com\/questions\/11362298\/guardedby-threadsafe-notthreadsafe\">https:\/\/stackoverflow.com\/questions\/11362298\/guardedby-threadsafe-notthreadsafe<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/hc.apache.org\/httpcomponents-core-ga\/httpcore\/apidocs\/org\/apache\/http\/annotation\/NotThreadSafe.html\">https:\/\/hc.apache.org\/httpcomponents-core-ga\/httpcore\/apidocs\/org\/apache\/http\/annotation\/NotThreadSafe.html<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/hc.apache.org\/httpcomponents-core-ga\/httpcore\/apidocs\/org\/apache\/http\/annotation\/ThreadSafe.html\">https:\/\/hc.apache.org\/httpcomponents-core-ga\/httpcore\/apidocs\/org\/apache\/http\/annotation\/ThreadSafe.html<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/hc.apache.org\/httpcomponents-core-ga\/httpcore\/apidocs\/org\/apache\/http\/annotation\/Immutable.html\">https:\/\/hc.apache.org\/httpcomponents-core-ga\/httpcore\/apidocs\/org\/apache\/http\/annotation\/Immutable.html<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/hc.apache.org\/httpcomponents-core-ga\/httpcore\/apidocs\/org\/apache\/http\/annotation\/GuardedBy.html\">https:\/\/hc.apache.org\/httpcomponents-core-ga\/httpcore\/apidocs\/org\/apache\/http\/annotation\/GuardedBy.html<\/a>\n\t<\/li>\n<\/ul>\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=3916\">Java \u4e2d\u7684 @Immutable\u3001@ThreadSafe\u3001@NotThreadSafe\u3001@GuardedBy \u6ce8\u89e3<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\uff0c\u6211\u4eec\u7684\u7535\u5546\u9879\u76ee\u4e2d\u9700\u8981\u4e00\u4e2a\u9650\u6d41\u529f\u80fd\uff0c\u6211\u6253\u7b97\u4f7f\u7528\u4ee4\u724c\u6876\u5b9e\u73b0\u3002 \u7136\u540e\uff0c\u6211\u5c31\u7f51\u4e0a\u67e5\u4e86\u4e00\u4e9b\u5173\u4e8e\u4ee4\u724c\u6876\u7b97\u6cd5\u7684\u6587\u7ae0\uff0c\u7528 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[202,1098,531,946,1931],"class_list":["post-3916","post","type-post","status-publish","format-standard","hentry","category-java","tag-locked","tag-1098","tag-531","tag-946","tag-1931"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3916","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=3916"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3916\/revisions"}],"predecessor-version":[{"id":3917,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3916\/revisions\/3917"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}