
	

	 



{"id":2501,"date":"2013-02-21T19:58:21","date_gmt":"2013-02-21T11:58:21","guid":{"rendered":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/?p=2501"},"modified":"2013-02-21T21:11:11","modified_gmt":"2013-02-21T13:11:11","slug":"aqs-3","status":"publish","type":"post","link":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/aqs-3\/","title":{"rendered":"The j.u.c Synchronizer Framework\u7ffb\u8bd1(\u4e09)\u4f7f\u7528\u3001\u6027\u80fd\u4e0e\u603b\u7ed3"},"content":{"rendered":"<p><a title=\"\u539f\u6587\u94fe\u63a5\" href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/gee.cs.oswego.edu\/dl\/papers\/aqs.pdf\" target=\"_blank\">\u539f\u6587\u94fe\u63a5<\/a> \u4f5c\u8005\uff1aDoug Lea \u8bd1\u8005\uff1a<a href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/www.weibo.com\/onionou\" target=\"_blank\">\u6b27\u632f\u806a<\/a> \u6821\u5bf9\uff1a\u4e01\u4e00<\/p>\n<h3 id=\"usage\">4 \u7528\u6cd5<\/h3>\n<p>AQS\u7c7b\u5c06\u4e0a\u8ff0\u7684\u529f\u80fd\u7ed3\u5408\u5230\u4e00\u8d77\uff0c\u5e76\u4e14\u4f5c\u4e3a\u4e00\u79cd\u57fa\u4e8e\u201c\u6a21\u7248\u65b9\u6cd5\u6a21\u5f0f\u201d<sup><a style=\"text-decoration: none;\" href=\"#r6\">[6]<\/a><\/sup>\u7684\u57fa\u7c7b\u63d0\u4f9b\u7ed9\u540c\u6b65\u5668\u3002\u5b50\u7c7b\u53ea\u9700\u5b9a\u4e49\u72b6\u6001\u7684\u68c0\u67e5\u4e0e\u66f4\u65b0\u76f8\u5173\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u63a7\u5236\u7740acquire\u548c release\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5c06AQS\u7684\u5b50\u7c7b\u4f5c\u4e3a\u540c\u6b65\u5668ADT\u5e76\u4e0d\u9002\u5408\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u7c7b\u5fc5\u987b\u63d0\u4f9b\u65b9\u6cd5\u5728\u5185\u90e8\u63a7\u5236acquire\u548crelease\u7684\u89c4\u5219\uff0c\u8fd9\u4e9b\u90fd\u4e0d\u5e94\u8be5\u88ab\u7528\u6237\u6240\u770b\u5230\u3002\u6240\u6709java.util.concurrent\u5305\u4e2d\u7684\u540c\u6b65\u5668\u7c7b\u90fd\u58f0\u660e\u4e86\u4e00\u4e2a\u79c1\u6709\u7684\u7ee7\u627f\u4e86<code>AbstractQueuedSynchronizer<\/code>\u7684\u5185\u90e8\u7c7b\uff0c\u5e76\u4e14\u628a\u6240\u6709\u540c\u6b65\u65b9\u6cd5\u90fd\u59d4\u6258\u7ed9\u8fd9\u4e2a\u5185\u90e8\u7c7b\u3002\u8fd9\u6837\u5404\u4e2a\u540c\u6b65\u5668\u7c7b\u7684\u516c\u5f00\u65b9\u6cd5\u5c31\u53ef\u4ee5\u4f7f\u7528\u9002\u5408\u81ea\u5df1\u7684\u540d\u79f0\u3002<!--more--><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u6700\u7b80\u5355\u7684<code>Mutex<\/code>\u7c7b\u7684\u5b9e\u73b0\uff0c\u5b83\u4f7f\u7528\u540c\u6b65\u72b6\u60010\u8868\u793a\u89e3\u9501\uff0c1\u8868\u793a\u9501\u5b9a\u3002\u8fd9\u4e2a\u7c7b\u5e76\u4e0d\u9700\u8981\u540c\u6b65\u65b9\u6cd5\u4e2d\u7684\u53c2\u6570\uff0c\u56e0\u6b64\u8fd9\u91cc\u5728\u8c03\u7528\u7684\u65f6\u5019\u4f7f\u75280\u4f5c\u4e3a\u5b9e\u53c2\uff0c\u65b9\u6cd5\u5b9e\u73b0\u91cc\u5c06\u5176\u5ffd\u7565\u3002<\/p>\n<p>[code lang=&#8221;java&#8221;]<br \/>\nclass Mutex {<br \/>\n\tclass Sync extends AbstractQueuedSynchronizer {<br \/>\n\t\tpublic boolean tryAcquire(int ignore) {<br \/>\n\t\t\treturn compareAndSetState(0, 1);<br \/>\n\t\t}<br \/>\n\t\tpublic boolean tryRelease(int ignore) {<br \/>\n\t\t\tsetState(0); return true;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tprivate final Sync sync = new Sync();<br \/>\n\tpublic void lock() { sync.acquire(0); }<br \/>\n\tpublic void unlock() { sync.release(0); }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u7684\u4e00\u4e2a\u66f4\u5b8c\u6574\u7684\u7248\u672c\uff0c\u4ee5\u53ca\u5176\u5b83\u7528\u6cd5\u6307\u5357\uff0c\u53ef\u4ee5\u5728J2SE\u7684\u6587\u6863\u4e2d\u627e\u5230\u3002\u8fd8\u53ef\u4ee5\u6709\u4e00\u4e9b\u53d8\u4f53\u3002\u5982\uff0ctryAcquire\u53ef\u4ee5\u4f7f\u7528\u4e00\u79cd\u201ctest-and-test-and-set\u201d\u7b56\u7565\uff0c\u5373\u5728\u6539\u53d8\u72b6\u6001\u503c\u524d\u5148\u5bf9\u72b6\u6001\u8fdb\u884c\u6821\u9a8c\u3002<\/p>\n<p>\u4ee4\u4eba\u8be7\u5f02\u7684\u662f\uff0c\u50cf\u4e92\u65a5\u9501\u8fd9\u6837\u6027\u80fd\u654f\u611f\u7684\u4e1c\u897f\u4e5f\u6253\u7b97\u901a\u8fc7\u59d4\u6258\u548c\u865a\u65b9\u6cd5\u7ed3\u5408\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u3002\u7136\u800c\uff0c\u8fd9\u6b63\u662f\u73b0\u4ee3\u52a8\u6001\u7f16\u8bd1\u5668\u4e00\u76f4\u5728\u91cd\u70b9\u7814\u7a76\u7684\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u7ed3\u6784\u3002\u7f16\u8bd1\u5668\u64c5\u957f\u5c06\u8fd9\u65b9\u9762\u7684\u5f00\u9500\u4f18\u5316\u6389\uff0c\u8d77\u7801\u4f1a\u4f18\u5316\u9891\u7e41\u8c03\u7528\u540c\u6b65\u5668\u7684\u90a3\u4e9b\u4ee3\u7801\u3002<\/p>\n<p><code>AbstractQueuedSynchronizer<\/code>\u7c7b\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u7528\u6765\u534f\u52a9\u7b56\u7565\u63a7\u5236\u3002\u4f8b\u5982\uff0c\u57fa\u7840\u7684acquire\u65b9\u6cd5\u6709\u53ef\u8d85\u65f6\u548c\u53ef\u4e2d\u65ad\u7684\u7248\u672c\u3002\u867d\u7136\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7684\u8ba8\u8bba\u90fd\u96c6\u4e2d\u5728\u50cf\u9501\u8fd9\u6837\u7684\u72ec\u5360\u6a21\u5f0f\u7684\u540c\u6b65\u5668\u4e0a\uff0c\u4f46<code>AbstractQueuedSynchronizer<\/code>\u7c7b\u4e5f\u5305\u542b\u53e6\u4e00\u7ec4\u65b9\u6cd5\uff08\u5982<code>acquireShared<\/code>\uff09\uff0c\u5b83\u4eec\u7684\u4e0d\u540c\u70b9\u5728\u4e8e<code>tryAcquireShared<\/code>\u548c<code>tryReleaseShared<\/code>\u65b9\u6cd5\u80fd\u591f\u544a\u77e5\u6846\u67b6\uff08\u901a\u8fc7\u5b83\u4eec\u7684\u8fd4\u56de\u503c\uff09\u5c1a\u80fd\u63a5\u53d7\u66f4\u591a\u7684\u8bf7\u6c42\uff0c\u6700\u7ec8\u6846\u67b6\u4f1a\u901a\u8fc7\u7ea7\u8054\u7684signal(cascading signals)\u5524\u9192\u591a\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p>\u867d\u7136\u5c06\u540c\u6b65\u5668\u5e8f\u5217\u5316\uff08\u6301\u4e45\u5316\u5b58\u50a8\u6216\u4f20\u8f93\uff09\u4e00\u822c\u6765\u8bf4\u6ca1\u6709\u592a\u5927\u610f\u4e49\uff0c\u4f46\u8fd9\u4e9b\u7c7b\u7ecf\u5e38\u4f1a\u88ab\u7528\u4e8e\u6784\u9020\u5176\u5b83\u7c7b\uff0c\u4f8b\u5982\u7ebf\u7a0b\u5b89\u5168\u7684\u96c6\u5408\uff0c\u800c\u8fd9\u4e9b\u96c6\u5408\u901a\u5e38\u662f\u53ef\u5e8f\u5217\u5316\u7684\u3002<code>AbstractQueuedSynchronizer<\/code>\u548c<code>ConditionObject<\/code>\u7c7b\u90fd\u63d0\u4f9b\u4e86\u65b9\u6cd5\u7528\u4e8e\u5e8f\u5217\u5316\u540c\u6b65\u72b6\u6001\uff0c\u4f46\u4e0d\u4f1a\u5e8f\u5217\u5316\u6f5c\u5728\u7684\u88ab\u963b\u585e\u7684\u7ebf\u7a0b\uff0c\u4e5f\u4e0d\u4f1a\u5e8f\u5217\u5316\u5176\u5b83\u5185\u90e8\u6682\u65f6\u6027\u7684\u7c3f\u8bb0\uff08bookkeeping\uff09\u53d8\u91cf\u3002\u5373\u4f7f\u5982\u6b64\uff0c\u5728\u53cd\u5e8f\u5217\u5316\u65f6\uff0c\u5927\u90e8\u5206\u540c\u6b65\u5668\u7c7b\u4e5f\u53ea\u4ec5\u5c06\u540c\u6b65\u72b6\u6001\u91cd\u7f6e\u4e3a\u521d\u59cb\u503c\uff0c\u8fd9\u4e0e\u5185\u7f6e\u9501\u7684\u9690\u5f0f\u7b56\u7565\u4e00\u81f4 \u2014\u2014 \u603b\u662f\u53cd\u5e8f\u5217\u5316\u5230\u4e00\u4e2a\u89e3\u9501\u72b6\u6001\u3002\u8fd9\u76f8\u5f53\u4e8e\u4e00\u4e2a\u7a7a\u64cd\u4f5c\uff0c\u4f46\u4ecd\u5fc5\u987b\u663e\u5f0f\u5730\u652f\u6301\u4ee5\u4fbf<code>final<\/code>\u5b57\u6bb5\u80fd\u591f\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<h4 id=\"ctrlfairness\">4.1 \u516c\u5e73\u8c03\u5ea6\u7684\u63a7\u5236<\/h4>\n<p>\u5c3d\u7ba1\u540c\u6b65\u5668\u662f\u57fa\u4e8eFIFO\u961f\u5217\u7684\uff0c\u4f46\u5b83\u4eec\u5e76\u4e0d\u4e00\u5b9a\u5c31\u5f97\u662f\u516c\u5e73\u7684\u3002\u53ef\u4ee5\u6ce8\u610f\u5230\uff0c\u5728\u57fa\u7840\u7684acquire\u7b97\u6cd5\uff083.3\u8282\uff09\u4e2d\uff0c<code>tryAcquire<\/code>\u662f\u5728\u5165\u961f\u524d\u88ab\u6267\u884c\u7684\u3002\u56e0\u6b64\u4e00\u4e2a\u65b0\u7684acquire\u7ebf\u7a0b\u80fd\u591f\u201c\u7a83\u53d6\u201d\u672c\u8be5\u5c5e\u4e8e\u961f\u5217\u5934\u90e8\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u901a\u8fc7\u540c\u6b65\u5668\u7684\u673a\u4f1a\u3002<\/p>\n<p>\u53ef<i>\u95ef\u5165<\/i>\u7684FIFO\u7b56\u7565\u901a\u5e38\u4f1a\u63d0\u4f9b\u6bd4\u5176\u5b83\u6280\u672f\u66f4\u9ad8\u7684\u603b\u541e\u5410\u7387\u3002\u5f53\u4e00\u4e2a\u6709\u7ade\u4e89\u7684\u9501\u5df2\u7ecf\u7a7a\u95f2\uff0c\u800c\u4e0b\u4e00\u4e2a\u51c6\u5907\u83b7\u53d6\u9501\u7684\u7ebf\u7a0b\u53c8\u6b63\u5728\u89e3\u9664\u963b\u585e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u65f6\u5c31\u6ca1\u6709\u7ebf\u7a0b\u53ef\u4ee5\u83b7\u53d6\u5230\u8fd9\u4e2a\u9501\uff0c\u5982\u679c\u4f7f\u7528<i>\u95ef\u5165<\/i>\u7b56\u7565\uff0c\u5219\u53ef\u51cf\u5c11\u8fd9\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694\u3002\u4e0e\u6b64\u540c\u65f6\uff0c\u8fd9\u79cd\u7b56\u7565\u8fd8\u53ef\u907f\u514d\u8fc7\u5206\u7684\uff0c\u65e0\u6548\u7387\u7684\u7ade\u4e89\uff0c\u8fd9\u79cd\u7ade\u4e89\u662f\u7531\u4e8e\u53ea\u5141\u8bb8\u4e00\u4e2a\uff08\u7b2c\u4e00\u4e2a\uff09\u6392\u961f\u7684\u7ebf\u7a0b\u88ab\u5524\u9192\u7136\u540e\u5c1d\u8bd5acquire\u64cd\u4f5c\u5bfc\u81f4\u7684\u3002\u5728\u53ea\u8981\u6c42\u77ed\u65f6\u95f4\u6301\u6709\u540c\u6b65\u5668\u7684\u573a\u666f\u4e2d\uff0c\u521b\u5efa\u540c\u6b65\u5668\u7684\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49<code>tryAcquire<\/code>\u5728\u63a7\u5236\u6743\u8fd4\u56de\u4e4b\u524d\u91cd\u590d\u8c03\u7528\u81ea\u5df1\u82e5\u5e72\u6b21\uff0c\u6765\u8fdb\u4e00\u6b65\u51f8\u663e<i>\u95ef\u5165<\/i>\u7684\u6548\u679c\u3002<\/p>\n<p><a href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/01\/fifo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2563\" alt=\"\" src=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/01\/fifo-300x90.png\" width=\"300\" height=\"90\" srcset=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/01\/fifo-300x90.png 300w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/01\/fifo.png 430w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u53ef\u95ef\u5165\u7684FIFO\u540c\u6b65\u5668\u53ea\u6709\u6982\u7387\u6027\u7684\u516c\u5e73\u5c5e\u6027\u3002\u9501\u961f\u5217\u5934\u90e8\u4e00\u4e2a\u89e3\u9664\u4e86\u963b\u585e\u7684\u7ebf\u7a0b\u62e5\u6709\u4e00\u6b21\u65e0\u504f\u5411\u7684\u673a\u4f1a\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u5373\u4e0d\u4f1a\u504f\u5411\u961f\u5934\u7684\u7ebf\u7a0b\u4e5f\u4e0d\u4f1a\u504f\u5411\u95ef\u5165\u7684\u7ebf\u7a0b<\/i>\uff09\u6765\u8d62\u5f97\u4e0e<i>\u95ef\u5165<\/i>\u7684\u7ebf\u7a0b\u4e4b\u95f4\u7684\u7ade\u4e89\uff0c\u5982\u679c\u7ade\u4e89\u5931\u8d25\uff0c\u8981\u4e48\u91cd\u65b0\u963b\u585e\u8981\u4e48\u8fdb\u884c\u91cd\u8bd5\u3002\u7136\u800c\uff0c\u5982\u679c<i>\u95ef\u5165<\/i>\u7684\u7ebf\u7a0b\u5230\u8fbe\u7684\u901f\u5ea6\u6bd4\u961f\u5934\u7684\u7ebf\u7a0b\u89e3\u9664\u963b\u585e\u5feb\uff0c\u90a3\u4e48\u5728\u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u5c06\u5f88\u96be\u8d62\u5f97\u7ade\u4e89\uff0c\u4ee5\u81f3\u4e8e\u51e0\u4e4e\u603b\u8981\u91cd\u65b0\u963b\u585e\uff0c\u5e76\u4e14\u5b83\u7684\u540e\u7ee7\u8282\u70b9\u4e5f\u4f1a\u4e00\u76f4\u4fdd\u6301\u963b\u585e\u3002\u5bf9\u4e8e\u77ed\u6682\u6301\u6709\u7684\u540c\u6b65\u5668\u6765\u8bf4\uff0c\u5728\u961f\u5217\u4e2d\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u88ab\u89e3\u9664\u963b\u585e\u671f\u95f4\uff0c\u591a\u5904\u7406\u5668\u4e0a\u5f88\u53ef\u80fd\u53d1\u751f\u8fc7\u591a\u6b21<i>\u95ef\u5165<\/i>\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u5373\u95ef\u5165\u7684\u7ebf\u7a0b\u7684<code>acquire<\/code>\u64cd\u4f5c<\/i>\uff09\u548c<code>release<\/code>\u4e86\u3002\u6b63\u5982\u4e0b\u6587\u6240\u63d0\u5230\u7684\uff0c\u6700\u7ec8\u7ed3\u679c\u5c31\u662f\u4fdd\u6301\u4e00\u6216\u591a\u4e2a\u7ebf\u7a0b\u7684\u9ad8\u8fdb\u5c55\u901f\u5ea6\u7684\u540c\u65f6\uff0c\u4ecd\u81f3\u5c11\u5728\u4e00\u5b9a\u6982\u7387\u4e0a\u907f\u514d\u4e86\u9965\u997f\u7684\u53d1\u751f\u3002<\/p>\n<p>\u5f53\u6709\u66f4\u9ad8\u7684\u516c\u5e73\u6027\u9700\u6c42\u65f6\uff0c\u5b9e\u73b0\u8d77\u6765\u4e5f\u5f88\u7b80\u5355\u3002\u5982\u679c\u9700\u8981\u4e25\u683c\u7684\u516c\u5e73\u6027\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u628atryAcquire\u65b9\u6cd5\u5b9a\u4e49\u4e3a\uff0c\u82e5\u5f53\u524d\u7ebf\u7a0b\u4e0d\u662f\u961f\u5217\u7684\u5934\u8282\u70b9\uff08\u53ef\u901a\u8fc7<code>getFirstQueuedThread<\/code>\u65b9\u6cd5\u68c0\u67e5\uff0c\u8fd9\u662f\u6846\u67b6\u63d0\u4f9b\u7684\u4e3a\u6570\u4e0d\u591a\u7684\u51e0\u4e2a\u68c0\u6d4b\u65b9\u6cd5\u4e4b\u4e00\uff09\uff0c\u5219\u7acb\u5373\u5931\u8d25\uff08\u8fd4\u56defalse\uff09\u3002<\/p>\n<p>\u4e00\u4e2a\u66f4\u5feb\uff0c\u4f46\u975e\u4e25\u683c\u516c\u5e73\u7684\u53d8\u4f53\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u82e5\u961f\u5217\u4e3a\u7a7a\uff08\u5224\u65ad\u7684\u77ac\u95f4\uff09\uff0c\u4ecd\u7136\u5141\u8bb8<code>tryAcquire<\/code>\u6267\u884c\u6210\u529f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u9047\u5230\u4e00\u4e2a\u7a7a\u961f\u5217\u65f6\u53ef\u80fd\u4f1a\u53bb\u7ade\u4e89\u4ee5\u4f7f\u81ea\u5df1\u7b2c\u4e00\u4e2a\u83b7\u5f97\u9501\uff0c\u8fd9\u6837\uff0c\u901a\u5e38\u81f3\u5c11\u6709\u4e00\u4e2a\u7ebf\u7a0b\u662f\u65e0\u9700\u5165\u961f\u5217\u7684\u3002<code>java.util.concurrent<\/code>\u5305\u4e2d\u6240\u6709\u652f\u6301\u516c\u5e73\u6a21\u5f0f\u7684\u540c\u6b65\u5668\u90fd\u91c7\u7528\u4e86\u8fd9\u79cd\u7b56\u7565\u3002<\/p>\n<p>\u5c3d\u7ba1\u516c\u5e73\u6027\u8bbe\u7f6e\u5728\u5b9e\u8df5\u4e2d\u5f88\u6709\u7528\uff0c\u4f46\u662f\u5b83\u4eec\u5e76\u6ca1\u6709\u4fdd\u969c\uff0c\u56e0\u4e3aJava Language Specification\u6ca1\u6709\u63d0\u4f9b\u8fd9\u6837\u7684\u8c03\u5ea6\u4fdd\u8bc1\u3002\u4f8b\u5982\uff1a\u5373\u4f7f\u662f\u4e25\u683c\u516c\u5e73\u7684\u540c\u6b65\u5668\uff0c\u5982\u679c\u4e00\u7ec4\u7ebf\u7a0b\u6c38\u8fdc\u4e0d\u9700\u8981\u963b\u585e\u6765\u8fbe\u5230\u4e92\u76f8\u7b49\u5f85\uff0c\u90a3\u4e48JVM\u53ef\u80fd\u4f1a\u51b3\u5b9a\u7eaf\u7cb9\u4ee5\u987a\u5e8f\u65b9\u5f0f\u8fd0\u884c\u5b83\u4eec\u3002\u5728\u5b9e\u9645\u4e2d\uff0c\u5355\u5904\u7406\u5668\u4e0a\uff0c\u5728\u62a2\u5360\u5f0f\u4e0a\u4e0b\u6587\u5207\u6362\u4e4b\u524d\uff0c\u8fd9\u6837\u7684\u7ebf\u7a0b\u6709\u53ef\u80fd\u662f\u5404\u81ea\u8fd0\u884c\u4e86\u4e00\u6bb5\u65f6\u95f4\u3002\u5982\u679c\u8fd9\u6837\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u6301\u6709\u67d0\u4e2a\u4e92\u65a5\u9501\uff0c\u5b83\u5c06\u5f88\u5feb\u4f1a\u88ab\u5207\u6362\u56de\u6765\uff0c\u4ec5\u662f\u4e3a\u4e86\u91ca\u653e\u5176\u6301\u6709\u7684\u9501\uff0c\u7136\u540e\u4f1a\u7ee7\u7eed\u963b\u585e\uff0c\u56e0\u4e3a\u5b83\u77e5\u9053\u6709\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u9700\u8981\u8fd9\u628a\u9501\uff0c\u8fd9\u66f4\u589e\u52a0\u4e86\u540c\u6b65\u5668\u53ef\u7528\u4f46\u6ca1\u6709\u7ebf\u7a0b\u80fd\u6765\u83b7\u53d6\u4e4b\u95f4\u7684\u95f4\u9694\u3002\u540c\u6b65\u5668\u516c\u5e73\u6027\u8bbe\u7f6e\u5728\u591a\u5904\u7406\u5668\u4e0a\u7684\u5f71\u54cd\u53ef\u80fd\u4f1a\u66f4\u5927\uff0c\u56e0\u4e3a\u5728\u8fd9\u79cd\u73af\u5883\u4e0b\u4f1a\u4ea7\u751f\u66f4\u591a\u7684\u4ea4\u9519\uff0c\u56e0\u6b64\u4e00\u4e2a\u7ebf\u7a0b\u5c31\u4f1a\u6709\u66f4\u591a\u7684\u673a\u4f1a\u53d1\u73b0\u9501\u88ab\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8bf7\u6c42\u3002<\/p>\n<p>\u5728\u9ad8\u7ade\u4e89\u4e0b\uff0c\u5f53\u4fdd\u62a4\u7684\u662f\u77ed\u6682\u6301\u6709\u9501\u7684\u4ee3\u7801\u4f53\u65f6\uff0c\u5c3d\u7ba1\u6027\u80fd\u53ef\u80fd\u4f1a\u8f83\u5dee\uff0c\u4f46\u516c\u5e73\u9501\u4ecd\u7136\u80fd\u6709\u6548\u5730\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u5f53\u516c\u5e73\u6027\u9501\u4fdd\u62a4\u7684\u662f\u76f8\u5bf9\u957f\u7684\u4ee3\u7801\u4f53\u548c\/\u6216\u6709\u7740\u76f8\u5bf9\u957f\u7684\u9501\u95f4(inter-lock)\u95f4\u9694\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<i>\u95ef\u5165<\/i>\u53ea\u80fd\u5e26\u6765\u5f88\u5c0f\u7684\u6027\u80fd\u4f18\u52bf\uff0c\u4f46\u5374\u53ef\u80fd\u4f1a\u5927\u5927\u589e\u52a0\u65e0\u9650\u7b49\u5f85\u7684\u98ce\u9669\u3002\u540c\u6b65\u5668\u6846\u67b6\u5c06\u8fd9\u4e9b\u5de5\u7a0b\u51b3\u7b56\u7559\u7ed9\u7528\u6237\u6765\u786e\u5b9a\u3002<\/p>\n<h4 id=\"synchronizers\">4.2 \u540c\u6b65\u5668<\/h4>\n<p>\u4e0b\u9762\u662f<code>java.util.concurrent<\/code>\u5305\u4e2d\u540c\u6b65\u5668\u5b9a\u4e49\u65b9\u5f0f\u7684\u6982\u8ff0\uff1a<\/p>\n<p><code>ReentrantLock<\/code>\u7c7b\u4f7f\u7528AQS\u540c\u6b65\u72b6\u6001\u6765\u4fdd\u5b58\u9501\uff08\u91cd\u590d\uff09\u6301\u6709\u7684\u6b21\u6570\u3002\u5f53\u9501\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u65f6\uff0c<code>ReentrantLock<\/code>\u4e5f\u4f1a\u8bb0\u5f55\u4e0b\u5f53\u524d\u83b7\u5f97\u9501\u7684\u7ebf\u7a0b\u6807\u8bc6\uff0c\u4ee5\u4fbf\u68c0\u67e5\u662f\u5426\u662f\u91cd\u590d\u83b7\u53d6\uff0c\u4ee5\u53ca\u5f53\u9519\u8bef\u7684\u7ebf\u7a0b\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u5982\u679c\u7ebf\u7a0b\u4e0d\u662f\u9501\u7684\u6301\u6709\u8005\uff0c\u5728\u6b64\u7ebf\u7a0b\u4e2d\u6267\u884c\u8be5\u9501\u7684<code>unlock<\/code>\u64cd\u4f5c\u5c31\u662f\u975e\u6cd5\u7684<\/i>\uff09\u8bd5\u56fe\u8fdb\u884c\u89e3\u9501\u64cd\u4f5c\u65f6\u68c0\u6d4b\u662f\u5426\u5b58\u5728\u975e\u6cd5\u72b6\u6001\u5f02\u5e38\u3002<code>ReentrantLock<\/code>\u4e5f\u4f7f\u7528\u4e86AQS\u63d0\u4f9b\u7684ConditionObject\uff0c\u8fd8\u5411\u5916\u66b4\u9732\u4e86\u5176\u5b83\u76d1\u63a7\u548c\u76d1\u6d4b\u76f8\u5173\u7684\u65b9\u6cd5\u3002<code>ReentrantLock<\/code>\u901a\u8fc7\u5728\u5185\u90e8\u58f0\u660e\u4e24\u4e2a\u4e0d\u540c\u7684<code>AbstractQueuedSynchronizer<\/code>\u5b9e\u73b0\u7c7b\uff08\u63d0\u4f9b\u516c\u5e73\u6a21\u5f0f\u7684\u90a3\u4e2a\u7981\u7528\u4e86<i>\u95ef\u5165<\/i>\u7b56\u7565\uff09\u6765\u5b9e\u73b0\u53ef\u9009\u7684\u516c\u5e73\u6a21\u5f0f\uff0c\u5728\u521b\u5efaReentrantLock\u5b9e\u4f8b\u7684\u65f6\u5019\u6839\u636e\u8bbe\u7f6e\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u5373<code>ReentrantLock<\/code>\u6784\u9020\u65b9\u6cd5\u4e2d\u7684<code>fair<\/code>\u53c2\u6570<\/i>\uff09\u4f7f\u7528\u76f8\u5e94\u7684<code>AbstractQueuedSynchronizer<\/code>\u5b9e\u73b0\u7c7b\u3002<\/p>\n<p><code>ReentrantReadWriteLock<\/code>\u7c7b\u4f7f\u7528AQS\u540c\u6b65\u72b6\u6001\u4e2d\u768416\u4f4d\u6765\u4fdd\u5b58\u5199\u9501\u6301\u6709\u7684\u6b21\u6570\uff0c\u5269\u4e0b\u768416\u4f4d\u7528\u6765\u4fdd\u5b58\u8bfb\u9501\u7684\u6301\u6709\u6b21\u6570\u3002<code>WriteLock<\/code>\u7684\u6784\u5efa\u65b9\u5f0f\u540c<code>ReentrantLock<\/code>\u3002<code>ReadLock<\/code>\u5219\u901a\u8fc7\u4f7f\u7528<code>acquireShared<\/code>\u65b9\u6cd5\u6765\u652f\u6301\u540c\u65f6\u5141\u8bb8\u591a\u4e2a\u8bfb\u7ebf\u7a0b\u3002<\/p>\n<p><code>Semaphore<\/code>\u7c7b\uff08\u8ba1\u6570\u4fe1\u53f7\u91cf\uff09\u4f7f\u7528AQS\u540c\u6b65\u72b6\u6001\u6765\u4fdd\u5b58\u4fe1\u53f7\u91cf\u7684\u5f53\u524d\u8ba1\u6570\u3002\u5b83\u91cc\u9762\u5b9a\u4e49\u7684acquireShared\u65b9\u6cd5\u4f1a\u51cf\u5c11\u8ba1\u6570\uff0c\u6216\u5f53\u8ba1\u6570\u4e3a\u975e\u6b63\u503c\u65f6\u963b\u585e\u7ebf\u7a0b\uff1b<code>tryRelease<\/code>\u65b9\u6cd5\u4f1a\u589e\u52a0\u8ba1\u6570\uff0c\u53ef\u80fd\u5728\u8ba1\u6570\u4e3a\u6b63\u503c\u65f6\u8fd8\u8981\u89e3\u9664\u7ebf\u7a0b\u7684\u963b\u585e\u3002<\/p>\n<p><code>CountDownLatch<\/code>\u7c7b\u4f7f\u7528AQS\u540c\u6b65\u72b6\u6001\u6765\u8868\u793a\u8ba1\u6570\u3002\u5f53\u8be5\u8ba1\u6570\u4e3a0\u65f6\uff0c\u6240\u6709\u7684acquire\u64cd\u4f5c\uff08<i>\u8bd1\u8005\u6ce8\uff1aacquire\u64cd\u4f5c\u662f\u4eceaqs\u7684\u89d2\u5ea6\u8bf4\u7684\uff0c\u5bf9\u5e94\u5230<code>CountDownLatch<\/code>\u4e2d\u5c31\u662f<code>await<\/code>\u65b9\u6cd5<\/i>\uff09\u624d\u80fd\u901a\u8fc7\u3002<\/p>\n<p><code>FutureTask<\/code>\u7c7b\u4f7f\u7528AQS\u540c\u6b65\u72b6\u6001\u6765\u8868\u793a\u67d0\u4e2a\u5f02\u6b65\u8ba1\u7b97\u4efb\u52a1\u7684\u8fd0\u884c\u72b6\u6001\uff08\u521d\u59cb\u5316\u3001\u8fd0\u884c\u4e2d\u3001\u88ab\u53d6\u6d88\u548c\u5b8c\u6210\uff09\u3002\u8bbe\u7f6e\uff08<i>\u8bd1\u8005\u6ce8\uff1a<code>FutureTask<\/code>\u7684<code>set<\/code>\u65b9\u6cd5<\/i>\uff09\u6216\u53d6\u6d88\uff08<i>\u8bd1\u8005\u6ce8\uff1a<code>FutureTask<\/code>\u7684<code>cancel<\/code>\u65b9\u6cd5<\/i>\uff09\u4e00\u4e2a<code>FutureTask<\/code>\u65f6\u4f1a\u8c03\u7528AQS\u7684<code>release<\/code>\u64cd\u4f5c\uff0c\u7b49\u5f85\u8ba1\u7b97\u7ed3\u679c\u7684\u7ebf\u7a0b\u7684\u963b\u585e\u89e3\u9664\u662f\u901a\u8fc7AQS\u7684<code>acquire<\/code>\u64cd\u4f5c\u5b9e\u73b0\u7684\u3002<\/p>\n<p><code>SynchronousQueues<\/code>\u7c7b\uff08\u4e00\u79cdCSP<i>\uff08Communicating Sequential Processes\uff09<\/i>\u5f62\u5f0f\u7684\u4f20\u9012\uff09\u4f7f\u7528\u4e86\u5185\u90e8\u7684\u7b49\u5f85\u8282\u70b9\uff0c\u8fd9\u4e9b\u8282\u70b9\u53ef\u4ee5\u7528\u4e8e\u534f\u8c03\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u3002\u540c\u65f6\uff0c\u5b83\u4f7f\u7528AQS\u540c\u6b65\u72b6\u6001\u6765\u63a7\u5236\u5f53\u67d0\u4e2a\u6d88\u8d39\u8005\u6d88\u8d39\u5f53\u524d\u4e00\u9879\u65f6\uff0c\u5141\u8bb8\u4e00\u4e2a\u751f\u4ea7\u8005\u7ee7\u7eed\u751f\u4ea7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002<\/p>\n<p><code>java.util.concurrent<\/code>\u5305\u7684\u4f7f\u7528\u8005\u5f53\u7136\u4e5f\u53ef\u4ee5\u4e3a\u81ea\u5b9a\u4e49\u7684\u5e94\u7528\u5b9a\u4e49\u81ea\u5df1\u7684\u540c\u6b65\u5668\u3002\u4f8b\u5982\uff0c\u90a3\u4e9b\u66fe\u8003\u8651\u5230\u8fc7\u7684\uff0c\u4f46\u6ca1\u6709\u91c7\u7eb3\u8fdb\u8fd9\u4e2a\u5305\u7684\u540c\u6b65\u5668\u5305\u62ec\u63d0\u4f9bWIN32\u4e8b\u4ef6\u5404\u79cd\u98ce\u683c\u7684\u8bed\u4e49\u7c7b\uff0c\u4e8c\u5143\u4fe1\u53f7\u91cf\uff0c\u96c6\u4e2d\u7ba1\u7406\u7684\u9501\u4ee5\u53ca\u57fa\u4e8e\u6811\u7684\u5c4f\u969c\u3002<\/p>\n<h2 id=\"performance\">5 \u6027\u80fd<\/h2>\n<p>\u867d\u7136AQS\u6846\u67b6\u9664\u4e86\u652f\u6301\u4e92\u65a5\u9501\u5916\uff0c\u8fd8\u652f\u6301\u5176\u5b83\u5f62\u5f0f\u7684\u540c\u6b65\u65b9\u5f0f\uff0c\u4f46\u9501\u7684\u6027\u80fd\u662f\u6700\u5bb9\u6613\u6d4b\u91cf\u548c\u6bd4\u8f83\u7684\u3002\u5373\u4f7f\u5982\u6b64\uff0c\u4e5f\u8fd8\u5b58\u5728\u8bb8\u591a\u4e0d\u540c\u7684\u6d4b\u91cf\u65b9\u5f0f\u3002\u8fd9\u91cc\u7684\u5b9e\u9a8c\u4e3b\u8981\u662f\u8bbe\u8ba1\u6765\u5c55\u793a\u9501\u7684\u5f00\u9500\u548c\u541e\u5410\u91cf\u3002<\/p>\n<p>\u5728\u6bcf\u4e2a\u6d4b\u8bd5\u4e2d\uff0c\u6240\u6709\u7ebf\u7a0b\u90fd\u91cd\u590d\u7684\u66f4\u65b0\u4e00\u4e2a\u4f2a\u968f\u673a\u6570\uff0c\u8be5\u968f\u673a\u6570\u7531<code>nextRandom(int seed)<\/code>\u65b9\u6cd5\u8ba1\u7b97\uff1a<\/p>\n<p>[code lang=&#8221;java&#8221;]<br \/>\nint t = (seed % 127773) * 16807 &#8211; (seed \/ 127773) * 2836;<br \/>\nreturn (t &gt; 0) ? t : t + 0x7fffffff;<br \/>\n[\/code]<\/p>\n<p>\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u7ebf\u7a0b\u4ee5\u6982\u7387S\u5728\u4e00\u4e2a\u4e92\u65a5\u9501\u4e0b\u66f4\u65b0\u5171\u4eab\u7684\u751f\u6210\u5668\uff0c\u5426\u5219\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u6982\u7387\u4e3a1-S<\/i>\uff09\u66f4\u65b0\u5176\u81ea\u5df1\u5c40\u90e8\u7684\u751f\u6210\u5668\uff0c\u6b64\u65f6\u662f\u4e0d\u9700\u8981\u9501\u7684\u3002\u5982\u6b64\uff0c\u9501\u5360\u7528\u533a\u57df\u7684\u8017\u65f6\u662f\u77ed\u6682\u7684\uff0c\u8fd9\u5c31\u4f7f\u7ebf\u7a0b\u6301\u6709\u9501\u671f\u95f4\u88ab\u62a2\u5360\u65f6\u7684\u5916\u754c\u5e72\u6270\u964d\u5230\u4e86\u6700\u5c0f\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u968f\u673a\u6027\u4e3b\u8981\u662f\u4e3a\u4e86\u4e24\u4e2a\u76ee\u7684\uff1a\u786e\u5b9a\u662f\u5426\u9700\u8981\u4f7f\u7528\u9501\uff08\u8fd9\u4e2a\u751f\u6210\u5668\u8db3\u4ee5\u5e94\u4ed8\u8fd9\u91cc\u7684\u9700\u6c42\uff09\uff0c\u4ee5\u53ca\u4f7f\u5faa\u73af\u4e2d\u7684\u4ee3\u7801\u4e0d\u53ef\u80fd\u88ab\u8f7b\u6613\u5730\u4f18\u5316\u6389\u3002<\/p>\n<p>\u8fd9\u91cc\u6bd4\u8f83\u4e86\u56db\u79cd\u9501\uff1a\u5185\u7f6e\u9501\uff0c\u7528\u7684\u662f<code>synchronized<\/code>\u5757\uff1b\u4e92\u65a5\u9501\uff0c\u7528\u7684\u662f\u50cf\u7b2c\u56db\u8282\u4f8b\u5b50\u4e2d\u7684\u90a3\u6837\u7b80\u5355\u7684Mutex\u7c7b\uff1b\u53ef\u91cd\u5165\u9501\uff0c\u7528\u7684\u662f<code>ReentrantLock<\/code>\uff1b\u4ee5\u53ca\u516c\u5e73\u9501\uff0c\u7528\u7684\u662f<code>ReentrantLock<\/code>\u7684\u516c\u5e73\u6a21\u5f0f\u3002\u6240\u6709\u6d4b\u8bd5\u90fd\u8fd0\u884c\u5728J2SE1.5 JDK build46\uff08\u5927\u81f4\u4e0ebeta2\u76f8\u540c\uff09\u7684server\u6a21\u5f0f\u4e0b\u3002\u5728\u6536\u96c6\u6d4b\u8bd5\u6570\u636e\u524d\uff0c\u6d4b\u8bd5\u7a0b\u5e8f\u5148\u8fd0\u884c20\u6b21\u975e\u7ade\u4e89\u7684\u6d4b\u8bd5\uff0c\u4ee5\u6392\u9664JVM\u201c\u9884\u70ed\u201d\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u66f4\u591a\u5173\u4e8e\u201c\u9884\u70ed\u201d\u7684\u5185\u5bb9\uff0c\u53c2\u89c1\uff1a<a href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/www.ibm.com\/developerworks\/cn\/java\/j-jtp12214\/#4.0\">Java \u7406\u8bba\u4e0e\u5b9e\u8df5: \u52a8\u6001\u7f16\u8bd1\u4e0e\u6027\u80fd\u6d4b\u91cf<\/a><\/i>\uff09\u8fc7\u7a0b\u7684\u5f71\u54cd\u3002\u9664\u4e86\u516c\u5e73\u6a21\u5f0f\u4e0b\u7684\u6d4b\u8bd5\u53ea\u8dd1\u4e86\u4e00\u767e\u4e07\u6b21\u8fed\u4ee3\uff0c\u5176\u5b83\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u7684\u6d4b\u8bd5\u90fd\u8fd0\u884c\u4e86\u4e00\u5343\u4e07\u6b21\u8fed\u4ee3\u3002<\/p>\n<p>\u8be5\u6d4b\u8bd5\u8fd0\u884c\u5728\u56db\u4e2aX86\u673a\u5668\u548c\u56db\u4e2aUltraSparc\u673a\u5668\u4e0a\u3002\u6240\u6709X86\u673a\u5668\u90fd\u8fd0\u884c\u7684\u662fRedHat\u57fa\u4e8eNPTL 2.4\u5185\u6838\u548c\u5e93\u7684Linux\u7cfb\u7edf\u3002\u6240\u6709\u7684UltraSparc\u673a\u5668\u90fd\u8fd0\u884c\u7684\u662fSolaris-9\u3002\u6d4b\u8bd5\u65f6\u6240\u6709\u7cfb\u7edf\u7684\u8d1f\u8f7d\u90fd\u5f88\u8f7b\u3002\u6839\u636e\u8be5\u6d4b\u8bd5\u7684\u7279\u5f81\uff0c\u5e76\u4e0d\u8981\u6c42\u7cfb\u7edf\u5b8c\u5168\u7a7a\u95f2\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u5373\u6d4b\u8bd5\u65f6\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u6709\u5176\u5b83\u8f83\u8f7b\u7684\u8d1f\u8f7d\u4e5f\u4e0d\u4f1a\u5f71\u54cd\u672c\u6b21\u6d4b\u8bd5\u7684\u7ed3\u679c\u3002<\/i>\uff09\u3002\u201c4P\u201d\u8fd9\u4e2a\u540d\u5b57\u53cd\u6620\u51fa\u53cc\u6838\u8d85\u7ebf\u7a0b\u7684Xeon\u66f4\u50cf\u662f4\u8def\u673a\u5668\uff0c\u800c\u4e0d\u662f2\u8def\u673a\u5668\u3002\u8fd9\u91cc\u6ca1\u6709\u5c06\u6d4b\u8bd5\u6570\u636e\u89c4\u8303\u5316\u3002\u5982\u4e0b\u6240\u793a\uff0c\u540c\u6b65\u7684\u76f8\u5bf9\u5f00\u9500\u4e0e\u5904\u7406\u5668\u7684\u6570\u91cf\u3001\u7c7b\u578b\u3001\u901f\u5ea6\u4e4b\u95f4\u4e0d\u5177\u5907\u7b80\u5355\u7684\u5173\u7cfb\u3002<\/p>\n<p><strong>\u88681 \u6d4b\u8bd5\u7684\u5e73\u53f0<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u540d\u5b57<\/th>\n<th>\u5904\u7406\u5668\u6570\u91cf<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u901f\u5ea6(Mhz)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1P<\/td>\n<td align=\"right\">1<\/td>\n<td align=\"left\">Pentium3<\/td>\n<td align=\"right\">900<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2P<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"left\">Pentium3<\/td>\n<td align=\"right\">1400<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2A<\/td>\n<td align=\"right\">2<\/td>\n<td align=\"left\">Athlon<\/td>\n<td align=\"right\">2000<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">4P<\/td>\n<td align=\"right\">2HT<\/td>\n<td align=\"left\">Pentium4\/Xeon<\/td>\n<td align=\"right\">2400<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">1U<\/td>\n<td align=\"right\">1<\/td>\n<td align=\"left\">UltraSparc2<\/td>\n<td align=\"right\">650<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">4U<\/td>\n<td align=\"right\">4<\/td>\n<td align=\"left\">UltraSparc2<\/td>\n<td align=\"right\">450<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">8U<\/td>\n<td align=\"right\">8<\/td>\n<td align=\"left\">UltraSparc3<\/td>\n<td align=\"right\">750<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">24U<\/td>\n<td align=\"right\">24<\/td>\n<td align=\"left\">UltraSparc3<\/td>\n<td align=\"right\">750<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"cost\">5.1 \u5f00\u9500<\/h3>\n<p>\u65e0\u7ade\u4e89\u60c5\u51b5\u4e0b\u7684\u5f00\u9500\u662f\u901a\u8fc7\u4ec5\u8fd0\u884c\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5c06\u6982\u7387S\u4e3a1\u65f6\u7684\u6bcf\u6b21\u8fed\u4ee3\u65f6\u95f4\u51cf\u53bb\u6982\u7387S\u4e3a0\uff08\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u7684\u6982\u7387\u4e3a0\uff09\u65f6\u7684\u6bcf\u6b21\u8fed\u4ee3\u65f6\u95f4\u5f97\u5230\u7684\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u8fd9\u91cc\u7684\u201c\u6982\u7387S\u201d\u5373\u524d\u6587\u63d0\u5230\u7684\u201c\u6982\u7387S\u201d\uff0c\u6982\u7387\u4e3a0\u65f6\u662f\u6ca1\u6709\u9501\u64cd\u4f5c\u7684\uff0c\u6982\u7387\u4e3a1\u65f6\u662f\u6bcf\u6b21\u90fd\u6709\u9501\u64cd\u4f5c\uff0c\u56e0\u6b64\u5c06\u6982\u7387\u4e3a1\u65f6\u7684\u8017\u65f6\u51cf\u53bb\u6982\u7387\u4e3a0\u65f6\u7684\u8017\u65f6\u5c31\u662f\u6574\u4e2a\u9501\u64cd\u4f5c\u7684\u5f00\u9500\u3002<\/i>\uff09\u3002\u88682\u4ee5\u7eb3\u79d2\u4e3a\u5355\u4f4d\u663e\u793a\u4e86\u975e\u7ade\u4e89\u573a\u666f\u4e0b\u6bcf\u6b21\u9501\u64cd\u4f5c\u7684\u5f00\u9500\u3002Mutex\u7c7b\u6700\u63a5\u8fd1\u4e8e\u6846\u67b6\u7684\u57fa\u672c\u8017\u65f6\uff0c\u53ef\u91cd\u5165\u9501\u7684\u989d\u5916\u5f00\u9500\u662f\u8bb0\u5f55\u5f53\u524d\u6240\u6709\u8005\u7ebf\u7a0b\u548c\u9519\u8bef\u68c0\u67e5\u7684\u8017\u65f6\uff0c\u5bf9\u4e8e\u516c\u5e73\u9501\u6765\u8bf4\u8fd8\u5305\u542b\u5f00\u59cb\u65f6\u68c0\u67e5\u961f\u5217\u662f\u5426\u4e3a\u7a7a\u7684\u8017\u65f6\u3002<\/p>\n<p>\u8868\u683c2\u4e5f\u5c55\u793a\u4e0e\u5185\u7f6e\u9501\u7684\u201c\u5feb\u901f\u8def\u5f84\uff08fast path\uff09\u201d\u5bf9\u6bd4\uff0c<code>tryAcquire<\/code>\u7684\u8017\u65f6\u3002\u8fd9\u91cc\u7684\u5dee\u5f02\u4e3b\u8981\u53cd\u6620\u51fa\u4e86\u5404\u9501\u548c\u673a\u5668\u4e2d\u4f7f\u7528\u7684\u4e0d\u540c\u7684\u539f\u5b50\u6307\u4ee4\u4ee5\u53ca\u5185\u5b58\u5c4f\u969c\u7684\u8017\u65f6\u3002\u5728\u591a\u5904\u7406\u5668\u4e0a\uff0c\u8fd9\u4e9b\u6307\u4ee4\u5e38\u5e38\u662f\u5b8c\u5168\u4f18\u4e8e\u6240\u6709\u5176\u5b83\u6307\u4ee4\u7684\u3002\u5185\u7f6e\u9501\u548c\u540c\u6b65\u5668\u7c7b\u4e4b\u95f4\u7684\u4e3b\u8981\u5dee\u522b\uff0c\u663e\u7136\u662f\u7531\u4e8eHotspot\u9501\u5728\u9501\u5b9a\u548c\u89e3\u9501\u65f6\u90fd\u4f7f\u7528\u4e86\u4e00\u6b21<code>compareAndSet<\/code>\uff0c\u800c\u540c\u6b65\u5668\u7684<code>acquire<\/code>\u64cd\u4f5c\u4f7f\u7528\u4e86\u4e00\u6b21<code>compareAndSet<\/code>\uff0c\u4f46<code>release<\/code>\u64cd\u4f5c\u7528\u7684\u662f\u4e00\u6b21<code>volatile<\/code>\u5199\uff08\u5373\uff0c\u591a\u5904\u7406\u5668\u4e0a\u7684\u4e00\u6b21\u5185\u5b58\u5c4f\u969c\u4ee5\u53ca\u6240\u6709\u5904\u7406\u5668\u4e0a\u7684\u91cd\u6392\u5e8f\u9650\u5236\uff09\u3002\u6bcf\u4e2a\u9501\u7684\u7edd\u5bf9\u7684\u548c\u76f8\u5bf9\u8017\u65f6\u56e0\u673a\u5668\u7684\u4e0d\u540c\u800c\u4e0d\u540c\u3002<\/p>\n<p><strong>\u88682 \u65e0\u7ade\u4e89\u65f6\u7684\u5355\u9501\u5f00\u9500\uff08\u5355\u4f4d\uff1a\u7eb3\u79d2\uff09<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u673a\u5668<\/th>\n<th>\u5185\u7f6e<\/th>\n<th>\u4e92\u65a5<\/th>\n<th>\u53ef\u91cd\u5165<\/th>\n<th>\u516c\u5e73\u53ef\u91cd\u5165<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1P<\/td>\n<td align=\"right\">18<\/td>\n<td align=\"right\">9<\/td>\n<td align=\"right\">31<\/td>\n<td align=\"right\">37<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2P<\/td>\n<td align=\"right\">58<\/td>\n<td align=\"right\">71<\/td>\n<td align=\"right\">77<\/td>\n<td align=\"right\">81<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2A<\/td>\n<td align=\"right\">13<\/td>\n<td align=\"right\">21<\/td>\n<td align=\"right\">31<\/td>\n<td align=\"right\">30<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">4P<\/td>\n<td align=\"right\">116<\/td>\n<td align=\"right\">95<\/td>\n<td align=\"right\">109<\/td>\n<td align=\"right\">117<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">1U<\/td>\n<td align=\"right\">90<\/td>\n<td align=\"right\">40<\/td>\n<td align=\"right\">58<\/td>\n<td align=\"right\">67<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">4U<\/td>\n<td align=\"right\">122<\/td>\n<td align=\"right\">82<\/td>\n<td align=\"right\">100<\/td>\n<td align=\"right\">115<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">8U<\/td>\n<td align=\"right\">160<\/td>\n<td align=\"right\">83<\/td>\n<td align=\"right\">103<\/td>\n<td align=\"right\">123<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">24U<\/td>\n<td align=\"right\">161<\/td>\n<td align=\"right\">84<\/td>\n<td align=\"right\">108<\/td>\n<td align=\"right\">119<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ece\u53e6\u4e00\u4e2a\u6781\u7aef\u770b\uff0c\u88683\u5c55\u793a\u4e86\u6982\u7387S\u4e3a1\uff0c\u8fd0\u884c256\u4e2a\u5e76\u53d1\u7ebf\u7a0b\u65f6\u4ea7\u751f\u4e86\u5927\u89c4\u6a21\u7684\u9501\u7ade\u4e89\u4e0b\u6bcf\u4e2a\u9501\u7684\u5f00\u9500\u3002\u5728\u5b8c\u5168\u9971\u548c\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef<i>\u95ef\u5165<\/i>\u7684FIFO\u9501\u6bd4\u5185\u7f6e\u9501\u7684\u5f00\u9500\u5c11\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\uff08\u4e5f\u5c31\u662f\u66f4\u5927\u7684\u541e\u5410\u91cf\uff09\uff0c\u6bd4\u516c\u5e73\u9501\u66f4\u662f\u5c11\u4e86\u4e24\u4e2a\u6570\u91cf\u7ea7\u3002\u8fd9\u8868\u73b0\u51fa\u5373\u4f7f\u6709\u7740\u6781\u5927\u7684\u7ade\u4e89\uff0c\u5728\u7ef4\u6301\u7ebf\u7a0b\u8fdb\u5c55\u65b9\u9762\u53ef<i>\u95ef\u5165<\/i>FIFO\u7b56\u7565\u7684\u6548\u7387\u3002<\/p>\n<p>\u88683\u4e5f\u8bf4\u660e\u4e86\u5373\u4f7f\u5728\u5185\u90e8\u5f00\u9500\u6bd4\u8f83\u4f4e\u7684\u60c5\u51b5\u4e0b\uff0c\u516c\u5e73\u9501\u7684\u6027\u80fd\u4e5f\u5b8c\u5168\u662f\u7531\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u65f6\u95f4\u6240\u51b3\u5b9a\u7684\u3002\u5217\u51fa\u7684\u65f6\u95f4\u5927\u81f4\u4e0a\u90fd\u4e0e\u5404\u5e73\u53f0\u4e0a\u7ebf\u7a0b\u963b\u585e\u548c\u89e3\u9664\u7ebf\u7a0b\u963b\u585e\u7684\u65f6\u95f4\u76f8\u79f0\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u540e\u9762\u589e\u52a0\u7684\u4e00\u4e2a\u5b9e\u9a8c\uff08\u4ec5\u4f7f\u7528\u673a\u56684P\uff09\u8868\u660e\uff0c\u5bf9\u4e8e\u8fd9\u91cc\u7528\u5230\u7684\u77ed\u6682\u6301\u6709\u7684\u9501\uff0c\u516c\u5e73\u53c2\u6570\u7684\u8bbe\u7f6e\u5728\u603b\u5dee\u5f02\u4e2d\u7684\u5f71\u54cd\u5f88\u5c0f\u3002\u8fd9\u91cc\u5c06\u7ebf\u7a0b\u7ec8\u6b62\u65f6\u95f4\u95f4\u7684\u5dee\u5f02\u8bb0\u5f55\u6210\u4e00\u4e2a\u7c97\u7c92\u5ea6\u7684\u79bb\u6563\u91cf\u6570\u3002\u57284P\u7684\u673a\u5668\u4e0a\uff0c\u516c\u5e73\u9501\u7684\u65f6\u95f4\u5ea6\u91cf\u7684\u6807\u51c6\u5dee\u5e73\u5747\u4e3a0.7%\uff0c\u53ef\u91cd\u5165\u9501\u5e73\u5747\u4e3a6.0%\u3002\u4f5c\u4e3a\u5bf9\u6bd4\uff0c\u4e3a\u6a21\u62df\u4e00\u4e2a\u957f\u65f6\u95f4\u6301\u6709\u9501\u7684\u573a\u666f\uff0c\u6d4b\u8bd5\u4e2d\u4f7f\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u6301\u6709\u9501\u7684\u60c5\u51b5\u4e0b\u8ba1\u7b97\u4e8616K\u6b21\u968f\u673a\u6570\u3002\u8fd9\u65f6\uff0c\u603b\u8fd0\u884c\u65f6\u95f4\u51e0\u4e4e\u662f\u76f8\u540c\u7684\uff08\u516c\u5e73\u9501\uff1a9.79s\uff0c\u53ef\u91cd\u5165\u9501\uff1a9.72s\uff09\u3002\u516c\u5e73\u6a21\u5f0f\u4e0b\u7684\u5dee\u5f02\u4f9d\u7136\u5f88\u5c0f\uff0c\u6807\u51c6\u5dee\u5e73\u5747\u4e3a0.1%\uff0c\u800c\u53ef\u91cd\u5165\u9501\u4e0a\u5347\u5230\u4e86\u5e73\u574729.5%\u3002<\/p>\n<p><strong>\u8868\u683c3 \u9971\u548c\u65f6\u7684\u5355\u9501\u5f00\u9500\uff08\u5355\u4f4d\uff1a\u7eb3\u79d2\uff09<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u673a\u5668<\/th>\n<th>\u5185\u7f6e<\/th>\n<th>\u4e92\u65a5<\/th>\n<th>\u53ef\u91cd\u5165<\/th>\n<th>\u516c\u5e73\u53ef\u91cd\u5165<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"center\">1P<\/td>\n<td align=\"right\">521<\/td>\n<td align=\"right\">46<\/td>\n<td align=\"right\">67<\/td>\n<td align=\"right\">8327<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2P<\/td>\n<td align=\"right\">930<\/td>\n<td align=\"right\">108<\/td>\n<td align=\"right\">132<\/td>\n<td align=\"right\">14967<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">2A<\/td>\n<td align=\"right\">748<\/td>\n<td align=\"right\">79<\/td>\n<td align=\"right\">84<\/td>\n<td align=\"right\">33910<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">4P<\/td>\n<td align=\"right\">1146<\/td>\n<td align=\"right\">188<\/td>\n<td align=\"right\">247<\/td>\n<td align=\"right\">15328<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">1U<\/td>\n<td align=\"right\">879<\/td>\n<td align=\"right\">153<\/td>\n<td align=\"right\">177<\/td>\n<td align=\"right\">41394<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">4U<\/td>\n<td align=\"right\">2590<\/td>\n<td align=\"right\">347<\/td>\n<td align=\"right\">368<\/td>\n<td align=\"right\">30004<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">8U<\/td>\n<td align=\"right\">1274<\/td>\n<td align=\"right\">157<\/td>\n<td align=\"right\">174<\/td>\n<td align=\"right\">31084<\/td>\n<\/tr>\n<tr>\n<td align=\"center\">24U<\/td>\n<td align=\"right\">1983<\/td>\n<td align=\"right\">160<\/td>\n<td align=\"right\">182<\/td>\n<td align=\"right\">32291<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"throughput\">5.2 \u541e\u5410\u91cf<\/h3>\n<p>\u5927\u90e8\u5206\u540c\u6b65\u5668\u90fd\u662f\u7528\u4e8e\u65e0\u7ade\u4e89\u548c\u6781\u5927\u7ade\u4e89\u4e4b\u95f4\u7684\u3002\u8fd9\u53ef\u4ee5\u7528\u5b9e\u9a8c\u5728\u4e24\u4e2a\u65b9\u9762\u8fdb\u884c\u68c0\u67e5\uff0c\u901a\u8fc7\u4fee\u6539\u56fa\u5b9a\u4e2a\u7ebf\u7a0b\u7684\u7ade\u4e89\u6982\u7387\uff0c\u548c\/\u6216\u901a\u8fc7\u5f80\u62e5\u6709\u56fa\u5b9a\u7ade\u4e89\u6982\u7387\u7684\u7ebf\u7a0b\u96c6\u5408\u91cc\u589e\u52a0\u66f4\u591a\u7684\u7ebf\u7a0b\u3002\u4e3a\u4e86\u8bf4\u660e\u8fd9\u4e9b\u5f71\u54cd\uff0c\u6d4b\u8bd5\u8fd0\u884c\u5728\u4e0d\u540c\u7684\u7ade\u4e89\u6982\u7387\u548c\u4e0d\u540c\u7684\u7ebf\u7a0b\u6570\u76ee\u4e0b\uff0c\u90fd\u7528\u7684\u662f\u53ef\u91cd\u5165\u9501\u3002\u9644\u56fe\u4f7f\u7528\u4e86\u4e00\u4e2a<i>slowdown<\/i>\u5ea6\u91cf\u6807\u51c6\u3002<\/p>\n<p><a href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/formula.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-3290\" alt=\"\" src=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/formula-300x66.jpg\" width=\"300\" height=\"66\" srcset=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/formula-300x66.jpg 300w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/formula.jpg 887w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u8fd9\u91cc\uff0ct\u662f\u603b\u8fd0\u884c\u65f6\u95f4\uff0cb\u662f\u4e00\u4e2a\u7ebf\u7a0b\u5728\u6ca1\u6709\u7ade\u4e89\u6216\u540c\u6b65\u4e0b\u7684\u57fa\u7ebf\u65f6\u95f4\uff0cn\u662f\u7ebf\u7a0b\u6570\uff0cp\u662f\u5904\u7406\u5668\u6570\uff0cS\u662f\u5171\u4eab\u8bbf\u95ee\u7684\u6bd4\u4f8b\uff08<i>\u8bd1\u8005\u6ce8\uff1a\u5373\u524d\u9762\u7684\u7ade\u4e89\u6982\u7387S<\/i>\uff09\u3002\u8ba1\u7b97\u7ed3\u679c\u662f\u5b9e\u9645\u6267\u884c\u65f6\u95f4\u4e0e\u7406\u60f3\u6267\u884c\u65f6\u95f4\uff08\u901a\u5e38\u662f\u65e0\u6cd5\u5f97\u5230\u7684\uff09\u7684\u6bd4\u7387\uff0c\u7406\u60f3\u6267\u884c\u65f6\u95f4\u662f\u901a\u8fc7\u4f7f\u7528Amdahl&#8217;s\u6cd5\u5219\u8ba1\u7b97\u51fa\u6765\u7684\u3002\u7406\u60f3\u65f6\u95f4\u6a21\u62df\u4e86\u4e00\u6b21\u6ca1\u6709\u540c\u6b65\u5f00\u9500\uff0c\u6ca1\u6709\u56e0\u9501\u4e89\u7528\u800c\u5bfc\u81f4\u7ebf\u7a0b\u963b\u585e\u7684\u6267\u884c\u8fc7\u7a0b\u3002\u5373\u4f7f\u8fd9\u6837\uff0c\u5728\u5f88\u4f4e\u7684\u7ade\u4e89\u4e0b\uff0c\u76f8\u6bd4\u7406\u60f3\u65f6\u95f4\uff0c\u6709\u4e00\u4e9b\u6d4b\u8bd5\u7ed3\u679c\u5374\u8868\u73b0\u51fa\u4e86\u5f88\u5c0f\u7684\u901f\u5ea6\u589e\u957f\uff0c\u5927\u6982\u662f\u7531\u4e8e\u57fa\u7ebf\u548c\u6d4b\u8bd5\u4e4b\u95f4\u7684\u4f18\u5316\u3001\u6d41\u6c34\u7ebf\u7b49\u65b9\u9762\u6709\u7740\u8f7b\u5fae\u7684\u5dee\u522b\u3002<\/p>\n<p>\u56fe\u4e2d\u7528\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570\u4e3a\u6bd4\u4f8b\u8fdb\u884c\u4e86\u7f29\u653e\u3002\u4f8b\u5982\uff0c\u503c\u4e3a1\u8868\u793a\u5b9e\u9645\u65f6\u95f4\u662f\u7406\u60f3\u65f6\u95f4\u7684\u4e24\u500d\uff0c4\u8868\u793a\u616216\u500d\u3002\u4f7f\u7528\u5bf9\u6570\u5c31\u4e0d\u9700\u8981\u4f9d\u8d56\u4e00\u4e2a\u968f\u610f\u7684\u57fa\u7ebf\u65f6\u95f4\uff08\u8fd9\u91cc\u6307\u7684\u662f\u8ba1\u7b97\u968f\u673a\u6570\u7684\u65f6\u95f4\uff09\uff0c\u56e0\u6b64\uff0c\u57fa\u4e8e\u4e0d\u540c\u5e95\u6570\u8ba1\u7b97\u7684\u7ed3\u679c\u8868\u73b0\u51fa\u7684\u8d8b\u52bf\u5e94\u8be5\u662f\u7c7b\u4f3c\u7684\u3002\u8fd9\u4e9b\u6d4b\u8bd5\u4f7f\u7528\u7684\u7ade\u4e89\u6982\u7387\u4ece1\/128\uff08\u6807\u8bc6\u4e3a\u201c0.008\u201d\uff09\u52301\uff0c\u4ee52\u7684\u5e42\u4e3a\u6b65\u957f\uff0c\u7ebf\u7a0b\u7684\u6570\u91cf\u4ece1\u52301024\uff0c\u4ee52\u7684\u5e42\u7684\u4e00\u534a\u4e3a\u6b65\u957f\u3002<\/p>\n<p>\u5728\u5355\u5904\u7406\u5668\uff081P\u548c1U\uff09\u4e0a\uff0c\u6027\u80fd\u968f\u7740\u7ade\u4e89\u7684\u4e0a\u5347\u800c\u4e0b\u964d\uff0c\u4f46\u4e0d\u4f1a\u968f\u7740\u7ebf\u7a0b\u6570\u7684\u589e\u52a0\u800c\u4e0b\u964d\u3002\u591a\u5904\u7406\u5668\u5728\u906d\u9047\u7ade\u4e89\u65f6\uff0c\u6027\u80fd\u4e0b\u964d\u7684\u66f4\u5feb\u3002\u6839\u636e\u591a\u5904\u7406\u5668\u76f8\u5173\u7684\u56fe\u8868\u663e\u793a\uff0c\u5f00\u59cb\u51fa\u73b0\u7684\u5cf0\u503c\u5904\u867d\u7136\u53ea\u6709\u51e0\u4e2a\u7ebf\u7a0b\u7684\u7ade\u4e89\uff0c\u4f46\u76f8\u5bf9\u6027\u80fd\u901a\u5e38\u5374\u6700\u5dee\u3002\u8fd9\u53cd\u6620\u51fa\u4e86\u4e00\u4e2a\u6027\u80fd\u7684<i>\u8fc7\u6e21\u533a\u57df<\/i>\uff0c\u5728\u8fd9\u91cc<i>\u95ef\u5165<\/i>\u7684\u7ebf\u7a0b\u548c\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u90fd\u51c6\u5907\u83b7\u53d6\u9501\uff0c\u8fd9\u4f1a\u8ba9\u5b83\u4eec\u9891\u7e41\u7684\u8feb\u4f7f\u5bf9\u65b9\u963b\u585e\u3002\u5728\u5927\u90e8\u5206\u65f6\u5019\uff0c\u8fc7\u6e21\u533a\u57df\u540e\u9762\u4f1a\u7d27\u63a5\u7740\u4e00\u4e2a<i>\u5e73\u6ed1\u533a\u57df<\/i>\uff0c\u56e0\u4e3a\u6b64\u65f6\u51e0\u4e4e\u6ca1\u6709\u7a7a\u95f2\u7684\u9501\uff0c\u6240\u4ee5\u4f1a\u4e0e\u5355\u5904\u7406\u5668\u4e0a\u987a\u5e8f\u6267\u884c\u7684\u6a21\u5f0f\u5dee\u4e0d\u591a\uff1b\u5728\u591a\u5904\u7406\u5668\u673a\u5668\u4e0a\u4f1a\u8f83\u65e9\u8fdb\u5165\u5e73\u6ed1\u533a\u57df\u3002\u4f8b\u5982\uff0c\u8bf7\u6ce8\u610f\uff0c\u5728\u6ee1\u7ade\u4e89\uff08\u6807\u8bc6\u4e3a\u201c1.000\u201d\uff09\u4e0b\u8fd9\u4e9b\u503c\u8868\u793a\uff0c\u5728\u5904\u7406\u5668\u8d8a\u5c11\u7684\u673a\u5668\u4e0a\uff0c\u4f1a\u6709\u66f4\u7cdf\u7cd5\u7684\u76f8\u5bf9\u901f\u5ea6\u4e0b\u964d\u3002<\/p>\n<p>\u6839\u636e\u8fd9\u4e9b\u7ed3\u679c\uff0c\u53ef\u4ee5\u9488\u5bf9\u963b\u585e\uff08park\/unpark\uff09\u505a\u8fdb\u4e00\u6b65\u8c03\u4f18\u4ee5\u51cf\u5c11\u4e0a\u4e0b\u6587\u5207\u6362\u548c\u76f8\u5173\u7684\u5f00\u9500\uff0c\u8fd9\u4f1a\u7ed9\u672c\u6846\u67b6\u5e26\u6765\u5c0f\u4f46\u663e\u8457\u7684\u63d0\u5347\u3002\u6b64\u5916\uff0c\u5728\u591a\u5904\u7406\u5668\u4e0a\u4e3a\u77ed\u65f6\u95f4\u6301\u6709\u7684\u4f46\u9ad8\u7ade\u4e89\u7684\u9501\u91c7\u7528\u67d0\u79cd\u5f62\u5f0f\u7684\u9002\u5e94\u6027\u81ea\u65cb\uff0c\u53ef\u4ee5\u907f\u514d\u8fd9\u91cc\u770b\u5230\u7684\u4e00\u4e9b\u6ce2\u52a8\uff0c\u8fd9\u5bf9\u540c\u6b65\u5668\u7c7b\u5927\u6709\u88e8\u76ca\u3002\u867d\u7136\u5728\u8de8\u4e0d\u540c\u4e0a\u4e0b\u6587\u65f6\u9002\u5e94\u6027\u81ea\u65cb\u5f88\u96be\u5f88\u597d\u7684\u5de5\u4f5c\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u672c\u6846\u67b6\u4e3a\u9047\u5230\u8fd9\u7c7b\u4f7f\u7528\u914d\u7f6e\u7684\u7279\u5b9a\u5e94\u7528\u6784\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f62\u5f0f\u7684\u9501\u3002<\/p>\n<p><a href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/1-1024x409.jpg\" alt=\"\" width=\"1024\" height=\"409\" class=\"aligncenter size-large wp-image-3296\" srcset=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/1-1024x409.jpg 1024w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/1-300x120.jpg 300w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/1.jpg 1250w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><br \/>\n<a href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/2-1024x385.jpg\" alt=\"\" width=\"1024\" height=\"385\" class=\"aligncenter size-large wp-image-3298\" srcset=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/2-1024x385.jpg 1024w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/2-300x113.jpg 300w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/2.jpg 1250w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><br \/>\n<a href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/3.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/3-1024x409.jpg\" alt=\"\" width=\"1024\" height=\"409\" class=\"aligncenter size-large wp-image-3300\" srcset=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/3-1024x409.jpg 1024w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/3-300x120.jpg 300w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/3.jpg 1250w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><br \/>\n<a href=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/4.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/4-1024x409.jpg\" alt=\"\" width=\"1024\" height=\"409\" class=\"aligncenter size-large wp-image-3294\" srcset=\"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/4-1024x409.jpg 1024w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/4-300x120.jpg 300w, https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-content\/uploads\/2013\/02\/4.jpg 1250w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<h2 id=\"conclusion\">6 \u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u64b0\u5199\u4e4b\u65f6\uff0c<code>java.util.concurrent<\/code>\u5305\u4e2d\u7684\u540c\u6b65\u5668\u6846\u67b6\u8fd8\u592a\u65b0\u6240\u4ee5\u8fd8\u4e0d\u80fd\u5728\u5b9e\u8df5\u4e2d\u4f7f\u7528\u3002\u56e0\u6b64\u5728J2SE 1.5\u6700\u7ec8\u7248\u672c\u53d1\u5e03\u4e4b\u524d\u90fd\u5f88\u96be\u770b\u5230\u5176\u5927\u8303\u56f4\u7684\u4f7f\u7528\uff0c\u5e76\u4e14\uff0c\u5b83\u7684\u8bbe\u8ba1\uff0cAPI\u5b9e\u73b0\u4ee5\u53ca\u6027\u80fd\u80af\u5b9a\u8fd8\u6709\u65e0\u6cd5\u9884\u6599\u7684\u540e\u679c\u3002\u4f46\u662f\uff0c\u6b64\u65f6\uff0c\u8fd9\u4e2a\u6846\u67b6\u660e\u663e\u80fd\u80dc\u4efb\u5176\u57fa\u672c\u7684\u76ee\u6807\uff0c\u5373\u4e3a\u521b\u5efa\u65b0\u7684\u540c\u6b65\u5668\u63d0\u4f9b\u4e00\u4e2a\u9ad8\u6548\u7684\u57fa\u7840\u3002<\/p>\n<h2 id=\"acknowledgments\">7 \u81f4\u8c22<\/h2>\n<p>Thanks to Dave Dice for countless ideas and advice during the development of this framework, to Mark Moir and Michael Scott for urging consideration of CLH queues, to David Holmes for critiquing early versions of the code and API, to Victor Luchangco and Bill Scherer for reviewing previous incarnations of the source code, and to the other members of the JSR166 Expert Group (Joe Bowbeer, Josh Bloch, Brian Goetz, David Holmes, and Tim Peierls) as well as Bill Pugh, for helping with design and specifications and commenting on drafts of this paper. Portions of this work were made possible by a DARPA PCES grant, NSF grant EIA-0080206 (for access to the 24way Sparc) and a Sun Collaborative Research Grant.<\/p>\n<h3 id=\"references\">\u53c2\u8003\u6587\u732e<\/h3>\n<ul>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r1\"><\/a>[1] Agesen, O., D. Detlefs, A. Garthwaite, R. Knippel, Y. S.Ramakrishna, and D. White. An Efficient Meta-lock for Implementing Ubiquitous Synchronization. ACM OOPSLA Proceedings, 1999.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r2\"><\/a>[2] Andrews, G. Concurrent Programming. Wiley, 1991.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r3\"><\/a>[3] Bacon, D. Thin Locks: Featherweight Synchronization for Java. ACM PLDI Proceedings, 1998.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r4\"><\/a>[4] Buhr, P. M. Fortier, and M. Coffin. Monitor Classification,ACM Computing Surveys, March 1995.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r5\"><\/a>[5] Craig, T. S. Building FIFO and priority-queueing spin locks from atomic swap. Technical Report TR 93-02-02,Department of Computer Science, University of Washington, Feb. 1993.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r6\"><\/a>[6] Gamma, E., R. Helm, R. Johnson, and J. Vlissides. Design Patterns, Addison Wesley, 1996.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r7\"><\/a>[7] Holmes, D. Synchronisation Rings, PhD Thesis, Macquarie University, 1999.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r8\"><\/a>[8] Magnussen, P., A. Landin, and E. Hagersten. Queue locks on cache coherent multiprocessors. 8th Intl. Parallel Processing Symposium, Cancun, Mexico, Apr. 1994.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r9\"><\/a>[9] Mellor-Crummey, J.M., and M. L. Scott. Algorithms for Scalable Synchronization on Shared-Memory Multiprocessors. ACM Trans. on Computer Systems,February 1991<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r10\"><\/a>[10] M. L. Scott and W N. Scherer III. Scalable Queue-Based Spin Locks with Timeout. 8th ACM Symp. on Principles and Practice of Parallel Programming, Snowbird, UT, June 2001.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r11\"><\/a>[11] Sun Microsystems. Multithreading in the Solaris Operating Environment. White paper available at https:\/\/2.zoppoz.workers.dev:443\/http\/wwws.sun.com\/software\/solaris\/whitepapers.html 2002.<\/li>\n<li><a style=\"font-size: 10px; color: black; text-decoration: none; margin: 0; padding: 0;\" name=\"r12\"><\/a>[12] Zhang, H., S. Liang, and L. Bak. Monitor Conversion in a Multithreaded Computer System. United States Patent 6,691,304. 2004.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5 \u4f5c\u8005\uff1aDoug Lea \u8bd1\u8005\uff1a\u6b27\u632f\u806a \u6821\u5bf9\uff1a\u4e01\u4e00 4 \u7528\u6cd5 AQS\u7c7b\u5c06\u4e0a\u8ff0\u7684\u529f\u80fd\u7ed3\u5408\u5230\u4e00\u8d77\uff0c\u5e76\u4e14\u4f5c\u4e3a [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29],"tags":[125],"class_list":["post-2501","post","type-post","status-publish","format-standard","hentry","category-concurrency-translation","tag-aqs"],"views":18464,"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/2.zoppoz.workers.dev:443\/https\/wp.me\/p8MSzY-El","jetpack_sharing_enabled":true,"post_thumbnail_image":"","content_first_image":null,"post_medium_image_300":null,"post_thumbnail_image_624":null,"pageviews":0,"total_comments":2,"category_name":"\u5e76\u53d1\u8bd1\u6587","_links":{"self":[{"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/posts\/2501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/comments?post=2501"}],"version-history":[{"count":0,"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/posts\/2501\/revisions"}],"wp:attachment":[{"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/media?parent=2501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/categories?post=2501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2.zoppoz.workers.dev:443\/http\/ifeve.com\/wp-json\/wp\/v2\/tags?post=2501"}],"curies":[{"name":"wp","href":"https:\/\/2.zoppoz.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}