{"id":235,"date":"2015-04-16T20:09:39","date_gmt":"2015-04-16T12:09:39","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=235"},"modified":"2026-08-18T12:09:29","modified_gmt":"2026-08-18T04:09:29","slug":"java%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=235","title":{"rendered":"Java\u7ebf\u7a0b\u540c\u6b65"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7ebf\u7a0b\u8981\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u5fc5\u987b\u4fdd\u8bc1\u67d0\u4e00\u523b\u53ea\u6709\u4e00\u6761\u7ebf\u7a0b\u5360\u7528\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53eb<strong style=\"color: #4870ac;\">\u540c\u6b65<\/strong>\uff08synchronization\uff09\u3002Java \u5728\u8bed\u8a00\u5c42\u9762\u63d0\u4f9b\u4e86\u652f\u6301\u3002<\/p>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">\u5173\u952e\u6982\u5ff5\u662f\u7ba1\u7a0b\uff08\u4e5f\u53eb\u4fe1\u53f7\u91cf semaphore\uff09\uff1a\u4e00\u4e2a\u4e92\u65a5\u72ec\u5360\u9501\u5b9a\uff0c\u6216\u79f0\u4e92\u65a5\u4f53\uff08mutex\uff09\u3002\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u6761\u7ebf\u7a0b\u80fd\u83b7\u5f97\u7ba1\u7a0b\uff1b\u5176\u4ed6\u8bd5\u56fe\u8fdb\u5165\u5df2\u9501\u5b9a\u7ba1\u7a0b\u7684\u7ebf\u7a0b\u5fc5\u987b\u6302\u8d77\u7b49\u5f85\u3002\u62e5\u6709\u7ba1\u7a0b\u7684\u7ebf\u7a0b\u53ef\u4ee5\u518d\u6b21\u8fdb\u5165\u540c\u4e00\u4e2a\u7ba1\u7a0b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">C \/ C++ \u5f80\u5f80\u8981\u8c03\u64cd\u4f5c\u7cfb\u7edf\u539f\u8bed\uff0c\u7528\u8d77\u6765\u522b\u626d\u3002Java \u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">synchronized<\/code> \u628a\u5927\u90e8\u5206\u590d\u6742\u6027\u6536\u6389\u3002\u540c\u6b65\u4ee3\u7801\u6709\u4e24\u79cd\u5199\u6cd5\uff1a\u540c\u6b65\u65b9\u6cd5\u548c\u540c\u6b65\u8bed\u53e5\u5757\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5199\u6cd5<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u4f55\u65f6\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">synchronized \u65b9\u6cd5<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7c7b\u662f\u4f60\u5199\u7684\uff0c\u6574\u6bb5\u65b9\u6cd5\u90fd\u8981\u4e92\u65a5<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">synchronized (obj) { &#8230; }<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7b2c\u4e09\u65b9\u7c7b\u6ca1\u6709\u540c\u6b65\u65b9\u6cd5\uff0c\u53c8\u62ff\u4e0d\u5230\u6e90\u7801<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u4f7f\u7528\u540c\u6b65\u65b9\u6cd5<\/h2>\n<p style=\"margin: 1em 0px;\">\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u9690\u5f0f\u7ba1\u7a0b\u3002\u8fdb\u5165\u5b83\u5c31\u662f\u8c03\u7528\u88ab synchronized \u4fee\u9970\u7684\u65b9\u6cd5\u3002\u4e00\u6761\u7ebf\u7a0b\u5728\u540c\u6b65\u65b9\u6cd5\u91cc\u65f6\uff0c\u5176\u4ed6\u8bd5\u56fe\u8c03\u7528\u8be5\u5b9e\u4f8b\u540c\u4e00\uff08\u6216\u5176\u4ed6\uff09\u540c\u6b65\u65b9\u6cd5\u7684\u7ebf\u7a0b\u5fc5\u987b\u7b49\u3002\u4ece\u540c\u6b65\u65b9\u6cd5\u8fd4\u56de\u5373\u9000\u51fa\u7ba1\u7a0b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5148\u770b\u4e00\u4e2a\u8be5\u540c\u6b65\u5374\u6ca1\u540c\u6b65\u7684\u4f8b\u5b50\u3002Callme.call \u5728\u65b9\u62ec\u53f7\u91cc\u6253\u5370\u5b57\u7b26\u4e32\uff0c\u4e2d\u95f4 sleep 1 \u79d2\uff0c\u6545\u610f\u628a\u65f6\u95f4\u7247\u8ba9\u51fa\u53bb\u3002\u4e09\u4e2a Caller \u5171\u7528\u540c\u4e00\u4e2a Callme \u5b9e\u4f8b\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">class Callme {\n    void call(String msg) {\n        System.out.print(\"[\" + msg);\n        try { Thread.sleep(1000); } catch (InterruptedException e) {\n            System.out.println(\"Interrupted\");\n        }\n        System.out.println(\"]\");\n    }\n}\nclass Caller implements Runnable {\n    String msg;\n    Callme target;\n    Thread t;\n    public Caller(Callme targ, String s) {\n        target = targ;\n        msg = s;\n        t = new Thread(this);\n        t.start();\n    }\n    public void run() { target.call(msg); }\n}\nclass Synch {\n    public static void main(String args[]) {\n        Callme target = new Callme();\n        Caller ob1 = new Caller(target, \"Hello\");\n        Caller ob2 = new Caller(target, \"Synchronized\");\n        Caller ob3 = new Caller(target, \"World\");\n        try {\n            ob1.t.join();\n            ob2.t.join();\n            ob3.t.join();\n        } catch (InterruptedException e) {\n            System.out.println(\"Interrupted\");\n        }\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6ca1\u6709\u540c\u6b65\u65f6\u8f93\u51fa\u4f1a\u6405\u5728\u4e00\u8d77\uff0c\u4f8b\u5982 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Hello[Synchronized[World]<\/code> \u518d\u8ddf\u51e0\u4e2a\u5b64\u7acb\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">]<\/code>\u3002\u8fd9\u662f\u7ade\u4e89\uff1a\u4e09\u6761\u7ebf\u7a0b\u62a2\u7740\u5b8c\u6210\u540c\u4e00\u65b9\u6cd5\u3002sleep \u53ea\u662f\u8ba9\u73b0\u8c61\u7a33\u5b9a\u590d\u73b0\uff1b\u771f\u5b9e\u7ade\u4e89\u66f4\u590d\u6742\u3001\u66f4\u4e0d\u53ef\u9884\u77e5\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">call<\/code> \u524d\u52a0\u4e0a synchronized \u540e\uff0c\u67d0\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u6761\u7ebf\u7a0b\u80fd\u8fdb call\uff0c\u8f93\u51fa\u53d8\u6210\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">[Hello]\n[Synchronized]\n[World]<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u591a\u7ebf\u7a0b\u4e0b\u51e1\u662f\u64cd\u7eb5\u5bf9\u8c61\u5185\u90e8\u72b6\u6001\u7684\u65b9\u6cd5\uff0c\u90fd\u8981\u7528 synchronized \u9632\u7ade\u4e89\u3002\u4e00\u6761\u7ebf\u7a0b\u8fdb\u5165\u67d0\u5b9e\u4f8b\u7684\u540c\u6b65\u65b9\u6cd5\u540e\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4e0d\u80fd\u518d\u8fdb\u8be5\u5b9e\u4f8b\u7684\u540c\u6b65\u65b9\u6cd5\uff1b\u4f46\u8be5\u5b9e\u4f8b\u4e0a<strong style=\"color: #4870ac;\">\u672a\u540c\u6b65<\/strong>\u7684\u65b9\u6cd5\u4ecd\u53ef\u88ab\u8c03\u7528\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u540c\u6b65\u8bed\u53e5\u5757<\/h2>\n<p style=\"margin: 1em 0px;\">\u7c7b\u4e0d\u662f\u4f60\u5199\u7684\u3001\u65b9\u6cd5\u4e5f\u6ca1\u52a0 synchronized \u65f6\uff0c\u628a\u8c03\u7528\u653e\u8fdb\u540c\u6b65\u5757\u5373\u53ef\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">synchronized (object) {\n    \/\/ statements to be synchronized\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">object \u662f\u88ab\u540c\u6b65\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u53ea\u6709\u4e00\u6761\u8bed\u53e5\u65f6\u82b1\u62ec\u53f7\u53ef\u7701\u7565\u3002\u540c\u6b65\u5757\u4fdd\u8bc1\uff1a\u53ea\u6709\u5f53\u524d\u7ebf\u7a0b\u6210\u529f\u8fdb\u5165 object \u7684\u7ba1\u7a0b\u540e\uff0c\u624d\u80fd\u8c03\u5b83\u7684\u6210\u5458\u65b9\u6cd5\u3002\u628a call \u4fdd\u6301\u975e\u540c\u6b65\uff0c\u5728 Caller.run \u91cc\u9501 target\uff0c\u6548\u679c\u4e0e\u7ed9 call \u52a0 synchronized \u76f8\u540c\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public void run() {\n    synchronized (target) {\n        target.call(msg);\n    }\n}<\/code><\/pre>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u5171\u4eab\u5bf9\u8c61\u7528\u7ba1\u7a0b\u4e92\u65a5\uff1b\u80fd\u6539\u6e90\u7801\u5c31\u7ed9\u65b9\u6cd5\u52a0 synchronized\uff0c\u6539\u4e0d\u4e86\u5c31\u628a\u8c03\u7528\u5305\u8fdb synchronized(obj)\uff1b\u672a\u540c\u6b65\u65b9\u6cd5\u6321\u4e0d\u4f4f\u7ade\u4e89\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7ebf\u7a0b\u8981\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u5fc5\u987b\u4fdd\u8bc1\u67d0\u4e00\u523b\u53ea\u6709\u4e00\u6761\u7ebf\u7a0b\u5360\u7528\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53eb\u540c\u6b65\uff08synchronization [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[178],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/235"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=235"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}