{"id":8006,"date":"2024-06-27T10:01:01","date_gmt":"2024-06-27T02:01:01","guid":{"rendered":""},"modified":"2024-06-27T10:01:01","modified_gmt":"2024-06-27T02:01:01","slug":"synchronized\u9501\u539f\u7406_reentrantlock\u5e95\u5c42\u539f\u7406","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8006.html","title":{"rendered":"synchronized\u9501\u539f\u7406_reentrantlock\u5e95\u5c42\u539f\u7406"},"content":{"rendered":"
\u5e95\u5c42\u539f\u7406\u5b9e\u73b0\u7684\u4e0d\u540c<\/span><\/span><\/p>\n<\/p>\n \u4e92\u65a5\u540c\u6b65\u6700\u4e3b\u8981\u7684\u95ee\u9898\u5c31\u662f\u8fdb\u884c\u7ebf\u7a0b\u963b\u585e\u548c\u5524\u9192\u6240\u5e26\u6765\u7684\u6027\u80fd\u95ee\u9898\uff0c\u56e0\u800c\u8fd9\u79cd\u540c\u6b65\u53c8\u79f0\u4e3a\u963b\u585e\u540c\u6b65\uff0c\u5b83\u5c5e\u4e8e\u4e00\u79cd\u60b2\u89c2\u7684\u5e76\u53d1\u7b56\u7565\uff0c\u5373\u7ebf\u7a0b\u83b7\u5f97\u7684\u662f\u72ec\u5360\u9501\u3002<\/span>\u72ec\u5360\u9501\u610f\u5473\u7740\u5176\u4ed6\u7ebf\u7a0b\u53ea\u80fd\u4f9d\u9760\u963b\u585e\u6765\u7b49\u5f85\u7ebf\u7a0b\u91ca\u653e\u9501<\/span>\u3002\u800c\u5728 CPU \u8f6c\u6362\u7ebf\u7a0b\u963b\u585e\u65f6\u4f1a\u5f15\u8d77\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u5f53\u6709\u5f88\u591a\u7ebf\u7a0b\u7ade\u4e89\u9501\u7684\u65f6\u5019\uff0c\u4f1a\u5f15\u8d77 CPU \u9891\u7e41\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5bfc\u81f4\u6548\u7387\u5f88\u4f4e\u3002synchronized \u91c7\u7528\u7684\u4fbf\u662f\u8fd9\u79cd\u5e76\u53d1\u7b56\u7565\u3002<\/span><\/p>\n \u968f\u7740\u6307\u4ee4\u96c6\u7684\u53d1\u5c55\uff0c\u6211\u4eec\u6709\u4e86\u53e6\u4e00\u79cd\u9009\u62e9\uff1a<\/span>\u57fa\u4e8e\u51b2\u7a81\u68c0\u6d4b\u7684\u4e50\u89c2\u5e76\u53d1\u7b56\u7565<\/span>\uff0c\u901a\u4fd7\u5730\u8bb2\u5c31\u662f\u5148\u8fdb\u6027\u64cd\u4f5c\uff0c\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u7ebf\u7a0b\u4e89\u7528\u5171\u4eab\u6570\u636e\uff0c\u90a3\u64cd\u4f5c\u5c31\u6210\u529f\u4e86\uff0c\u5982\u679c\u5171\u4eab\u6570\u636e\u88ab\u4e89\u7528\uff0c\u4ea7\u751f\u4e86\u51b2\u7a81\uff0c\u90a3\u5c31\u518d\u8fdb\u884c\u5176\u4ed6\u7684\u8865\u507f\u63aa\u65bd\uff08\u6700\u5e38\u89c1\u7684\u8865\u507f\u63aa\u65bd\u5c31\u662f\u4e0d\u65ad\u5730\u91cd\u62fe\uff0c\u76f4\u5230\u8bd5\u6210\u529f\u4e3a\u6b62\uff09\uff0c\u8fd9\u79cd\u4e50\u89c2\u7684\u5e76\u53d1\u7b56\u7565\u7684\u8bb8\u591a\u5b9e\u73b0\u90fd\u4e0d\u9700\u8981\u628a\u7ebf\u7a0b\u6302\u8d77\uff0c\u56e0\u6b64\u8fd9\u79cd\u540c\u6b65\u88ab\u79f0\u4e3a\u975e\u963b\u585e\u540c\u6b65\u3002ReetrantLock \u91c7\u7528\u7684\u4fbf\u662f\u8fd9\u79cd\u5e76\u53d1\u7b56\u7565\u3002<\/span><\/p>\n \u5728\u4e50\u89c2\u7684\u5e76\u53d1\u7b56\u7565\u4e2d\uff0c\u9700\u8981\u64cd\u4f5c\u548c\u51b2\u7a81\u68c0\u6d4b\u8fd9\u4e24\u4e2a\u6b65\u9aa4\u5177\u5907\u539f\u5b50\u6027\uff0c\u5b83\u9760\u786c\u4ef6\u6307\u4ee4\u6765\u4fdd\u8bc1\uff0c\u8fd9\u91cc\u7528\u7684\u662f CAS \u64cd\u4f5c\uff08Compare and Swap\uff09\u3002JDK1.5 \u4e4b\u540e\uff0cJava \u7a0b\u5e8f\u624d\u53ef\u4ee5\u4f7f\u7528CAS\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7814\u7a76 ReentrantLock \u7684\u6e90\u4ee3\u7801\uff0c\u4f1a\u53d1\u73b0\u5176\u4e2d\u6bd4\u8f83\u91cd\u8981\u7684\u83b7\u5f97\u9501\u7684\u4e00\u4e2a\u65b9\u6cd5\u662f compareAndSetState\uff0c\u8fd9\u91cc\u5176\u5b9e\u5c31\u662f\u8c03\u7528\u7684 CPU \u63d0\u4f9b\u7684\u7279\u6b8a\u6307\u4ee4\u3002\u73b0\u4ee3\u7684 CPU \u63d0\u4f9b\u4e86\u6307\u4ee4\uff0c\u53ef\u4ee5\u81ea\u52a8\u66f4\u65b0\u5171\u4eab\u6570\u636e\uff0c\u800c\u4e14\u80fd\u591f\u68c0\u6d4b\u5230\u5176\u4ed6\u7ebf\u7a0b\u7684\u5e72\u6270\uff0c\u800c compareAndSet() \u5c31\u7528\u8fd9\u4e9b\u4ee3\u66ff\u4e86\u9501\u5b9a\u3002\u8fd9\u4e2a\u7b97\u6cd5\u79f0\u4f5c\u975e\u963b\u585e\u7b97\u6cd5\uff0c\u610f\u601d\u662f\u4e00\u4e2a\u7ebf\u7a0b\u7684\u5931\u8d25\u6216\u8005\u6302\u8d77\u4e0d\u5e94\u8be5\u5f71\u54cd\u5176\u4ed6\u7ebf\u7a0b\u7684\u5931\u8d25\u6216\u6302\u8d77\u3002<\/p>\n Java 5 \u4e2d\u5f15\u5165\u4e86\u6ce8\u5165 AutomicInteger\u3001AutomicLong\u3001AutomicReference \u7b49\u7279\u6b8a\u7684\u539f\u5b50\u6027\u53d8\u91cf\u7c7b\uff0c\u5b83\u4eec\u63d0\u4f9b\u7684\u5982\uff1acompareAndSet()\u3001incrementAndSet()\u548cgetAndIncrement()\u7b49\u65b9\u6cd5\u90fd\u4f7f\u7528\u4e86 CAS \u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5b83\u4eec\u90fd\u662f\u7531\u786c\u4ef6\u6307\u4ee4\u6765\u4fdd\u8bc1\u7684\u539f\u5b50\u65b9\u6cd5\u3002<\/p>\n <\/p>\n \u57fa\u672c\u8bed\u6cd5\u4e0a\uff0cReentrantLock \u4e0e synchronized \u5f88\u76f8\u4f3c\uff0c\u5b83\u4eec\u90fd\u5177\u5907\u4e00\u6837\u7684\u7ebf\u7a0b\u91cd\u5165\u7279\u6027\uff0c\u53ea\u662f\u4ee3\u7801\u5199\u6cd5\u4e0a\u6709\u70b9\u533a\u522b\u800c\u5df2\uff0c\u4e00\u4e2a\u8868\u73b0\u4e3a API \u5c42\u9762\u7684\u4e92\u65a5\u9501\uff08Lock\uff09\uff0c\u4e00\u4e2a\u8868\u73b0\u4e3a\u539f\u751f\u8bed\u6cd5\u5c42\u9762\u7684\u4e92\u65a5\u9501\uff08synchronized\uff09\u3002ReentrantLock \u76f8\u5bf9 synchronized \u800c\u8a00\u8fd8\u662f\u589e\u52a0\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e09\u9879\uff1a<\/p>\n ReetrantLock \u6709\u4e24\u79cd\u9501\uff1a\u5ffd\u7565\u4e2d\u65ad\u9501\u548c\u54cd\u5e94\u4e2d\u65ad\u9501\u3002\u5ffd\u7565\u4e2d\u65ad\u9501\u4e0e synchronized \u5b9e\u73b0\u7684\u4e92\u65a5\u9501\u4e00\u6837\uff0c\u4e0d\u80fd\u54cd\u5e94\u4e2d\u65ad\uff0c\u800c\u54cd\u5e94\u4e2d\u65ad\u9501\u53ef\u4ee5\u54cd\u5e94\u4e2d\u65ad\u3002<\/p>\n \u5982\u679c\u67d0\u4e00\u7ebf\u7a0b A \u6b63\u5728\u6267\u884c\u9501\u4e2d\u7684\u4ee3\u7801\uff0c\u53e6\u4e00\u7ebf\u7a0bB\u6b63\u5728\u7b49\u5f85\u83b7\u53d6\u8be5\u9501\uff0c\u53ef\u80fd\u7531\u4e8e\u7b49\u5f85\u65f6\u95f4\u8fc7\u957f\uff0c\u7ebf\u7a0b B \u4e0d\u60f3\u7b49\u5f85\u4e86\uff0c\u60f3\u5148\u5904\u7406\u5176\u4ed6\u4e8b\u60c5\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u5b83\u4e2d\u65ad\u81ea\u5df1\u6216\u8005\u5728\u522b\u7684\u7ebf\u7a0b\u4e2d\u4e2d\u65ad\u5b83\uff0c\u5982\u679c\u6b64\u65f6 ReetrantLock \u63d0\u4f9b\u7684\u662f\u5ffd\u7565\u4e2d\u65ad\u9501\uff0c\u5219\u5b83\u4e0d\u4f1a\u53bb\u7406\u4f1a\u8be5\u4e2d\u65ad\uff0c\u800c\u662f\u8ba9\u7ebf\u7a0bB\u7ee7\u7eed\u7b49\u5f85\uff0c\u800c\u5982\u679c\u6b64\u65f6 ReetrantLock \u63d0\u4f9b\u7684\u662f\u54cd\u5e94\u4e2d\u65ad\u9501\uff0c\u90a3\u4e48\u5b83\u4fbf\u4f1a\u5904\u7406\u4e2d\u65ad\uff0c\u8ba9\u7ebf\u7a0b B \u653e\u5f03\u7b49\u5f85\uff0c\u8f6c\u800c\u53bb\u5904\u7406\u5176\u4ed6\u4e8b\u60c5\u3002<\/p>\n \u83b7\u5f97\u54cd\u5e94\u4e2d\u65ad\u9501\u7684\u4e00\u822c\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n \u8fd9\u91cc\u6709\u4e00\u4e2a\u4e0d\u9519\u7684\u5206\u6790\u4e2d\u65ad\u7684\u793a\u4f8b\u4ee3\u7801\uff08\u6458\u81ea\u7f51\u4e0a\uff09\u3002<\/p>\n \u5f53\u7528 synchronized \u4e2d\u65ad\u5bf9\u4e92\u65a5\u9501\u7684\u7b49\u5f85\u65f6\uff0c\u5e76\u4e0d\u8d77\u4f5c\u7528\uff0c\u8be5\u7ebf\u7a0b\u4f9d\u7136\u4f1a\u4e00\u76f4\u7b49\u5f85\uff0c\u5982\u4e0b\u9762\u7684\u5b9e\u4f8b\uff1a<\/p>\n \u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n <\/p>\n \u6211\u4eec\u7b49\u5f85\u4e86\u5f88\u4e45\uff0c\u540e\u9762\u4f9d\u7136\u6ca1\u6709\u8f93\u51fa\uff0c\u8bf4\u660e\u8bfb\u7ebf\u7a0b\u5bf9\u4e92\u65a5\u9501\u7684\u7b49\u5f85\u5e76\u6ca1\u6709\u88ab\u4e2d\u65ad\uff0c\u4e5f\u5c31\u662f\u8be5\u6237\u5403\u9501\u6ca1\u6709\u54cd\u5e94\u5bf9\u8bfb\u7ebf\u7a0b\u7684\u4e2d\u65ad\u3002<\/p>\n \u6211\u4eec\u518d\u5c06\u4e0a\u9762\u4ee3\u7801\u4e2d synchronized \u7684\u4e92\u65a5\u9501\u6539\u4e3a ReentrantLock \u7684\u54cd\u5e94\u4e2d\u65ad\u9501\uff0c\u5373\u6539\u4e3a\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n \u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n <\/p>\n \u4ece\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5c1d\u8bd5\u4e2d\u65ad\u540e\u8f93\u51fa\u4e86 catch \u8bed\u53e5\u5757\u4e2d\u7684\u5185\u5bb9\uff0c\u4e5f\u8f93\u51fa\u4e86\u540e\u9762\u7684\u201c\u8bfb\u7ed3\u675f\u201d\uff0c\u8bf4\u660e\u7ebf\u7a0b\u5bf9\u4e92\u65a5\u9501\u7684\u7b49\u5f85\u88ab\u4e2d\u65ad\u4e86\uff0c\u4e5f\u5c31\u662f\u8be5\u4e92\u65a5\u9501\u54cd\u5e94\u4e86\u5bf9\u8bfb\u7ebf\u7a0b\u7684\u4e2d\u65ad\u3002<\/p>\n \n<\/div>\n <\/p>\n","protected":false},"excerpt":{"rendered":"synchronized\u9501\u539f\u7406_reentrantlock\u5e95\u5c42\u539f\u7406\u5e95\u5c42\u539f\u7406\u5b9e\u73b0\u7684\u4e0d\u540c\u4e92\u65a5\u540c\u6b65\u6700\u4e3b\u8981\u7684\u95ee\u9898\u5c31\u662f\u8fdb\u884c\u7ebf\u7a0b\u963b\u585e\u548c\u5524\u9192\u6240\u5e26\u6765\u7684\u6027\u80fd\u95ee\u9898...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8006"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8006"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8006\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}} \u7528\u9014\u6bd4\u8f83<\/h4>\n<\/p>\n
\n
<\/span><\/span>\n<\/div>\n \u53ef\u4e2d\u65ad\u9501<\/h3>\n
ReentrantLock lock = new ReentrantLock(); ........... lock.lockInterruptibly();\/\/\u83b7\u53d6\u54cd\u5e94\u4e2d\u65ad\u9501 try { \/\/\u66f4\u65b0\u5bf9\u8c61\u7684\u72b6\u6001 \/\/\u6355\u83b7\u5f02\u5e38\uff0c\u5fc5\u8981\u65f6\u6062\u590d\u5230\u539f\u6765\u7684\u4e0d\u53d8\u7ea6\u675f \/\/\u5982\u679c\u6709return\u8bed\u53e5\uff0c\u653e\u5728\u8fd9\u91cc }finally{ lock.unlock(); \/\/\u9501\u5fc5\u987b\u5728finally\u5757\u4e2d\u91ca\u653e } <\/code><\/pre>\n
public class Buffer { private Object lock; public Buffer() { lock = this; } public void write() { synchronized (lock) { long startTime = System.currentTimeMillis(); System.out.println(\"\u5f00\u59cb\u5f80\u8fd9\u4e2abuff\u5199\u5165\u6570\u636e\u2026\"); for (;;)\/\/ \u6a21\u62df\u8981\u5904\u7406\u5f88\u957f\u65f6\u95f4 { if (System.currentTimeMillis() - startTime > Integer.MAX_VALUE) { break; } } System.out.println(\"\u7ec8\u4e8e\u5199\u5b8c\u4e86\"); } } public void read() { synchronized (lock) { System.out.println(\"\u4ece\u8fd9\u4e2abuff\u8bfb\u6570\u636e\"); } } public static void main(String[] args) { Buffer buff = new Buffer(); final Writer writer = new Writer(buff); final Reader reader = new Reader(buff); writer.start(); reader.start(); new Thread(new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); for (;;) { \/\/\u7b495\u79d2\u949f\u53bb\u4e2d\u65ad\u8bfb if (System.currentTimeMillis() - start > 5000) { System.out.println(\"\u4e0d\u7b49\u4e86\uff0c\u5c1d\u8bd5\u4e2d\u65ad\"); reader.interrupt(); \/\/\u5c1d\u8bd5\u4e2d\u65ad\u8bfb\u7ebf\u7a0b break; } } } }).start(); \/\/ \u6211\u4eec\u671f\u5f85\u201c\u8bfb\u201d\u8fd9\u4e2a\u7ebf\u7a0b\u80fd\u9000\u51fa\u7b49\u5f85\u9501\uff0c\u53ef\u662f\u4e8b\u4e0e\u613f\u8fdd\uff0c\u4e00\u65e6\u8bfb\u8fd9\u4e2a\u7ebf\u7a0b\u53d1\u73b0\u81ea\u5df1\u5f97\u4e0d\u5230\u9501\uff0c \/\/ \u5c31\u4e00\u76f4\u5f00\u59cb\u7b49\u5f85\u4e86\uff0c\u5c31\u7b97\u5b83\u7b49\u6b7b\uff0c\u4e5f\u5f97\u4e0d\u5230\u9501\uff0c\u56e0\u4e3a\u5199\u7ebf\u7a0b\u898121\u4ebf\u79d2\u624d\u80fd\u5b8c\u6210 T_T \uff0c\u5373\u4f7f\u6211\u4eec\u4e2d\u65ad\u5b83\uff0c \/\/ \u5b83\u90fd\u4e0d\u6765\u54cd\u5e94\u4e0b\uff0c\u770b\u6765\u771f\u7684\u8981\u7b49\u6b7b\u4e86\u3002\u8fd9\u4e2a\u65f6\u5019\uff0cReentrantLock\u7ed9\u4e86\u4e00\u79cd\u673a\u5236\u8ba9\u6211\u4eec\u6765\u54cd\u5e94\u4e2d\u65ad\uff0c \/\/ \u8ba9\u201c\u8bfb\u201d\u80fd\u4f38\u80fd\u5c48\uff0c\u52c7\u6562\u653e\u5f03\u5bf9\u8fd9\u4e2a\u9501\u7684\u7b49\u5f85\u3002\u6211\u4eec\u6765\u6539\u5199Buffer\u8fd9\u4e2a\u7c7b\uff0c\u5c31\u53ebBufferInterruptibly\u5427\uff0c\u53ef\u4e2d\u65ad\u7f13\u5b58\u3002 } } class Writer extends Thread { private Buffer buff; public Writer(Buffer buff) { this.buff = buff; } @Override public void run() { buff.write(); } } class Reader extends Thread { private Buffer buff; public Reader(Buffer buff) { this.buff = buff; } @Override public void run() { buff.read();\/\/\u8fd9\u91cc\u4f30\u8ba1\u4f1a\u4e00\u76f4\u963b\u585e System.out.println(\"\u8bfb\u7ed3\u675f\"); } } <\/code><\/pre>\n
import java.util.concurrent.locks.ReentrantLock; public class BufferInterruptibly { private ReentrantLock lock = new ReentrantLock(); public void write() { lock.lock(); try { long startTime = System.currentTimeMillis(); System.out.println(\"\u5f00\u59cb\u5f80\u8fd9\u4e2abuff\u5199\u5165\u6570\u636e\u2026\"); for (;;)\/\/ \u6a21\u62df\u8981\u5904\u7406\u5f88\u957f\u65f6\u95f4 { if (System.currentTimeMillis() - startTime > Integer.MAX_VALUE) { break; } } System.out.println(\"\u7ec8\u4e8e\u5199\u5b8c\u4e86\"); } finally { lock.unlock(); } } public void read() throws InterruptedException { lock.lockInterruptibly();\/\/ \u6ce8\u610f\u8fd9\u91cc\uff0c\u53ef\u4ee5\u54cd\u5e94\u4e2d\u65ad try { System.out.println(\"\u4ece\u8fd9\u4e2abuff\u8bfb\u6570\u636e\"); } finally { lock.unlock(); } } public static void main(String args[]) { BufferInterruptibly buff = new BufferInterruptibly(); final Writer2 writer = new Writer2(buff); final Reader2 reader = new Reader2(buff); writer.start(); reader.start(); new Thread(new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); for (;;) { if (System.currentTimeMillis() - start > 5000) { System.out.println(\"\u4e0d\u7b49\u4e86\uff0c\u5c1d\u8bd5\u4e2d\u65ad\"); reader.interrupt(); \/\/\u6b64\u5904\u4e2d\u65ad\u8bfb\u64cd\u4f5c break; } } } }).start(); } } class Reader2 extends Thread { private BufferInterruptibly buff; public Reader2(BufferInterruptibly buff) { this.buff = buff; } @Override public void run() { try { buff.read();\/\/\u53ef\u4ee5\u6536\u5230\u4e2d\u65ad\u7684\u5f02\u5e38\uff0c\u4ece\u800c\u6709\u6548\u9000\u51fa } catch (InterruptedException e) { System.out.println(\"\u6211\u4e0d\u8bfb\u4e86\"); } System.out.println(\"\u8bfb\u7ed3\u675f\"); } } class Writer2 extends Thread { private BufferInterruptibly buff; public Writer2(BufferInterruptibly buff) { this.buff = buff; } @Override public void run() { buff.write(); } } <\/code><\/pre>\n