{"id":581,"date":"2013-11-17T11:30:58","date_gmt":"2013-11-17T03:30:58","guid":{"rendered":"http:\/\/coderbee.net\/?p=581"},"modified":"2013-12-15T16:57:34","modified_gmt":"2013-12-15T08:57:34","slug":"juc-%e5%8e%9f%e5%ad%90%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20131117\/581","title":{"rendered":"JUC \u539f\u5b50\u7c7b"},"content":{"rendered":"<h2>volatile \u53d8\u91cf<\/h2>\n<p>volatile\u53d8\u91cf\u5177\u6709\u53ef\u89c1\u6027\uff0c\u4e5f\u5c31\u662f\u8bf4\u7ebf\u7a0b\u80fd\u591f\u81ea\u52a8\u53d1\u73b0volatile \u53d8\u91cf\u7684\u6700\u65b0\u503c\uff1b\u5bf9volatile\u53d8\u91cf\u8fdb\u884c\u64cd\u4f5c\u4e0d\u4f1a\u9020\u6210\u963b\u585e\u3002<\/p>\n<p>\u9002\u7528\u4e8e\uff1a\u591a\u4e2a\u53d8\u91cf\u4e4b\u95f4\u6216\u8005\u67d0\u4e2a\u53d8\u91cf\u7684\u5f53\u524d\u503c\u4e0e\u4fee\u6539\u540e\u503c\u4e4b\u95f4\u6ca1\u6709\u7ea6\u675f\u3002<\/p>\n<p>\u6b63\u786e\u4f7f\u7528volatile\u53d8\u91cf\u7684\u6761\u4ef6\uff1a<\/p>\n<ol>\n<li>\u5bf9\u53d8\u91cf\u7684\u5199\u64cd\u4f5c\u4e0d\u4f9d\u8d56\u4e8e\u5f53\u524d\u503c\u3002<\/li>\n<li>\u8be5\u53d8\u91cf\u6ca1\u6709\u5305\u542b\u5728\u5177\u6709\u5176\u4ed6\u53d8\u91cf\u7684\u4e0d\u53d8\u5f0f\u4e2d\u3002<\/li>\n<\/ol>\n<p>\u6240\u4ee5\uff0cvolatile\u53d8\u91cf\u4e0d\u652f\u6301\u50cf<code>i++<\/code>\u8fd9\u6837\u7684\u539f\u5b50\u64cd\u4f5c\uff0c\u56e0\u4e3a\u8fd9\u6761\u8bed\u53e5\u5305\u542b\u4e86\u4e09\u4e2a\u6b65\u9aa4\uff1a\u8bfb\u53d6-\u52a01\u64cd\u4f5c-\u5199\u53d8\u91cf\u3002<br \/>\n<!--more--><\/p>\n<h2>CAS\u64cd\u4f5c<\/h2>\n<p>\u5927\u591a\u6570CPU\u90fd\u63d0\u4f9b\u4e86CAS\u64cd\u4f5c\u6307\u4ee4\uff0c\u8fd9\u6761\u6307\u4ee4\u7684\u4f5c\u7528\u662f\uff1a\u5bf9\u4e8e\u4e00\u4e2a\u53d8\u91cfvar\uff0c\u5982\u679c\u5b83\u7684\u5f53\u524d\u503c\u662fexcepted\uff0c\u5219\u628a\u5b83\u7684\u503c\u66f4\u65b0\u4e3anewValue\uff0c\u5426\u5219\u5c31\u4e0d\u66f4\u65b0\u3002<\/p>\n<p>\u7531\u4e8eJVM\u5c4f\u853d\u4e86\u64cd\u4f5c\u7cfb\u7edf\u6709\u5173\u7684\u7ec6\u8282\uff0cHotSpot\u865a\u62df\u673a\u901a\u8fc7<code>sun.misc.Unsafe<\/code>\u7c7b\u63d0\u4f9b\u4e86\u8fd9\u6837\u7684\u64cd\u4f5c\u6307\u4ee4\u3002<\/p>\n<h2>\u539f\u5b50\u7c7b<\/h2>\n<p>\u539f\u5b50\u7c7b\u662f\u5bf9volatile\u53d8\u91cf\u7684\u589e\u5f3a\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7a0d\u5fae\u590d\u6742\u7684\u7b26\u5408\u64cd\u4f5c\uff0c\u5982\u4e0a\u9762\u63d0\u5230\u7684<code>i++<\/code>\u64cd\u4f5c\u3002\u8fd9\u91cc\u4ee5<code>java.util.concurrent.atomic.AtomicInteger<\/code>\u4e3a\u4f8b\u3002<\/p>\n<h3>\u76f4\u63a5\u8bfb\u5199\u64cd\u4f5c<\/h3>\n<p>AtomicInteger\u6709\u4e00\u4e2a<code>volatile int value<\/code>\u5c5e\u6027\u8868\u793a\u5bf9\u8c61\u7684\u503c\uff0c\u8fd9\u6837\uff0c\u5bf9\u53d8\u91cf\u7684\u8bfb\u5199\u64cd\u4f5c\u5c31\u53ef\u4ee5\u501f\u52a9volatile\u8bed\u4e49\u76f4\u63a5\u5b8c\u6210\u3002<\/p>\n<pre><code class=\"java\">public final int get() {\n     return value;\n}\n\npublic final void set(int newValue) {\n     value = newValue;\n}\n<\/code><\/pre>\n<h3>CAS\u64cd\u4f5c<\/h3>\n<p>compareAndSet\u662f\u901a\u8fc7unsafe\u7c7b\u7684\u672c\u5730\u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u662f\u5b9e\u73b0\u5176\u4ed6\u590d\u5408\u64cd\u4f5c\u7684\u65b9\u6cd5\u7684\u57fa\u7840\u3002<\/p>\n<pre><code class=\"java\">public final boolean compareAndSet(int expect, int update) {\n     return unsafe.compareAndSwapInt(this, valueOffset, expect, update);\n}\n<\/code><\/pre>\n<h3>\u5bf9\u53d8\u91cf\u7684\u52a0\u51cf\u64cd\u4f5c<\/h3>\n<p>CPU\u867d\u7136\u63d0\u4f9b\u4e86CAS\u8fd9\u6837\u7684\u64cd\u4f5c\u6307\u4ee4\uff0c\u4f46\u5e76\u6ca1\u6709\u63d0\u4f9b\u7c7b\u4f3c\u8fd9\u6837\u7684\u64cd\u4f5c\uff1a\u5bf9\u53d8\u91cf\u52a0\u4e00\u4e2a\u6307\u5b9a\u7684\u503c\uff0c\u518d\u8fd4\u56de\u65e7\u503c\u3002\u4f46AtomicInteger\u7c7b\u5374\u80fd\u591f\u63d0\u4f9b\uff0c\u4e0b\u9762\u5c31\u770b\u770b\u91cc\u9762\u7684\u9b54\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">public final int getAndAdd(int delta) {\n     for (;;) {\n          int current = get(); \/\/step 1\n          int next = current + delta; \/\/step 2\n          if (compareAndSet(current, next)) \/\/step 3\n               return current;\n     }\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5f88\u7b80\u6d01\uff0c\u5728\u4e00\u4e2a\u65e0\u7a77\u7684\u5faa\u73af\u91cc\u9762\u4e0d\u65ad\u5c1d\u8bd5\u8fd9\u6837\u7684\u64cd\u4f5c\u5e8f\u5217\uff1a\u5148\u53d6\u53d8\u91cf\u7684\u5f53\u524d\u503c\uff0c\u8fdb\u884c\u52a0\u64cd\u4f5c\uff0c\u7136\u540e\u7528CAS\u64cd\u4f5c\u8bbe\u7f6e\u65b0\u7684\u503c\uff0c\u5982\u679c\u8bbe\u7f6e\u6210\u529f\u5219\u8fd4\u56de\uff0c\u5426\u5219\u7ee7\u7eed\u5c1d\u8bd5\u3002<\/p>\n<p>\u8fd9\u91cc\u4e4b\u6240\u4ee5\u8981\u653e\u5230\u4e00\u4e2a\u65e0\u7a77\u5faa\u73af\u91cc\u8fdb\u884c\u64cd\u4f5c\uff0c\u662f\u56e0\u4e3astep 1\u4e4b\u540e\uff0cstep 3\u6210\u529f\u4e4b\u524d\uff0c\u53d8\u91cf\u7684\u503c\u90fd\u6709\u53ef\u80fd\u88ab\u6539\u53d8\uff0c\u53ea\u8981\u53d8\u91cf\u7684\u503c\u88ab\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539\u4e86\uff0c\u5c31\u9700\u8981\u91cd\u8bd5\u3002\u4f46\u53ea\u8981\u7ebf\u7a0b\u4e4b\u95f4\u7684\u5199\u7ade\u4e89\u4e0d\u6fc0\u70c8\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5e8f\u5217\u5c31\u603b\u4f1a\u6210\u529f\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u539f\u5b50\u7c7b\u7684\u590d\u5408\u64cd\u4f5c\u7684\u5b9e\u73b0\u601d\u8def\u662f\uff1aCAS + \u5931\u8d25\u540e\u4e0d\u65ad\u91cd\u8bd5\u3002<\/p>\n<p>\u5176\u4ed6\u7684\u7c7b\u4f3c\u64cd\u4f5c\u4e5f\u662f\u91c7\u7528\u8fd9\u6837\u7684\u601d\u8def\u5b9e\u73b0\u7684\u3002<\/p>\n<h3>ABA\u95ee\u9898<\/h3>\n<p>\u5728\u4e0a\u9762\u7684<code>getAndAdd<\/code>\u65b9\u6cd5\u7684\u5b9e\u73b0\u91cc\uff0c\u7ebf\u7a0bT\u6267\u884cstep1\u540e\u3001\u6267\u884cstep3\u4e4b\u524d\u88ab\u6302\u8d77\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0bT2\u628a<code>value<\/code>\u5c5e\u6027\u4eceA\u4fee\u6539\u4e3aB\uff0c\u7136\u540e\u53c8\u4fee\u6539\u56deA\uff0c\u90a3\u4e48T\u6062\u590d\u6267\u884c\u65f6\uff0c\u5b83\u662f\u4e0d\u4f1a\u53d1\u73b0<code>value<\/code>\u66fe\u7ecf\u88ab\u4fee\u6539\uff0c\u7ee7\u7eed\u6267\u884cstep 3\u662f\u6210\u529f\u7684\u3002\u8fd9\u4e2a\u95ee\u9898\u5c31\u662fABA\u95ee\u9898\u3002<\/p>\n<p>ABA\u95ee\u9898\u7684\u89e3\u51b3\u601d\u8def\u662f\u7ed9\u53d8\u91cf\u7684\u503c\u5173\u8054\u4e00\u4e2a\u6233\u8bb0\uff0c\u6bcf\u6b21\u4fee\u6539\u53d8\u91cf\u7684\u503c\u65f6\u540c\u65f6\u6539\u53d8\u6233\u8bb0\uff0c\u90a3\u4e48\u5373\u4f7f\u53d8\u91cf\u7684\u503c\u4e0d\u53d8\u4e5f\u53ef\u4ee5\u53d1\u73b0\u5b83\u662f\u5426\u88ab\u4fee\u6539\u8fc7\u3002<\/p>\n<h3>AtomicBoolean<\/h3>\n<p>\u8fd9\u91cc\u8981\u7279\u522b\u8bf4\u4e0b\u7684\u662fAtomicBoolean\u7684\u5b9e\u73b0\u3002boolean\u53ea\u80fd\u8868\u793atrue\u6216false\uff0c\u53ea\u9700\u8981\u4e00\u4e2a\u6bd4\u7279\u4f4d\u5c31\u591f\u4e86\uff0c\u4f46\u6240\u6709CPU\u90fd\u4e0d\u76f4\u63a5\u8bfb\u5199\u67d0\u4e2a\u6bd4\u7279\u4f4d\uff0c\u6700\u5c0f\u7684\u4e5f\u662f\u5b57\u8282\u3002<\/p>\n<p><code>AtomicBoolean<\/code>\u91c7\u7528<code>volatile int value<\/code>\u6765\u8868\u793a\u5176\u72b6\u6001\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>volatile \u53d8\u91cf volatile\u53d8\u91cf\u5177\u6709\u53ef\u89c1\u6027\uff0c\u4e5f\u5c31\u662f\u8bf4\u7ebf\u7a0b\u80fd\u591f\u81ea\u52a8\u53d1\u73b0 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20131117\/581\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[110,111,112],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/581"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=581"}],"version-history":[{"count":1,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"predecessor-version":[{"id":582,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/581\/revisions\/582"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}