{"id":931,"date":"2014-05-18T16:16:47","date_gmt":"2014-05-18T08:16:47","guid":{"rendered":"http:\/\/coderbee.net\/?p=931"},"modified":"2014-05-18T19:56:32","modified_gmt":"2014-05-18T11:56:32","slug":"java8-striped64-%e5%92%8c-longadder","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20140518\/931","title":{"rendered":"Java8 Striped64 \u548c LongAdder"},"content":{"rendered":"<h3>\u6570\u636e striping<\/h3>\n<p>\u6839\u636e\u7ef4\u57fa\u767e\u79d1\u7684\u8fd9\u6bb5<a href=\"http:\/\/en.wikipedia.org\/wiki\/Data_striping\">\u8bf4\u660e<\/a>\uff1a<\/p>\n<blockquote>\n<p>In computer data storage, data striping is the technique of segmenting logically sequential data, such as a file, so that consecutive segments are stored on different physical storage devices.<br \/>\n  <br \/>\n    Striping is useful when a processing device requests data more quickly than a single storage device can provide it. By spreading segments across multiple devices which can be accessed concurrently, total data throughput is increased. It is also a useful method for balancing I\/O load across an array of disks. Striping is used across disk drives in redundant array of independent disks (RAID) storage, network interface controllers, different computers in clustered file systems and grid-oriented storage, and RAM in some systems.<\/p>\n<\/blockquote>\n<p>\u6570\u636e striping \u5c31\u662f\u628a\u903b\u8f91\u4e0a\u8fde\u7eed\u7684\u6570\u636e\u5206\u4e3a\u591a\u4e2a\u6bb5\uff0c\u4f7f\u8fd9\u4e00\u5e8f\u5217\u7684\u6bb5\u5b58\u50a8\u5728\u4e0d\u540c\u7684\u7269\u7406\u8bbe\u5907\u4e0a\u3002\u901a\u8fc7\u628a\u6bb5\u5206\u6563\u5230\u591a\u4e2a\u8bbe\u5907\u4e0a\u53ef\u4ee5\u589e\u52a0\u8bbf\u95ee\u5e76\u53d1\u6027\uff0c\u4ece\u800c\u63d0\u5347\u603b\u4f53\u7684\u541e\u5410\u91cf\u3002<\/p>\n<h2>Striped64<\/h2>\n<p>JDK 8 \u7684 <code>java.util.concurrent.atomic<\/code> \u4e0b\u6709\u4e00\u4e2a\u5305\u672c\u5730\u7684\u7c7b <code>Striped64<\/code> \uff0c\u5b83\u6301\u6709\u5e38\u89c1\u8868\u793a\u548c\u673a\u5236\u7528\u4e8e\u7c7b\u652f\u6301\u52a8\u6001 striping \u5230 64bit \u503c\u4e0a\u3002<\/p>\n<h3>\u8bbe\u8ba1\u601d\u8def<\/h3>\n<p>\u8fd9\u4e2a\u7c7b\u7ef4\u62a4\u4e00\u4e2a\u5ef6\u8fdf\u521d\u59cb\u7684\u3001\u539f\u5b50\u5730\u66f4\u65b0\u503c\u7684\u8868\uff0c\u52a0\u4e0a\u989d\u5916\u7684 \u201cbase\u201d \u5b57\u6bb5\u3002\u8868\u7684\u5927\u5c0f\u662f 2 \u7684\u5e42\u3002\u7d22\u5f15\u4f7f\u7528\u6bcf\u7ebf\u7a0b\u7684\u54c8\u5e0c\u7801\u6765masked\u3002\u8fd9\u4e2a\u7684\u51e0\u4e4e\u6240\u6709\u58f0\u660e\u90fd\u662f\u5305\u79c1\u6709\u7684\uff0c\u901a\u8fc7\u5b50\u7c7b\u76f4\u63a5\u8bbf\u95ee\u3002<\/p>\n<p>\u8868\u7684\u6761\u76ee\u662f Cell \u7c7b\uff0c\u4e00\u4e2a\u586b\u5145\u8fc7\uff08\u901a\u8fc7 <code>sun.misc.Contended<\/code> \uff09\u7684 AtomicLong  \u7684\u53d8\u4f53\uff0c\u7528\u4e8e\u51cf\u5c11\u7f13\u5b58\u7ade\u4e89\u3002\u586b\u5145\u5bf9\u4e8e\u591a\u6570 Atomics \u662f\u8fc7\u5ea6\u6740\u4f24\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e00\u822c\u4e0d\u89c4\u5219\u5730\u5206\u5e03\u5728\u5185\u5b58\u91cc\uff0c\u56e0\u6b64\u5f7c\u6b64\u95f4\u4e0d\u4f1a\u6709\u592a\u591a\u51b2\u7a81\u3002\u4f46\u5b58\u5728\u4e8e\u6570\u7ec4\u7684\u539f\u5b50\u5bf9\u8c61\u5c06\u503e\u5411\u4e8e\u5f7c\u6b64\u76f8\u90bb\u5730\u653e\u7f6e\uff0c\u56e0\u6b64\u5c06\u901a\u5e38\u5171\u4eab\u7f13\u5b58\u884c\uff08\u5bf9\u6027\u80fd\u6709\u5de8\u5927\u7684\u526f\u4f5c\u7528\uff09\uff0c\u5728\u6ca1\u6709\u8fd9\u4e2a\u9632\u5907\u4e0b\u3002<\/p>\n<p>\u90e8\u5206\u5730\uff0c\u56e0\u4e3aCell\u76f8\u5bf9\u6bd4\u8f83\u5927\uff0c\u6211\u4eec\u907f\u514d\u521b\u5efa\u5b83\u4eec\u76f4\u5230\u9700\u8981\u65f6\u3002\u5f53\u6ca1\u6709\u7ade\u4e89\u65f6\uff0c\u6240\u6709\u7684\u66f4\u65b0\u90fd\u4f5c\u7528\u5230 base \u5b57\u6bb5\u3002\u6839\u636e\u7b2c\u4e00\u6b21\u7ade\u4e89\uff08\u66f4\u65b0 base \u7684 CAS \u5931\u8d25\uff09\uff0c\u8868\u88ab\u521d\u59cb\u5316\u4e3a\u5927\u5c0f 2\u3002\u8868\u7684\u5927\u5c0f\u6839\u636e\u66f4\u591a\u7684\u7ade\u4e89\u52a0\u500d\uff0c\u76f4\u5230\u5927\u4e8e\u6216\u7b49\u4e8eCPU\u6570\u91cf\u7684\u6700\u5c0f\u7684 2 \u7684\u5e42\u3002\u8868\u7684\u69fd\u5728\u5b83\u4eec\u9700\u8981\u4e4b\u524d\u4fdd\u6301\u7a7a\u3002<\/p>\n<p>\u4e00\u4e2a\u5355\u72ec\u7684\u81ea\u65cb\u9501\uff08\u201ccellsBusy\u201d\uff09\u7528\u4e8e\u521d\u59cb\u5316\u548cresize\u8868\uff0c\u8fd8\u6709\u7528\u65b0\u7684Cell\u586b\u5145\u69fd\u3002\u4e0d\u9700\u8981\u963b\u585e\u9501\uff0c\u5f53\u9501\u4e0d\u53ef\u5f97\uff0c\u7ebf\u7a0b\u5c1d\u8bd5\u5176\u4ed6\u69fd\uff08\u6216 base\uff09\u3002\u5728\u8fd9\u4e9b\u91cd\u8bd5\u4e2d\uff0c\u4f1a\u589e\u52a0\u7ade\u4e89\u548c\u51cf\u5c11\u672c\u5730\u6027\uff0c\u8fd9\u4ecd\u7136\u597d\u4e8e\u5176\u4ed6\u9009\u62e9\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u901a\u8fc7 ThreadLocalRandom \u7ef4\u62a4\u7ebf\u7a0b\u63a2\u9488\u5b57\u6bb5\uff0c\u4f5c\u4e3a\u6bcf\u7ebf\u7a0b\u7684\u54c8\u5e0c\u7801\u3002\u6211\u4eec\u8ba9\u5b83\u4eec\u4e3a 0 \u6765\u4fdd\u6301\u672a\u521d\u59cb\u5316\u76f4\u5230\u5b83\u4eec\u5728\u69fd 0 \u7ade\u4e89\u3002\u7136\u540e\u521d\u59cb\u5316\u5b83\u4eec\u4e3a\u901a\u5e38\u4e0d\u4f1a\u4e92\u76f8\u51b2\u7a81\u7684\u503c\u3002\u5f53\u6267\u884c\u66f4\u65b0\u64cd\u4f5c\u65f6\uff0c\u7ade\u4e89\u548c\/\u6216\u8868\u51b2\u7a81\u901a\u8fc7\u5931\u8d25\u4e86\u7684 CAS \u6765\u6307\u793a\u3002\u6839\u636e\u51b2\u7a81\uff0c\u5982\u679c\u8868\u7684\u5927\u5c0f\u5c0f\u4e8e\u5bb9\u91cf\uff0c\u5b83\u7684\u5927\u5c0f\u52a0\u500d\uff0c\u9664\u975e\u6709\u4e9b\u7ebf\u7a0b\u6301\u6709\u4e86\u9501\u3002\u5982\u679c\u4e00\u4e2a\u54c8\u5e0c\u540e\u7684\u69fd\u662f\u7a7a\u7684\uff0c\u4e14\u9501\u53ef\u5f97\uff0c\u521b\u5efa\u65b0\u7684Cell\u3002\u5426\u5219\uff0c\u5982\u679c\u69fd\u5b58\u5728\uff0c\u91cd\u8bd5CAS\u3002\u91cd\u8bd5\u901a\u8fc7 \u201c\u91cd\u6563\u5217\uff0cdouble hashing\u201d \u6765\u7ee7\u7eed\uff0c\u4f7f\u7528\u4e00\u4e2a\u6b21\u8981\u7684\u54c8\u5e0c\u7b97\u6cd5\uff08Marsaglia XorShift\uff09\u6765\u5c1d\u8bd5\u627e\u5230\u4e00\u4e2a\u81ea\u7531\u69fd\u4f4d\u3002<\/p>\n<p>\u8868\u7684\u5927\u5c0f\u662f\u6709\u4e0a\u9650\u7684\uff0c\u56e0\u4e3a\uff0c\u5f53\u7ebf\u7a0b\u6570\u591a\u4e8eCPU\u6570\u65f6\uff0c\u5047\u5982\u6bcf\u4e2a\u7ebf\u7a0b\u7ed1\u5b9a\u5230\u4e00\u4e2aCPU\u4e0a\uff0c\u5b58\u5728\u4e00\u4e2a\u5b8c\u7f8e\u7684\u54c8\u5e0c\u51fd\u6570\u6620\u5c04\u7ebf\u7a0b\u5230\u69fd\u4e0a\uff0c\u6d88\u9664\u4e86\u51b2\u7a81\u3002\u5f53\u6211\u4eec\u5230\u8fbe\u5bb9\u91cf\uff0c\u6211\u4eec\u968f\u673a\u6539\u53d8\u78b0\u649e\u7ebf\u7a0b\u7684\u54c8\u5e0c\u7801\u641c\u7d22\u8fd9\u4e2a\u6620\u5c04\u3002\u56e0\u4e3a\u641c\u7d22\u662f\u968f\u673a\u7684\uff0c\u51b2\u7a81\u53ea\u80fd\u901a\u8fc7CAS\u5931\u8d25\u6765\u77e5\u9053\uff0c\u6536\u655bconvergence \u662f\u6162\u7684\uff0c\u56e0\u4e3a\u7ebf\u7a0b\u901a\u5e38\u4e0d\u4f1a\u4e00\u76f4\u7ed1\u5b9a\u5230CPU\u4e0a\uff0c\u53ef\u80fd\u6839\u672c\u4e0d\u4f1a\u53d1\u751f\u3002\u7136\u800c\uff0c\u5c3d\u7ba1\u6709\u8fd9\u4e9b\u9650\u5236\uff0c\u5728\u8fd9\u4e9b\u6848\u4f8b\u4e0b\u89c2\u5bdf\u5230\u7684\u7ade\u4e89\u9891\u7387\u663e\u8457\u5730\u4f4e\u3002<\/p>\n<p>\u5f53\u54c8\u5e0c\u5230\u7279\u5b9a Cell \u7684\u7ebf\u7a0b\u7ec8\u6b62\u540e\uff0cCell \u53ef\u80fd\u53d8\u4e3a\u7a7a\u95f2\u7684\uff0c\u8868\u52a0\u500d\u540e\u5bfc\u81f4\u6ca1\u6709\u7ebf\u7a0b\u54c8\u5e0c\u5230\u6269\u5c55\u7684 Cell \u4e5f\u4f1a\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u3002\u6211\u4eec\u4e0d\u5c1d\u8bd5\u53bb\u68c0\u6d4b\u6216\u79fb\u9664\u8fd9\u4e9b Cell\uff0c\u5728\u5b9e\u4f8b\u957f\u671f\u8fd0\u884c\u7684\u5047\u8bbe\u4e0b\uff0c\u89c2\u5bdf\u5230\u7684\u7ade\u4e89\u6c34\u5e73\u5c06\u91cd\u73b0\uff0c\u6240\u4ee5 Cell \u5c06\u6700\u7ec8\u88ab\u518d\u6b21\u9700\u8981\u3002\u5bf9\u4e8e\u77ed\u671f\u5b58\u6d3b\u7684\u5b9e\u4f8b\uff0c\u8fd9\u6ca1\u5173\u7cfb\u3002<\/p>\n<h3>\u8bbe\u8ba1\u601d\u8def\u5c0f\u7ed3<\/h3>\n<ul>\n<li>striping\u548c\u7f13\u5b58\u884c\u586b\u5145\uff1a\u901a\u8fc7\u628a\u7c7b\u6570\u636e striping \u4e3a 64bit \u7684\u7247\u6bb5\uff0c\u4f7f\u6570\u636e\u6210\u4e3a\u7f13\u5b58\u884c\u53cb\u597d\u7684\uff0c\u51cf\u5c11CAS\u7ade\u4e89\u3002<\/li>\n<li>\u5206\u89e3\u8868\u793a\uff1a\u5bf9\u4e8e\u4e00\u4e2a\u6570\u5b57 5\uff0c\u53ef\u4ee5\u5206\u89e3\u4e3a\u4e00\u5e8f\u5217\u6570\u7684\u548c\uff1a<code>2 + 3<\/code>\uff0c\u8fd9\u4e2a\u6570\u5b57\u52a0 1 \u4e5f\u7b49\u4ef7\u4e8e\u5b83\u7684\u5206\u89e3\u5e8f\u5217\u4e2d\u7684\u4efb\u4e00 \u6570\u5b57\u52a0 1\uff1a<code>5 + 1 = 2 + (3 + 1)<\/code>\u3002<\/li>\n<li>\u901a\u8fc7\u628a\u5206\u89e3\u5e8f\u5217\u5b58\u653e\u5728\u8868\u91cc\u9762\uff0c\u8868\u7684\u6761\u76ee\u90fd\u662f\u586b\u5145\u540e\u7684 Cell\uff1b\u9650\u5236\u8868\u7684\u5927\u5c0f\u4e3a 2 \u7684\u5e42\uff0c\u5219\u53ef\u4ee5\u7528\u63a9\u7801\u6765\u5b9e\u73b0\u7d22\u5f15\uff1b\u540c\u65f6\u628a\u8868\u7684\u5927\u5c0f\u9650\u5236\u4e3a\u5927\u4e8e\u7b49\u4e8eCPU\u6570\u91cf\u7684\u6700\u5c0f\u7684 2 \u7684\u5e42\u3002<\/li>\n<li>\u5f53\u8868\u7684\u6761\u76ee\u4e0a\u51fa\u73b0\u7ade\u4e89\u65f6\uff0c\u5728\u5230\u8fbe\u5bb9\u91cf\u524d\u8868\u6269\u5bb9\u4e00\u500d\uff0c\u901a\u8fc7\u589e\u52a0\u6761\u76ee\u6765\u51cf\u5c11\u7ade\u4e89\u3002<\/li>\n<\/ul>\n<h3>Cell \u7c7b<\/h3>\n<p><code>Cell<\/code> \u7c7b\u662f <code>Striped64<\/code> \u7684\u9759\u6001\u5185\u90e8\u7c7b\u3002\u901a\u8fc7\u6ce8\u89e3 <code>@sun.misc.Contended<\/code> \u6765\u81ea\u52a8\u5b9e\u73b0\u7f13\u5b58\u884c\u586b\u5145\uff0c\u8ba9Java\u7f16\u8bd1\u5668\u548cJRE\u8fd0\u884c\u65f6\u6765\u51b3\u5b9a\u5982\u4f55\u586b\u5145\u3002\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u586b\u5145\u4e86\u7684\u3001\u63d0\u4f9b\u4e86CAS\u66f4\u65b0\u7684volatile\u53d8\u91cf\u3002<\/p>\n<pre><code class=\"java\">@sun.misc.Contended static final class Cell {\n     volatile long value;\n     Cell(long x) { value = x; }\n     final boolean cas(long cmp, long val) {\n          return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val);\n     }\n\n     \/\/ Unsafe mechanics\n     private static final sun.misc.Unsafe UNSAFE;\n     private static final long valueOffset;\n     static {\n          try {\n               UNSAFE = sun.misc.Unsafe.getUnsafe();\n               Class&lt;?&gt; ak = Cell.class;\n               valueOffset = UNSAFE.objectFieldOffset\n                    (ak.getDeclaredField(\"value\"));\n          } catch (Exception e) {\n               throw new Error(e);\n          }\n     }\n}\n<\/code><\/pre>\n<h3>Striped64<\/h3>\n<p>Striped64 \u901a\u8fc7\u4e00\u4e2a Cell \u6570\u7ec4\u7ef4\u6301\u4e86\u4e00\u5e8f\u5217\u5206\u89e3\u6570\u7684\u8868\u793a\uff0c\u901a\u8fc7 base \u5b57\u6bb5\u7ef4\u6301\u6570\u7684\u521d\u59cb\u503c\uff0c\u901a\u8fc7 cellsBusy \u5b57\u6bb5\u6765\u63a7\u5236 resing \u548c\/\u6216 \u521b\u5efaCell\u3002\u5b83\u8fd8\u63d0\u4f9b\u4e86\u5bf9\u6570\u8fdb\u884c\u7d2f\u52a0\u7684\u673a\u5236\u3002<\/p>\n<pre><code class=\"java\">abstract class Striped64 extends Number {\n    static final int NCPU = Runtime.getRuntime().availableProcessors();\n\n     \/\/ \u5b58\u653e Cell \u7684\u8868\u3002\u5f53\u4e0d\u4e3a\u7a7a\u65f6\u5927\u5c0f\u662f 2 \u7684\u5e42\u3002\n    transient volatile Cell[] cells;\n\n     \/\/ base \u503c\uff0c\u5728\u6ca1\u6709\u7ade\u4e89\u65f6\u4f7f\u7528\uff0c\u4e5f\u4f5c\u4e3a\u8868\u521d\u59cb\u5316\u7ade\u4e89\u65f6\u7684\u4e00\u4e2a\u540e\u5907\u3002\n    transient volatile long base;\n\n     \/\/ \u81ea\u65cb\u9501\uff0c\u5728 resizing \u548c\/\u6216 \u521b\u5efaCell\u65f6\u4f7f\u7528\u3002\n    transient volatile int cellsBusy;\n}\n<\/code><\/pre>\n<h4>\u7d2f\u52a0\u673a\u5236 longAccumulate<\/h4>\n<p>\u8bbe\u8ba1\u601d\u8def\u91cc\u9488\u5bf9\u673a\u5236\u7684\u5b9e\u73b0\uff0c\u6838\u5fc3\u903b\u8f91\u3002\u8be5\u65b9\u6cd5\u5904\u7406\u6d89\u53ca\u521d\u59cb\u5316\u3001resing\u3001\u521b\u5efa\u65b0cell\u3001\u548c\/\u6216\u7ade\u4e89\u7684\u66f4\u65b0\u3002<\/p>\n<p>\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\n<p>if  \u8868\u5df2\u521d\u59cb\u5316<\/p>\n<blockquote>\n<ul>\n<li>if  \u6620\u5c04\u5230\u7684\u69fd\u662f\u7a7a\u7684\uff0c\u52a0\u9501\u540e\u518d\u6b21\u5224\u65ad\uff0c\u5982\u679c\u4ecd\u7136\u662f\u7a7a\u7684\uff0c\u521d\u59cb\u5316cell\u5e76\u5173\u8054\u5230\u69fd\u3002<\/li>\n<li>else if  \uff08\u69fd\u4e0d\u4e3a\u7a7a\uff09\u5728\u69fd\u4e0a\u4e4b\u524d\u7684CAS\u5df2\u7ecf\u5931\u8d25\uff0c\u91cd\u8bd5\u3002<\/li>\n<li>else if  \uff08\u69fd\u4e0d\u4e3a\u7a7a\u3001\u4e14\u4e4b\u524d\u7684CAS\u6ca1\u5931\u8d25\uff0c\uff09\u5728\u6b64\u69fd\u7684cell\u4e0a\u5c1d\u8bd5\u66f4\u65b0<\/li>\n<li>else if  \u8868\u5df2\u8fbe\u5230\u5bb9\u91cf\u4e0a\u9650\u6216\u88ab\u6269\u5bb9\u4e86\uff0c\u91cd\u8bd5\u3002<\/li>\n<li>else if  \u5982\u679c\u4e0d\u5b58\u5728\u51b2\u7a81\uff0c\u5219\u8bbe\u7f6e\u4e3a\u5b58\u5728\u51b2\u7a81\uff0c\u91cd\u8bd5\u3002<\/li>\n<li>else if  \u5982\u679c\u6210\u529f\u83b7\u53d6\u5230\u9501\uff0c\u5219\u6269\u5bb9\u3002<\/li>\n<li>else  \u91cd\u6563\u5217\uff0c\u5c1d\u8bd5\u5176\u4ed6\u69fd\u3002   <\/li>\n<\/ul>\n<\/blockquote>\n<\/li>\n<li>\n<p>else if  \u9501\u7a7a\u95f2\u4e14\u83b7\u53d6\u9501\u6210\u529f\uff0c\u521d\u59cb\u5316\u8868<\/p>\n<\/li>\n<li>else if  \u56de\u9000 base \u4e0a\u66f4\u65b0\u4e14\u6210\u529f\u5219\u9000\u51fa<\/li>\n<li>else  \u7ee7\u7eed<\/li>\n<\/ul>\n<pre><code class=\"java\">final void longAccumulate(long x, LongBinaryOperator fn,\n                                boolean wasUncontended) {\n     int h;\n     if ((h = getProbe()) == 0) {\n          \/\/ \u672a\u521d\u59cb\u5316\u7684\n          ThreadLocalRandom.current(); \/\/ \u5f3a\u5236\u521d\u59cb\u5316\n          h = getProbe();\n          wasUncontended = true;\n     }\n\n     \/\/ \u6700\u540e\u7684\u69fd\u4e0d\u4e3a\u7a7a\u5219 true\uff0c\u4e5f\u7528\u4e8e\u63a7\u5236\u6269\u5bb9\uff0cfalse\u91cd\u8bd5\u3002\n     boolean collide = false;\n     for (;;) {\n          Cell[] as; Cell a; int n; long v;\n          if ((as = cells) != null &amp;&amp; (n = as.length) &gt; 0) {\n               \/\/ \u8868\u5df2\u7ecf\u521d\u59cb\u5316\n\n               if ((a = as[(n - 1) &amp; h]) == null) {\n                    \/\/ \u7ebf\u7a0b\u6240\u6620\u5c04\u5230\u7684\u69fd\u662f\u7a7a\u7684\u3002\n\n                    if (cellsBusy == 0) {       \/\/ \u5c1d\u8bd5\u5173\u8054\u65b0\u7684Cell\n\n                         \/\/ \u9501\u672a\u88ab\u4f7f\u7528\uff0c\u4e50\u89c2\u5730\u521b\u5efa\u5e76\u521d\u59cb\u5316cell\u3002\n                         Cell r = new Cell(x);\n\n                         if (cellsBusy == 0 &amp;&amp; casCellsBusy()) {\n                              \/\/ \u9501\u4ecd\u7136\u662f\u7a7a\u95f2\u7684\u3001\u4e14\u6210\u529f\u83b7\u53d6\u5230\u9501\n\n                              boolean created = false;\n                              try {               \/\/ \u5728\u6301\u6709\u9501\u65f6\u518d\u6b21\u68c0\u67e5\u69fd\u662f\u5426\u7a7a\u95f2\u3002\n                                   Cell[] rs; int m, j;\n                                   if ((rs = cells) != null &amp;&amp;\n                                        (m = rs.length) &gt; 0 &amp;&amp;\n                                        rs[j = (m - 1) &amp; h] == null) {\n                                        \/\/ \u6240\u6620\u5c04\u7684\u69fd\u4ecd\u4e3a\u7a7a\n\n                                        rs[j] = r;          \/\/ \u5173\u8054 cell \u5230\u69fd\n                                        created = true;\n                                   }\n                              } finally {\n                                   cellsBusy = 0;     \/\/ \u91ca\u653e\u9501\n                              }\n                              if (created)\n                                   break;               \/\/ \u6210\u529f\u521b\u5efacell\u5e76\u5173\u8054\u5230\u69fd\uff0c\u9000\u51fa\n                              continue;           \/\/ \u69fd\u73b0\u5728\u4e0d\u4e3a\u7a7a\u4e86\n                         }\n                    }\n                    \/\/ \u9501\u88ab\u5360\u7528\u4e86\uff0c\u91cd\u8bd5\n                    collide = false;\n               }\n               \/\/ \u69fd\u88ab\u5360\u7528\u4e86\n\n               else if (!wasUncontended)       \/\/ \u5df2\u7ecf\u77e5\u9053 CAS \u5931\u8d25\n                    wasUncontended = true;      \/\/ \u5728\u91cd\u6563\u5217\u540e\u7ee7\u7eed\n\n               \/\/ \u5728\u5f53\u524d\u69fd\u7684cell\u4e0a\u5c1d\u8bd5\u66f4\u65b0\n               else if (a.cas(v = a.value, ((fn == null) ? v + x :\n                                                  fn.applyAsLong(v, x))))\n                    break;\n\n               \/\/ \u8868\u5927\u5c0f\u8fbe\u5230\u4e0a\u9650\u6216\u6269\u5bb9\u4e86\uff1b\n               \/\/ \u8868\u8fbe\u5230\u4e0a\u9650\u540e\u5c31\u4e0d\u4f1a\u518d\u5c1d\u8bd5\u4e0b\u9762if\u7684\u6269\u5bb9\u4e86\uff0c\u53ea\u4f1a\u91cd\u6563\u5217\uff0c\u5c1d\u8bd5\u5176\u4ed6\u69fd\n               else if (n &gt;= NCPU || cells != as)\n                    collide = false;            \/\/ At max size or stale\n\n               \/\/  \u5982\u679c\u4e0d\u5b58\u5728\u51b2\u7a81\uff0c\u5219\u8bbe\u7f6e\u4e3a\u5b58\u5728\u51b2\u7a81\n               else if (!collide)\n                    collide = true;\n\n               \/\/ \u6709\u7ade\u4e89\uff0c\u9700\u8981\u6269\u5bb9\n               else if (cellsBusy == 0 &amp;&amp; casCellsBusy()) {\n                    \/\/ \u9501\u7a7a\u95f2\u4e14\u6210\u529f\u83b7\u53d6\u5230\u9501\n                    try {\n                         if (cells == as) {      \/\/ \u8ddd\u4e0a\u4e00\u6b21\u68c0\u67e5\u540e\u8868\u6ca1\u6709\u6539\u53d8\uff0c\u6269\u5bb9\uff1a\u52a0\u500d\n                              Cell[] rs = new Cell[n &lt;&lt; 1];\n                              for (int i = 0; i &lt; n; ++i)\n                                   rs[i] = as[i];\n                              cells = rs;\n                         }\n                    } finally {\n                         cellsBusy = 0;     \/\/ \u91ca\u653e\u9501\n                    }\n                    collide = false;\n                    continue;                   \/\/ \u5728\u6269\u5bb9\u540e\u7684\u8868\u4e0a\u91cd\u8bd5\n               }\n\n               \/\/ \u6ca1\u6cd5\u83b7\u53d6\u9501\uff0c\u91cd\u6563\u5217\uff0c\u5c1d\u8bd5\u5176\u4ed6\u69fd\n               h = advanceProbe(h);\n          }\n          else if (cellsBusy == 0 &amp;&amp; cells == as &amp;&amp; casCellsBusy()) {\n               \/\/ \u52a0\u9501\u7684\u60c5\u51b5\u4e0b\u521d\u59cb\u5316\u8868\n\n               boolean init = false;\n               try {                           \/\/ Initialize table\n                    if (cells == as) {\n                         Cell[] rs = new Cell[2];\n                         rs[h &amp; 1] = new Cell(x);\n                         cells = rs;\n                         init = true;\n                    }\n               } finally {\n                    cellsBusy = 0;     \/\/ \u91ca\u653e\u9501\n               }\n               if (init)\n                    break;     \/\/ \u6210\u529f\u521d\u59cb\u5316\uff0c\u5df2\u66f4\u65b0\uff0c\u8df3\u51fa\u5faa\u73af\n          }\n          else if (casBase(v = base, ((fn == null) ? v + x :\n                                             fn.applyAsLong(v, x))))\n               \/\/ \u8868\u672a\u88ab\u521d\u59cb\u5316\uff0c\u53ef\u80fd\u6b63\u5728\u521d\u59cb\u5316\uff0c\u56de\u9000\u4f7f\u7528 base\u3002\n               break;                          \/\/ \u56de\u9000\u5230\u4f7f\u7528 base\n     }\n}\n<\/code><\/pre>\n<h2>LongAdder<\/h2>\n<p>LongAdder \u7ee7\u627f\u81ea Striped64\uff0c\u5b83\u7684\u65b9\u6cd5\u53ea\u9488\u5bf9\u7b80\u5355\u7684\u60c5\u51b5\uff1acell\u5b58\u5728\u4e14\u66f4\u65b0\u65e0\u7ade\u4e89\uff0c\u5176\u4f59\u60c5\u51b5\u90fd\u901a\u8fc7 Striped64 \u7684longAccumulate\u65b9\u6cd5\u6765\u5b8c\u6210\u3002<\/p>\n<pre><code class=\"java\">public void add(long x) {\n     Cell[] as; long b, v; int m; Cell a;\n     if ((as = cells) != null || !casBase(b = base, b + x)) {\n          \/\/ cells \u4e0d\u4e3a\u7a7a \u6216\u5728 base \u4e0acas\u5931\u8d25\u3002\u4e5f\u5373\u51fa\u73b0\u4e86\u7ade\u4e89\u3002\n          boolean uncontended = true;\n\n          \/\/\n          if (as == null || (m = as.length - 1) &lt; 0 ||\n               (a = as[getProbe() &amp; m]) == null ||\n               !(uncontended = a.cas(v = a.value, v + x)))\n               \/\/ \u5982\u679c\u6240\u6620\u5c04\u7684\u69fd\u4e0d\u4e3a\u7a7a\uff0c\u4e14\u6210\u529f\u66f4\u65b0\u5219\u8fd4\u56de\uff0c\u5426\u5219\u8fdb\u5165\u590d\u6742\u5904\u7406\u6d41\u7a0b\u3002\n               longAccumulate(x, null, uncontended);\n     }\n}\n\n\/\/ \u83b7\u53d6\u5f53\u524d\u7684\u548c\u3002base\u503c\u52a0\u4e0a\u6bcf\u4e2acell\u7684\u503c\u3002\npublic long sum() {\n     Cell[] as = cells; Cell a;\n     long sum = base;\n     if (as != null) {\n          for (int i = 0; i &lt; as.length; ++i) {\n               if ((a = as[i]) != null)\n                    sum += a.value;\n          }\n     }\n     return sum;\n}\n<\/code><\/pre>\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>\u6570\u636e striping \u6839\u636e\u7ef4\u57fa\u767e\u79d1\u7684\u8fd9\u6bb5\u8bf4\u660e\uff1a In computer dat &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20140518\/931\">\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":[91,168,167],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/931"}],"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=931"}],"version-history":[{"count":4,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/931\/revisions"}],"predecessor-version":[{"id":935,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/931\/revisions\/935"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}