{"id":78848,"date":"2026-02-28T12:17:19","date_gmt":"2026-02-28T04:17:19","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78848.html"},"modified":"2026-02-28T12:17:19","modified_gmt":"2026-02-28T04:17:19","slug":"02%e3%80%81javaee-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e4%ba%8c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78848.html","title":{"rendered":"02\u3001JAVAEE--\u591a\u7ebf\u7a0b(\u4e8c)"},"content":{"rendered":"<h5 id=\"%E7%BB%A7%E7%BB%AD%E5%AD%A6%E4%B9%A0%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E7%9F%A5%E8%AF%86~%F0%9F%98%8A\">\u7ee7\u7eed\u5b66\u4e60\u591a\u7ebf\u7a0b\u7684\u77e5\u8bc6~&#x1f60a;<\/h5>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"300\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260228041717-69a26c4d7c469.jpg\" width=\"300\" \/><\/p>\n<hr \/>\n<h2 id=\"%E4%B8%80%E3%80%81%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E9%97%AE%E9%A2%98\">\u4e00\u3001\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/h2>\n<h3 id=\"1%E3%80%81%E9%80%A0%E6%88%90%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E4%BA%94%E5%A4%A7%E5%8E%9F%E5%9B%A0\">1\u3001\u9020\u6210\u7ebf\u7a0b\u5b89\u5168\u7684\u4e94\u5927\u539f\u56e0&#x1f928;<\/h3>\n<h4 id=\"%E2%91%A0%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%9A%84%E9%9A%8F%E6%9C%BA%E8%B0%83%E5%BA%A6(%E6%A0%B9%E6%9C%AC%E5%8E%9F%E5%9B%A0)\">\u2460\u64cd\u4f5c\u7cfb\u7edf\u7684\u968f\u673a\u8c03\u5ea6(\u6839\u672c\u539f\u56e0)<\/h4>\n<h4 id=\"%E2%91%A1%E4%B8%A4%E4%B8%AA%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%97%B6%E4%BF%AE%E6%94%B9%E5%90%8C%E4%B8%80%E4%B8%AA%E5%8F%98%E9%87%8F\">\u2461\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u540c\u4e00\u4e2a\u53d8\u91cf<\/h4>\n<h4 id=\"%E2%91%A2%E4%BF%AE%E6%94%B9%E5%8F%98%E9%87%8F%E6%93%8D%E4%BD%9C%E4%B8%8D%E6%98%AF%E5%8E%9F%E5%AD%90%E7%9A%84\">\u2462\u4fee\u6539\u53d8\u91cf\u64cd\u4f5c\u4e0d\u662f\u539f\u5b50\u7684<\/h4>\n<h4 id=\"%E2%91%A3%E5%86%85%E5%AD%98%E5%8F%AF%E8%A7%81%E6%80%A7%E9%97%AE%E9%A2%98\">\u2463\u5185\u5b58\u53ef\u89c1\u6027\u95ee\u9898<\/h4>\n<h4 id=\"%E2%91%A4%E6%8C%87%E4%BB%A4%E9%87%8D%E6%8E%92%E5%BA%8F%E9%97%AE%E9%A2%98\">\u2464\u6307\u4ee4\u91cd\u6392\u5e8f\u95ee\u9898<\/h4>\n<h4 id=\"%E7%AC%AC%E2%91%A0%E8%B7%9F%E7%AC%AC%E2%91%A1%E6%88%91%E4%BB%AC%E6%97%A0%E6%B3%95%E7%9B%B4%E6%8E%A5%E8%A7%A3%E5%86%B3%EF%BC%8C%E4%BA%8E%E6%98%AF%E5%BE%80%E5%BE%80%E4%BB%8E%E7%AC%AC%E2%91%A2%E7%A7%8D%E7%9D%80%E6%89%8B\">\u7b2c\u2460\u8ddf\u7b2c\u2461\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u89e3\u51b3&#xff0c;\u4e8e\u662f\u5f80\u5f80\u4ece\u7b2c\u2462\u79cd\u7740\u624b<\/h4>\n<hr \/>\n<h3 id=\"2%E3%80%81synchronized%E7%9A%84%E7%89%B9%E6%80%A7\">2\u3001synchronized\u7684\u7279\u6027<\/h3>\n<h4 id=\"%E5%BC%95%E5%85%A5synchronized%E6%9C%AC%E8%B4%A8%E6%98%AF%E9%80%9A%E8%BF%87%E4%B8%A4%E4%B8%AA%E7%BA%BF%E7%A8%8B%E4%B9%8B%E9%97%B4%E4%BA%A7%E7%94%9F%E9%94%81%E7%AB%9E%E4%BA%89%E6%9D%A5%E7%A1%AE%E4%BF%9D%E5%9C%A8%E5%90%8C%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E4%B8%8B%E7%9A%84%E4%BF%AE%E6%94%B9%E6%93%8D%E4%BD%9C%E6%98%AF%E5%8E%9F%E5%AD%90%E7%9A%84%EF%BC%88%E5%8C%85%E6%8B%AC%E8%AF%BB%E6%94%B9%E5%86%99%E4%B8%89%E6%AD%A5%EF%BC%89\">\u5f15\u5165synchronized\u672c\u8d28\u662f\u901a\u8fc7\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4ea7\u751f\u9501\u7ade\u4e89\u6765\u786e\u4fdd\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e0b\u7684\u4fee\u6539\u64cd\u4f5c\u662f<span style=\"color:#ff9900\">\u539f\u5b50\u7684<\/span>&#xff08;\u5305\u62ec\u8bfb\u6539\u5199\u4e09\u6b65&#xff09;<\/h4>\n<h4 id=\"%E5%8F%A6%E5%A4%96synchronized%E6%9C%AC%E8%BA%AB%E8%BF%98%E5%85%B7%E6%9C%89%E5%87%A0%E4%B8%AA%E9%87%8D%E8%A6%81%E7%89%B9%E6%80%A7%EF%BC%9A\">\u53e6\u5916synchronized\u672c\u8eab\u8fd8\u5177\u6709\u51e0\u4e2a\u91cd\u8981\u7279\u6027&#xff1a;<\/h4>\n<h4 id=\"%E2%91%A0%E4%BA%92%E6%96%A5%E6%80%A7\">\u2460\u4e92\u65a5\u6027<\/h4>\n<h5 id=\"%E5%90%8C%E4%B8%80%E6%97%B6%E5%88%BB%EF%BC%8C%E5%8F%AA%E8%83%BD%E6%9C%89%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E6%8B%A5%E6%9C%89%E8%BF%99%E6%8A%8A%E9%94%81%F0%9F%99%82\">\u540c\u4e00\u65f6\u523b&#xff0c;\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u62e5\u6709\u8fd9\u628a\u9501&#x1f642;<\/h5>\n<h6 id=\"%E8%BF%87%E7%A8%8B%E5%A6%82%E4%B8%8B%EF%BC%9A\">\u8fc7\u7a0b\u5982\u4e0b&#xff1a;<\/h6>\n<h6 id=\"%E7%BA%BF%E7%A8%8BA%E8%8E%B7%E5%BE%97%E9%94%81---%E7%BA%BF%E7%A8%8BB%E9%98%BB%E5%A1%9E%E7%AD%89%E5%BE%85---%E7%BA%BF%E7%A8%8BA%E9%87%8A%E6%94%BE%E9%94%81---%E7%BA%BF%E7%A8%8BB%E8%8E%B7%E5%BE%97%E9%94%81\">\u7ebf\u7a0bA\u83b7\u5f97\u9501&#8212;\u7ebf\u7a0bB\u963b\u585e\u7b49\u5f85&#8212;\u7ebf\u7a0bA\u91ca\u653e\u9501&#8212;\u7ebf\u7a0bB\u83b7\u5f97\u9501<\/h6>\n<p>package JavaEE;<\/p>\n<p>public class Demo13 {<br \/>\n    private static int sum;<\/p>\n<p>\/\/synchronized\u7684\u4e92\u65a5\u6027,\u7ebf\u7a0b1\u62ff\u5230\u9501\u7ebf\u7a0b2\u518d\u7533\u8bf7\u5c31\u4f1a\u963b\u585e<br \/>\n    public static void main(String[] args) throws InterruptedException {<br \/>\n        \/\/\u5565\u7c7b\u578b\u7684\u9501\u90fd\u53ef\u4ee5<br \/>\n        Object locke &#061; new Object();<\/p>\n<p>        Thread t1 &#061; new Thread(()-&gt;{<br \/>\n            for (int i &#061; 0; i &lt; 50000; i &#043;&#043;) {<br \/>\n                \/\/\u53ea\u5bf9sum\u52a0\u4e86\u9501&#xff0c;\u5916\u90e8\u7684\u8fd8\u662f\u53ef\u4ee5\u5e76\u53d1\u6267\u884c<br \/>\n                \/\/\u672c\u8d28\u662f\u5f15\u5165\u4e86\u9501\u7ade\u4e89<br \/>\n                synchronized (locke) {<br \/>\n                    \/\/\u770b\u4f3c\u53ea\u6709\u4e00\u6b65\u64cd\u4f5c&#xff0c;\u5728cpu\u4e0a\u9762\u6709\u4e09\u6b65<br \/>\n                    \/\/\u52a0\u9501\u7684\u672c\u8d28\u76ee\u7684\u5c31\u662f\u786e\u4fddsum&#043;&#043;\u7684\u539f\u5b50\u6027&#xff0c;<br \/>\n                          \/\/\u786e\u4fdd\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5b83\u8fdb\u884c\u64cd\u4f5c\u3002<br \/>\n                    sum&#043;&#043;;<br \/>\n                }<br \/>\n            }<br \/>\n        });<\/p>\n<p>        Thread t2 &#061; new Thread(()-&gt;{<br \/>\n            for (int i &#061; 0; i &lt; 50000; i&#043;&#043;) {<br \/>\n                synchronized (locke) {<br \/>\n                    sum&#043;&#043;;<br \/>\n                }<br \/>\n            }<br \/>\n        });<\/p>\n<p>        t1.start();<br \/>\n        t2.start();<br \/>\n        t1.join();<br \/>\n        t2.join();<\/p>\n<p>        System.out.println(sum);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<hr \/>\n<h4 id=\"%E2%91%A1%E5%8F%AF%E9%87%8D%E5%85%A5%E6%80%A7\">\u2461\u53ef\u91cd\u5165\u6027<\/h4>\n<h6 id=\"%E5%B0%B1%E6%98%AF%E5%9C%A8%E5%AF%B9%E5%90%8C%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E5%86%8D%E6%AC%A1%E5%8A%A0%E9%94%81%E6%97%B6%EF%BC%8C%E4%B8%8D%E4%BC%9A%E4%BA%A7%E7%94%9F%E6%AD%BB%E9%94%81%F0%9F%98%80\">\u5c31\u662f\u5728\u5bf9\u540c\u4e00\u4e2a\u7ebf\u7a0b\u518d\u6b21\u52a0\u9501\u65f6&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u6b7b\u9501&#x1f600;<\/h6>\n<p>package JavaEE;<\/p>\n<p>public class Demo15 {<br \/>\n    \/\/synchronized\u7684\u53ef\u91cd\u5165\u6027,\u4f7f\u8fd9\u91cc\u4e0d\u4f1a\u6b7b\u9501<br \/>\n    public static void main(String[] args) {<br \/>\n        Object locke1 &#061; new Object();<\/p>\n<p>        Thread t &#061; new Thread(()-&gt;{<br \/>\n            synchronized (locke1){<br \/>\n                synchronized (locke1){<br \/>\n                    System.out.println(&#034;hh&#034;);<br \/>\n                }<br \/>\n            }<br \/>\n        });<\/p>\n<p>        t.start();<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h3 id=\"3%E3%80%81%E6%AD%BB%E9%94%81%E7%9A%84%E5%9B%9B%E5%A4%A7%E5%BF%85%E8%A6%81%E6%9D%A1%E4%BB%B6%EF%BC%88%E9%9D%A2%E8%AF%95%E9%87%8D%E7%82%B9%EF%BC%89%F0%9F%98%AE\">3\u3001\u6b7b\u9501\u7684\u56db\u5927\u5fc5\u8981\u6761\u4ef6<span style=\"color:#ff9900\">&#xff08;\u9762\u8bd5\u91cd\u70b9&#xff09;&#x1f62e;<\/span><\/h3>\n<h6 id=\"%E5%90%8C%E6%97%B6%E6%BB%A1%E8%B6%B3%E4%BB%A5%E4%B8%8B%E5%9B%9B%E7%A7%8D%E6%83%85%E5%86%B5%E6%89%8D%E4%BC%9A%E4%BA%A7%E7%94%9F%E6%AD%BB%E9%94%81\">\u540c\u65f6\u6ee1\u8db3\u4ee5\u4e0b\u56db\u79cd\u60c5\u51b5\u624d\u4f1a\u4ea7\u751f\u6b7b\u9501<\/h6>\n<h4 id=\"%E2%91%A0%E4%BA%92%E6%96%A5%E6%80%A7(sychronized%E5%85%B7%E5%A4%87)\">\u2460\u4e92\u65a5\u6027(sychronized\u5177\u5907)<\/h4>\n<h4 id=\"%E2%91%A1%E4%B8%8D%E5%8F%AF%E8%A2%AB%E6%8A%A2%E5%8D%A0(sychronized%E5%85%B7%E5%A4%87)\">\u2461\u4e0d\u53ef\u88ab\u62a2\u5360(sychronized\u5177\u5907)<\/h4>\n<h6 id=\"%E6%9C%89%E7%9A%84%E7%BA%BF%E7%A8%8B%E5%8F%AF%E8%83%BD%E4%B8%8D%E4%BC%9A%E9%98%BB%E5%A1%9E%EF%BC%8C%E7%9B%B4%E6%8E%A5%E6%8A%8A%E9%94%81%E6%8A%A2%E8%BF%87%E6%9D%A5\">\u6709\u7684\u7ebf\u7a0b\u53ef\u80fd\u4e0d\u4f1a\u963b\u585e&#xff0c;\u76f4\u63a5\u628a\u9501\u62a2\u8fc7\u6765<\/h6>\n<h4 id=\"%E2%91%A2%E4%BF%9D%E6%8C%81%E5%86%8D%E8%AF%B7%E6%B1%82\">\u2462\u4fdd\u6301\u518d\u8bf7\u6c42<\/h4>\n<h6 id=\"%E7%BA%BF%E7%A8%8B1%E5%9C%A8%E6%8C%81%E6%9C%89%E9%94%81A%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%EF%BC%8C%E5%90%8C%E6%97%B6%E8%AF%B7%E6%B1%82%E9%94%81B\">\u7ebf\u7a0b1\u5728\u6301\u6709\u9501A\u7684\u60c5\u51b5\u4e0b&#xff0c;\u540c\u65f6\u8bf7\u6c42\u9501B<\/h6>\n<p>\u6ce8\u610f&#xff1a;1\u3001\u5982\u679c\u901a\u7bc7\u53ea\u6709\u4e00\u628a\u9501&#xff0c;\u5e76\u4e14\u5177\u5907\u53ef\u91cd\u5165\u6027&#xff0c;\u4e0d\u4f1a\u6b7b\u9501<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a02\u3001\u5982\u679c\u591a\u628a\u9501\u4e4b\u95f4\u4e0d\u8fdb\u884c\u5d4c\u5957&#xff0c;\u4e0d\u4f1a\u6b7b\u9501<\/p>\n<h4 id=\"%E2%91%A3%E5%BE%AA%E7%8E%AF%E7%AD%89%E5%BE%85\">\u2463\u5faa\u73af\u7b49\u5f85<\/h4>\n<h6 id=\"%E5%9C%A8%E6%BB%A1%E8%B6%B3%E4%BF%9D%E6%8C%81%E5%86%8D%E8%AF%B7%E6%B1%82%E7%9A%84%E6%9D%A1%E4%BB%B6%E4%B8%8B%EF%BC%8C%E7%AD%89%E5%BE%85%E9%94%81%E7%9A%84%E9%A1%BA%E5%BA%8F%E5%87%BA%E7%8E%B0%E4%BA%86%E5%BE%AA%E7%8E%AF\">\u5728\u6ee1\u8db3\u4fdd\u6301\u518d\u8bf7\u6c42\u7684\u6761\u4ef6\u4e0b&#xff0c;\u7b49\u5f85\u9501\u7684\u987a\u5e8f\u51fa\u73b0\u4e86\u5faa\u73af<\/h6>\n<p>package JavaEE;<\/p>\n<p>\/\/\u6700\u7b80\u5355\u7684\u6f14\u793a\u6b7b\u9501\u65b9\u5f0f,\u91cd\u70b9\u8bb0\u5fc6\u4e00\u4e0b<br \/>\npublic class Demo14 {<br \/>\n    public static void main(String[] args) throws InterruptedException {<br \/>\n        Object locke1 &#061; new Object();<br \/>\n        Object locke2 &#061; new Object();<\/p>\n<p>        Thread t1 &#061; new Thread(()-&gt;{<br \/>\n            \/\/\u6ce8\u610f&#xff0c;\u8fd9\u91cc\u8981\u6f14\u793a\u6b7b\u9501\u7684\u8bdd\u9700\u8981\u5d4c\u5957&#xff0c;\u4e24\u4e2a\u9501\u5d4c\u5957\u8d77\u6765,<br \/>\n              \/\/\u5426\u5219\u5c31\u610f\u5473\u7740\u5728\u4e0d\u540c\u65f6\u95f4\u83b7\u53d6\u9501&#xff0c;\u800c\u4e0d\u662f\u540c\u65f6\u6301\u6709\u4e24\u4e2a\u9501.<br \/>\n                synchronized (locke1) {<br \/>\n                    try {<br \/>\n            \/\/\u4f7f\u7528sleep\u4f11\u7720\u786e\u4fddt2\u4e5f\u62ff\u5230\u4e86\u9501<br \/>\n                        Thread.sleep(1000);<br \/>\n                    } catch (InterruptedException e) {<br \/>\n                        throw new RuntimeException(e);<br \/>\n                    }<br \/>\n                    synchronized (locke2) {<br \/>\n                        System.out.println(&#034;t1&#034;);<br \/>\n                    }<br \/>\n                }<br \/>\n        });<\/p>\n<p>        Thread t2 &#061; new Thread(()-&gt;{<br \/>\n                synchronized (locke2) {<br \/>\n                    try {<br \/>\n                        Thread.sleep(1000);<br \/>\n                    } catch (InterruptedException e) {<br \/>\n                        throw new RuntimeException(e);<br \/>\n                    }<br \/>\n                    synchronized (locke1) {<br \/>\n                        System.out.println(&#034;t2&#034;);<br \/>\n                    }<br \/>\n                }<br \/>\n        });<\/p>\n<p>        t1.start();<br \/>\n        t2.start();<br \/>\n        t1.join();<br \/>\n        t2.join();<\/p>\n<p>    }<br \/>\n}<\/p>\n<hr \/>\n<h3 id=\"4%E3%80%81%E6%AD%BB%E9%94%81%E7%9A%84%E4%B8%89%E5%A4%A7%E5%9C%BA%E6%99%AF\">4\u3001\u6b7b\u9501\u7684\u4e09\u5927\u573a\u666f<\/h3>\n<h4 id=\"%E2%91%A0%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E4%B8%80%E6%8A%8A%E9%94%81\">\u2460\u4e00\u4e2a\u7ebf\u7a0b\u4e00\u628a\u9501<\/h4>\n<p>\u53ef\u91cd\u5165\u7684\u9501\u53ef\u4ee5\u89e3\u51b3<\/p>\n<h4 id=\"%E2%91%A1%E4%B8%A4%E4%B8%AA%E7%BA%BF%E7%A8%8B%E4%B8%A4%E6%8A%8A%E9%94%81\">\u2461\u4e24\u4e2a\u7ebf\u7a0b\u4e24\u628a\u9501<\/h4>\n<p>\u4e0a\u9762\u6700\u8fd1\u7684\u8fd9\u4e2a\u793a\u4f8b<\/p>\n<h4 id=\"%E2%91%A2M%E4%B8%AA%E7%BA%BF%E7%A8%8BN%E6%8A%8A%E9%94%81\">\u2462M\u4e2a\u7ebf\u7a0bN\u628a\u9501<\/h4>\n<p>\u54f2\u5b66\u5bb6\u5c31\u9910\u95ee\u9898&#xff1a;\u4e00\u4e2a\u4e2a\u5c31\u9910\u65f6\u6b63\u5e38&#xff0c;\u540c\u65f6\u5c31\u9910\u65f6\u5c31\u4f1a\u51fa\u73b0\u6b7b\u9501<\/p>\n<hr \/>\n<h3 id=\"5%E3%80%81wait()%E4%B8%8Enotify()\">5\u3001wait()\u4e0enotify()\u65b9\u6cd5<\/h3>\n<h4 id=\"wait()%E4%B8%BB%E8%A6%81%E8%B5%B7%E5%88%B0%E6%8E%A7%E5%88%B6%E7%BA%BF%E7%A8%8B%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F%E7%9A%84%E4%BD%9C%E7%94%A8\">wait()\u4e3b\u8981\u8d77\u5230\u63a7\u5236\u7ebf\u7a0b\u6267\u884c\u987a\u5e8f\u7684\u4f5c\u7528<\/h4>\n<h4 id=\"wait%E4%B8%8Esleep%E7%9A%84%E4%B8%89%E7%82%B9%E5%8C%BA%E5%88%AB%EF%BC%9A\">wait\u4e0esleep\u7684\u4e09\u70b9\u533a\u522b&#xff1a;<\/h4>\n<h4 id=\"1%E3%80%81wait%E4%BD%BF%E7%94%A8%E5%90%8E%E9%87%8A%E6%94%BE%E9%94%81%EF%BC%8Csleep%E4%BD%BF%E7%94%A8%E5%90%8E%E4%BB%8D%E6%8C%81%E6%9C%89%E9%94%81\">1\u3001wait\u4f7f\u7528\u540e\u91ca\u653e\u9501&#xff0c;sleep\u4f7f\u7528\u540e\u4ecd\u6301\u6709\u9501<\/h4>\n<h4 id=\"2%E3%80%81wait%E5%8F%AA%E8%83%BD%E5%9C%A8synchronized%E4%B8%AD%E4%BD%BF%E7%94%A8%EF%BC%8Csleep%E4%BB%BB%E4%BD%95%E5%9C%B0%E6%96%B9%E9%83%BD%E5%8F%AF%E4%BB%A5\">2\u3001wait\u53ea\u80fd\u5728synchronized\u4e2d\u4f7f\u7528&#xff0c;sleep\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u4ee5<\/h4>\n<h4 id=\"3%E3%80%81wait%E5%8F%AF%E4%BB%A5%E8%A2%ABnotify%E5%94%A4%E9%86%92%EF%BC%8Csleep%E5%BF%85%E9%A1%BB%E8%A6%81%E7%AD%89%E4%BC%91%E7%9C%A0%E6%97%B6%E9%97%B4%E7%BB%93%E6%9D%9F\">3\u3001wait\u53ef\u4ee5\u88abnotify\u5524\u9192&#xff0c;sleep\u5fc5\u987b\u8981\u7b49\u4f11\u7720\u65f6\u95f4\u7ed3\u675f<\/h4>\n<p>package JavaEE;<\/p>\n<p>import java.util.Scanner;<\/p>\n<p>public class Demo17 {<br \/>\n    public static void main(String[] args) {<br \/>\n        Object locker &#061; new Object();<\/p>\n<p>        Thread t1 &#061; new Thread(()-&gt;{<\/p>\n<p>            synchronized (locker){<br \/>\n                System.out.println(&#034;t1wait\u4e4b\u524d&#034;);<br \/>\n                try {<br \/>\n                    \/\/t1\u901a\u8fc7wait\u91ca\u653e\u4e86\u9501&#xff0c;\u5e76\u8fdb\u5165\u7b49\u5f85<br \/>\n                    locker.wait();<br \/>\n                } catch (InterruptedException e) {<br \/>\n                    throw new RuntimeException(e);<br \/>\n                }<br \/>\n                System.out.println(&#034;t1wait\u4e4b\u540e&#034;);<br \/>\n            }<\/p>\n<p>        });<\/p>\n<p>        Thread t2 &#061; new Thread(()-&gt;{<\/p>\n<p>            synchronized (locker){<br \/>\n                System.out.println(&#034;t2wait\u4e4b\u524d&#034;);<br \/>\n                try {<br \/>\n                    \/\/t2\u901a\u8fc7wait\u91ca\u653e\u4e86\u9501&#xff0c;\u5e76\u8fdb\u5165\u7b49\u5f85<br \/>\n                    locker.wait();<br \/>\n                } catch (InterruptedException e) {<br \/>\n                    throw new RuntimeException(e);<br \/>\n                }<br \/>\n                System.out.println(&#034;t2wait\u4e4b\u540e&#034;);<br \/>\n            }<\/p>\n<p>        });<\/p>\n<p>        Thread t3 &#061; new Thread(()-&gt;{<br \/>\n                Scanner scanner &#061; new Scanner(System.in);<br \/>\n                System.out.println(&#034;\u8f93\u5165\u4efb\u610f\u5185\u5bb9\u7ed3\u675f\u7ebf\u7a0b&#xff1a;&#034;);<br \/>\n                scanner.next();<\/p>\n<p>                synchronized (locker){<br \/>\n              \/\/locker.notify();<br \/>\n                locker.notifyAll();<\/p>\n<p>            }\/\/\u4e00\u76f4\u7b49\u5230\u51fa\u4e86\u8fd9\u91cc&#xff0c;t3\u91ca\u653e\u9501&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u7ee7\u7eed\u8fdb\u884c<br \/>\n        });<\/p>\n<p>        t1.start();<br \/>\n        t2.start();<br \/>\n        t3.start();<br \/>\n    }<br \/>\n}<\/p>\n<h4 id=\"%E5%8F%A6%E5%A4%96wait%E5%90%8E%E5%8F%AF%E4%BB%A5%E8%AE%BE%E7%BD%AE%E8%B6%85%E6%97%B6%E6%97%B6%E9%97%B4%EF%BC%8C%E4%B8%8D%E4%BC%9A%E6%AD%BB%E7%AD%89%E3%80%82notifyAll%E5%8F%AF%E4%BB%A5%E5%94%A4%E9%86%92%E6%89%80%E6%9C%89%E7%BA%BF%E7%A8%8B%3D\">\u53e6\u5916wait\u540e\u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4&#xff0c;\u4e0d\u4f1a\u6b7b\u7b49\u3002notifyAll\u53ef\u4ee5\u5524\u9192\u6240\u6709\u7ebf\u7a0b<\/h4>\n<hr \/>\n<h3 id=\"6%E3%80%81%E5%AF%B9%E4%BA%8E%E2%91%A3%E5%92%8C%E2%91%A4\">\u00a0 6\u3001\u2463\u5185\u5b58\u53ef\u89c1\u6027\u95ee\u9898<span style=\"color:#ff9900\">(<\/span><span style=\"color:#ff9900\">\u9762\u8bd5\u91cd\u70b9)&#x1f62e;<\/span><\/h3>\n<h4 id=\"%E5%AF%B9%E4%BA%8E%E2%91%A3%E5%86%85%E5%AD%98%E5%8F%AF%E8%A7%81%E6%80%A7%E9%97%AE%E9%A2%98%EF%BC%8C%E7%AE%80%E5%8D%95%E8%AF%B4%E5%B0%B1%E6%98%AF%EF%BC%9A%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E4%BF%AE%E6%94%B9%E4%BA%86%E5%8F%98%E9%87%8F%EF%BC%8C%E8%80%8C%E5%8F%A6%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E7%9C%8B%E4%B8%8D%E5%88%B0%E8%BF%99%E4%B8%AA%E4%BF%AE%E6%94%B9\">\u5bf9\u4e8e\u2463\u5185\u5b58\u53ef\u89c1\u6027\u95ee\u9898<\/h4>\n<h4>\u7b80\u5355\u8bf4\u5c31\u662f&#xff1a;\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u4e86\u53d8\u91cf&#xff0c;\u800c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u770b\u4e0d\u5230\u8fd9\u4e2a\u4fee\u6539<\/h4>\n<h4>&#xff08;\u7f16\u8bd1\u5668\u5bf9\u8bfb\u7684\u4f18\u5316&#xff09;<\/h4>\n<h6 id=\"%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81%E5%A6%82%E4%B8%8B%EF%BC%9A\">\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/h6>\n<p>package JavaEE;<\/p>\n<p>import java.util.Scanner;<\/p>\n<p>public class Demo16 {<br \/>\n    \/\/\u53d1\u73b0\u53d8\u91cf\u5728\u7ebf\u7a0b1\u4e2d\u8bfb\u53d6&#xff0c;\u7ebf\u7a0b2\u4e2d\u4fee\u6539&#xff0c;\u52a0\u4e0avolatile\u5c31\u597d<br \/>\n    private static boolean flag &#061; true;<br \/>\n  \/\/private static volatile bollean flag &#061; true;<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        Thread t1 &#061; new Thread(()-&gt;{<\/p>\n<p>            while (flag){<br \/>\n                    \/\/Thread.sleep(1);<br \/>\n            }<\/p>\n<p>            System.out.println(&#034;\u7ebf\u7a0bt1\u7ed3\u675f&#034;);<br \/>\n        });<\/p>\n<p>        Thread t2 &#061; new Thread(()-&gt;{<br \/>\n            System.out.println(&#034;\u8f93\u5165\u4efb\u610f\u5185\u5bb9\u7ed3\u675f\u7ebf\u7a0bt1&#034;);<br \/>\n            Scanner scanner &#061; new Scanner(System.in);<br \/>\n            scanner.next();<br \/>\n            \/\/\u4fee\u6539\u7684\u662f\u5de5\u4f5c\u5185\u5b58\u4e2d\u7684\u53d8\u91cf,\u800c\u4e0d\u662f\u4e3b\u5b58\u91cc\u7684<br \/>\n            flag &#061; false;<\/p>\n<p>            System.out.println(&#034;flag &#061; &#034; &#043; flag);<br \/>\n        });<\/p>\n<p>        t1.start();<br \/>\n        t2.start();<br \/>\n    }<br \/>\n}<\/p>\n<h4 id=\"%E5%AE%83%E7%9A%84%E6%9C%AC%E8%B4%A8%E5%8E%9F%E7%90%86%E6%98%AF%EF%BC%9A\">\u5b83\u7684\u672c\u8d28\u539f\u7406\u662f&#xff1a;<\/h4>\n<h4 id=\"JMM(JAVA%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B)%E4%B8%BA%E4%BA%86%E6%8F%90%E9%AB%98%E6%80%A7%E8%83%BD%EF%BC%8C%E5%85%81%E8%AE%B8%E6%AF%8F%E4%B8%AA%E7%BA%BF%E7%A8%8B%E5%B0%86%E4%B8%BB%E5%86%85%E5%AD%98%E7%9A%84%E5%8F%98%E9%87%8F%E6%8B%B7%E8%B4%9D%E5%88%B0%E8%87%AA%E5%B7%B1%E7%9A%84%E5%B7%A5%E4%BD%9C%E5%86%85%E5%AD%98%E4%B8%AD%E6%93%8D%E4%BD%9C%E3%80%82%E7%BA%BF%E7%A8%8B%E7%9B%B4%E6%8E%A5%E8%AF%BB%E5%86%99%E5%B7%A5%E4%BD%9C%E5%86%85%E5%AD%98%EF%BC%8C%E4%B8%8D%E6%AF%8F%E6%AC%A1%E9%83%BD%E8%AE%BF%E9%97%AE%E4%B8%BB%E5%86%85%E5%AD%98%E3%80%82%E8%BF%99%E5%B0%B1%E5%AF%BC%E8%87%B4%EF%BC%9A%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E4%BF%AE%E6%94%B9%E4%BA%86%E5%B7%A5%E4%BD%9C%E5%86%85%E5%AD%98%E4%B8%AD%E7%9A%84%E5%8F%98%E9%87%8F%EF%BC%8C%E4%BD%86%E5%8F%A6%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E7%9C%8B%E4%B8%8D%E5%88%B0%E8%BF%99%E4%B8%AA%E4%BF%AE%E6%94%B9%E2%80%94%E2%80%94%E5%9B%A0%E4%B8%BA%E4%BF%AE%E6%94%B9%E8%BF%98%E6%B2%A1%E5%90%8C%E6%AD%A5%E5%9B%9E%E4%B8%BB%E5%86%85%E5%AD%98%EF%BC%8C%E6%88%96%E8%80%85%E8%AF%B4%E5%8F%A6%E4%B8%80%E4%B8%AA%E7%BA%BF%E7%A8%8B%E8%BF%98%E5%9C%A8%E8%AF%BB%E8%87%AA%E5%B7%B1%E5%B7%A5%E4%BD%9C%E5%86%85%E5%AD%98%E4%B8%AD%E7%9A%84%E6%97%A7%E5%80%BC%E3%80%82\">JMM(JAVA\u5185\u5b58\u6a21\u578b)\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd&#xff0c;\u5141\u8bb8\u6bcf\u4e2a\u7ebf\u7a0b\u5c06\u4e3b\u5185\u5b58\u7684\u53d8\u91cf\u62f7\u8d1d\u5230\u81ea\u5df1\u7684\u5de5\u4f5c\u5185\u5b58\u4e2d\u64cd\u4f5c\u3002\u7ebf\u7a0b\u76f4\u63a5\u8bfb\u5199\u5de5\u4f5c\u5185\u5b58&#xff0c;\u4e0d\u6bcf\u6b21\u90fd\u8bbf\u95ee\u4e3b\u5185\u5b58\u3002\u8fd9\u5c31\u5bfc\u81f4&#xff1a;\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u4e86\u5de5\u4f5c\u5185\u5b58\u4e2d\u7684\u53d8\u91cf&#xff0c;\u4f46\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u770b\u4e0d\u5230\u8fd9\u4e2a\u4fee\u6539&#8212;\u56e0\u4e3a\u4fee\u6539\u8fd8\u6ca1\u540c\u6b65\u56de\u4e3b\u5185\u5b58&#xff0c;\u6216\u8005\u8bf4\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8fd8\u5728\u8bfb\u81ea\u5df1\u5de5\u4f5c\u5185\u5b58\u4e2d\u7684\u65e7\u503c<\/h4>\n<h4 id=\"%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95%EF%BC%9A%E5%8A%A0%E5%85%A5volatile%E4%BF%AE%E9%A5%B0%E5%8F%98%E9%87%8F\">\u89e3\u51b3\u529e\u6cd5&#xff1a;\u52a0\u5165volatile\u4fee\u9970\u53d8\u91cf<\/h4>\n<h4 id=\"volatile%E7%9A%84%E4%BD%9C%E7%94%A8%E5%B0%B1%E6%98%AF%EF%BC%8C%E5%BC%BA%E5%88%B6%E8%A6%81%E6%B1%82%E7%BA%BF%E7%A8%8B%E6%AF%8F%E6%AC%A1%E8%AF%BB%E5%8F%96%E5%8F%98%E9%87%8F%E6%97%B6%E5%BF%85%E9%A1%BB%E4%BB%8E%E4%B8%BB%E5%86%85%E5%AD%98%E4%B8%AD%E8%AF%BB%E5%8F%96%EF%BC%8C%E6%AF%8F%E6%AC%A1%E4%BF%AE%E6%94%B9%E5%AE%8C%E5%8F%98%E9%87%8F%E4%BB%A5%E5%90%8E%E5%BF%85%E9%A1%BB%E7%AB%8B%E5%88%BB%E5%86%99%E5%9B%9E%E4%B8%BB%E5%86%85%E5%AD%98%EF%BC%8C%E4%B8%8D%E8%83%BD%E4%BD%BF%E7%94%A8%E5%B7%A5%E4%BD%9C%E5%8F%98%E9%87%8F%E4%B8%AD%E7%9A%84%E7%BC%93%E5%AD%98%E5%89%AF%E6%9C%AC\">volatile\u7684\u4f5c\u7528\u5c31\u662f&#xff0c;\u5f3a\u5236\u8981\u6c42\u7ebf\u7a0b\u6bcf\u6b21\u8bfb\u53d6\u53d8\u91cf\u65f6\u5fc5\u987b\u4ece\u4e3b\u5185\u5b58\u4e2d\u8bfb\u53d6&#xff0c;\u6bcf\u6b21\u4fee\u6539\u5b8c\u53d8\u91cf\u4ee5\u540e\u5fc5\u987b\u7acb\u523b\u5199\u56de\u4e3b\u5185\u5b58&#xff0c;\u4e0d\u80fd\u4f7f\u7528\u5de5\u4f5c\u53d8\u91cf\u4e2d\u7684\u7f13\u5b58\u526f\u672c<\/h4>\n<hr \/>\n<h2 id=\"%E4%BA%8C%E3%80%81%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E7%9A%84%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F%EF%BC%88%E9%9D%A2%E8%AF%95%E9%87%8D%E7%82%B9%EF%BC%89\">\u4e8c\u3001\u5355\u4f8b\u6a21\u5f0f<\/h2>\n<h3 id=\"1%E3%80%81%E9%A5%BF%E6%B1%89%E6%A8%A1%E5%BC%8F\">1\u3001\u997f\u6c49\u6a21\u5f0f<\/h3>\n<h4 id=\"%E2%80%A2%20%E7%89%B9%E7%82%B9\">\u2022 \u7279\u70b9&#xff1a;\u7c7b\u52a0\u8f7d\u65f6\u5c31\u521b\u5efa\u5b9e\u4f8b<\/h4>\n<h4>\u2022 \u4f18\u70b9&#xff1a;\u5b9e\u73b0\u7b80\u5355&#xff0c;\u5929\u751f\u7ebf\u7a0b\u5b89\u5168&#xff08;\u53ea\u6709\u8bfb\u64cd\u4f5c&#xff0c;\u6ca1\u6709\u5199\u64cd\u4f5c&#xff09;<\/h4>\n<h4>\u2022 \u7f3a\u70b9&#xff1a;\u5982\u679c\u4e00\u76f4\u6ca1\u7528\u5230&#xff0c;\u53ef\u80fd\u9020\u6210\u8d44\u6e90\u6d6a\u8d39<\/h4>\n<p>package JavaEE;<\/p>\n<p>    public class Singleton{<br \/>\n        private static Singleton singleton &#061; new Singleton();<\/p>\n<p>        \/\/\u4f7f\u7528\u9759\u6001\u7c7b&#xff0c;\u907f\u514d\u8fdb\u5165\u8c03\u7528\u65b9\u6cd5\u9700\u8981\u521b\u5efa\u5b9e\u4f8b&#xff0c;\u9677\u5165\u521b\u5efa\u5b9e\u4f8b\u65b9\u9762\u7684\u6b7b\u5faa\u73af<br \/>\n        \/\/\u76f4\u63a5\u4f7f\u7528\u7c7b\u540d\u5c31\u53ef\u4ee5\u8c03\u7528\u8be5\u65b9\u6cd5<br \/>\n        public static Singleton getSingleton(){<\/p>\n<p>            \/\/&#xff08;\u53ea\u6709\u8bfb\u64cd\u4f5c&#xff0c;\u6ca1\u6709\u5199\u64cd\u4f5c&#xff09;<br \/>\n            return singleton;<br \/>\n        }<\/p>\n<p>        \/\/\u4fdd\u8bc1\u79c1\u6709&#xff0c;\u5916\u90e8\u4e0d\u80fd\u521b\u5efa\u5b9e\u4f8b<br \/>\n        private Singleton(){};<br \/>\n}<\/p>\n<h3>2\u3001\u61d2\u6c49\u6a21\u5f0f<\/h3>\n<h4 id=\"%E2%80%A2%20%E7%89%B9%E7%82%B9\">\u2022 \u7279\u70b9&#xff1a;\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\u624d\u521b\u5efa\u5b9e\u4f8b<\/h4>\n<h4>\u2022 \u4f18\u70b9&#xff1a;\u6309\u9700\u52a0\u8f7d&#xff0c;\u8282\u7701\u8d44\u6e90<\/h4>\n<h4>\u2022 \u7f3a\u70b9&#xff1a;\u9700\u8981\u5904\u7406\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff08;\u6709\u5199\u64cd\u4f5c&#xff09;<\/h4>\n<p>package JavaEE;<\/p>\n<p>public class SingletonLazy {<br \/>\n    private static volatile SingletonLazy singletonLazy &#061; null;<\/p>\n<p>    public static SingletonLazy getSingletonLazy(){<\/p>\n<p>                if(singletonLazy &#061;&#061; null){<br \/>\n                    \/\/\u5199<br \/>\n                    singletonLazy &#061; new SingletonLazy();<br \/>\n                }<\/p>\n<p>        }<br \/>\n        \/\/\u8bfb<br \/>\n        return singletonLazy;<br \/>\n    }<\/p>\n<p>    private SingletonLazy(){};<br \/>\n}<\/p>\n<h3 id=\"%E5%AF%B9%E4%BA%8E%E2%91%A4%E5%86%85%E5%AD%98%E9%87%8D%E6%8E%92%E5%BA%8F%E9%97%AE%E9%A2%98%EF%BC%8C%E7%AE%80%E5%8D%95%E8%AF%B4%E5%B0%B1%E6%98%AF%E7%BC%96%E8%AF%91%E5%99%A8%E5%AF%B9%E5%86%99%E7%9A%84%E4%BC%98%E5%8C%96\">3\u3001\u7ebf\u7a0b\u5b89\u5168\u7684\u5355\u4f8b\u6a21\u5f0f<span style=\"color:#ff9900\">&#xff08;\u9762\u8bd5\u91cd\u70b9&#xff09;&#x1f642;<\/span><\/h3>\n<h4>\u5355\u4ece\u2464\u5185\u5b58\u91cd\u6392\u5e8f\u95ee\u9898\u4e0a\u6765\u770b<\/h4>\n<h4>\u7b80\u5355\u8bf4\u5c31\u662f&#xff1a;&#xff08;\u7f16\u8bd1\u5668\u5bf9\u5199\u7684\u4f18\u5316&#xff09;<\/h4>\n<p>package JavaEE;<\/p>\n<p>public class SingletonLazy {<br \/>\n    private static volatile SingletonLazy singletonLazy &#061; null;<br \/>\n    private static Object locker &#061; new Object();<\/p>\n<p>    public static SingletonLazy getSingletonLazy(){<\/p>\n<p>        \/\/\u7b2c\u4e00\u6b21\u68c0\u67e5&#xff1a;\u5b9e\u4f8b\u662f\u5426\u5df2\u7ecf\u521b\u5efa&#xff1f;(\u4e0d\u7528\u9501&#xff0c;\u63d0\u9ad8\u6027\u80fd)<br \/>\n        \/\/\u4f5c\u7528&#xff1a;\u89c4\u907f\u4e0d\u5fc5\u8981\u7684\u52a0\u9501\u64cd\u4f5c<br \/>\n        if(singletonLazy &#061;&#061; null){<br \/>\n            \/\/\u52a0\u9501&#xff0c;\u5f15\u8d77\u9501\u7ade\u4e89\u8fd9\u6837\u9632\u6b62\u540c\u4e00\u65f6\u95f4\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b<br \/>\n            synchronized (locker){<br \/>\n               \/\/\u7b2c\u4e8c\u6b21\u68c0\u67e5&#xff1a;\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u88ab\u521b\u5efa<br \/>\n               \/\/\u4f5c\u7528&#xff1a;\u9632\u6b62\u591a\u7ebf\u7a0b\u4e0b\u4e00\u4e2a\u7ebf\u7a0b\u521b\u5efa\u5b8c&#xff0c;\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53c8\u521b\u5efa<br \/>\n                if(singletonLazy &#061;&#061; null){<br \/>\n                    \/\/\u5728\u5355\u4f8b\u6a21\u5f0f\u4e0b&#xff0c;\u521b\u5efa\u552f\u4e00\u4e00\u4e2aSingletonLazy\u7c7b\u7684\u5b9e\u4f8b<br \/>\n                    \/\/volatile\u5173\u952e\u5b57\u89e3\u51b3\u6307\u4ee4\u91cd\u6392\u5e8f\u7684\u4f4d\u7f6e<br \/>\n                    \/\/new\u64cd\u4f5c\u975e\u539f\u5b50\u6027&#xff1a;\u5206\u914d\u5185\u5b58-\u521d\u59cb\u5316-\u8d4b\u503c<br \/>\n                    singletonLazy &#061; new SingletonLazy();<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        return singletonLazy;<br \/>\n    }<\/p>\n<p>    private SingletonLazy(){};<\/p>\n<p>}<\/p>\n<h4>\u5b83\u7684\u672c\u8d28\u539f\u7406\u662f&#xff1a;<\/h4>\n<h4 id=\"\">\u6307\u4ee4\u91cd\u6392\u5e8f\u9020\u6210\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;DCL(\u53cc\u91cd\u68c0\u67e5\u9501)\u5355\u4f8b\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5178\u578b\u4f8b\u5b50\u3002\u4e3b\u8981\u662f\u56e0\u4e3anew\u64cd\u4f5c\u4e0d\u662f\u539f\u5b50\u7684&#xff0c;\u5b83\u5305\u62ec<span style=\"color:#ff9900\">\u5206\u914d\u5185\u5b58&#xff0c;\u521d\u59cb\u5316&#xff0c;\u8d4b\u503c<\/span>\u4e09\u4e2a\u6b65\u9aa4\u3002\u5728\u6781\u7aef\u60c5\u51b5\u4e0b&#xff0c;\u7f16\u8bd1\u5668\u6216CPU\u4e3a\u4e86\u4f18\u5316\u5c06\u521d\u59cb\u5316\u548c\u8d4b\u503c\u91cd\u6392\u5e8f&#xff0c;\u5bfc\u81f4\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u62ff\u5230\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u5bf9\u8c61<\/h4>\n<h4>\u4e3e\u4e2a\u4f8b\u5b50&#034;&#x1f330;&#034;&#xff1a;<\/h4>\n<h4>1\u3001\u7ebf\u7a0bA\u8d70\u5230<\/h4>\n<h4><span style=\"color:#956fe7\">singletonLazy &#061; new SingletonLazy();<\/span>&#xff0c;\u56e0\u4e3a\u91cd\u6392\u5e8f&#xff0c;\u5b83\u5148\u8d4b\u503c\u4e86&#xff08;\u6b64\u65f6instance\u4e0d\u518d\u4e3anull&#xff0c;\u4f46\u6307\u5411\u7684\u662f\u4e00\u5757\u8fd8\u6ca1\u521d\u59cb\u5316\u7684\u5185\u5b58&#xff09;<\/h4>\n<h4>2\u3001\u7ebf\u7a0bB\u6070\u597d\u8fd9\u65f6\u8fdb\u6765\u68c0\u67e5\u00a0<\/h4>\n<h4><span style=\"color:#956fe7\">if(singletonLazy &#061;&#061; null)<\/span>&#8212;\u53d1\u73b0singlenLazy\u4e0d\u4e3anull &#xff01;<\/h4>\n<h4>3\u3001\u7ebf\u7a0bB\u76f4\u63a5\u8fd4\u56de\u5e76\u4f7f\u7528\u8fd9\u4e2asingletonLazy&#xff0c;\u7ed3\u679c\u8bbf\u95ee\u5230\u7684\u5bf9\u8c61\u91cc\u7684\u6570\u636e\u90fd\u662f\u9ed8\u8ba4\u503c&#xff08;\u6bd4\u5982int\u662f0&#xff0c;\u5bf9\u8c61\u5f15\u7528\u662fnull&#xff09;&#xff0c;\u7a0b\u5e8f\u51fa\u9519<\/h4>\n<h4>\u89e3\u51b3\u529e\u6cd5&#xff1a;\u52a0\u5165volatile\u4fee\u9970\u53d8\u91cf<\/h4>\n<h4>volatile\u7684\u6838\u5fc3\u4f5c\u7528\u5c31\u662f\u7981\u6b62\u4e86\u8fd9\u79cd\u91cd\u6392\u5e8f&#xff0c;\u4fdd\u8bc1\u4e86\u521d\u59cb\u5316\u64cd\u4f5c\u4e00\u5b9a\u5728\u8d4b\u503c\u4e4b\u524d\u4ea7\u751f&#xff0c;\u4ece\u800c\u4fdd\u8bc1\u6240\u6709\u7ebf\u7a0b\u770b\u5230\u7684\u90fd\u662f\u5b8c\u6574\u521d\u59cb\u5316\u7684\u5bf9\u8c61<\/h4>\n<hr \/>\n<h3>\u603b\u7ed3&#xff1a;<\/h3>\n<h4><span style=\"color:#ff9900\">\u9501&#xff08;synchronized&#xff09;<\/span>\u4fdd\u8bc1\u4e86\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u7684\u5b9e\u4f8b\u88ab\u521b\u5efa&#xff0c;\u89e3\u51b3\u4e86\u539f\u5b50\u6027\u95ee\u9898<\/h4>\n<h4><span style=\"color:#ff9900\">\u53cc\u91cdif<\/span>\u5224\u65ad\u51cf\u5c11\u4e86\u9501\u7ade\u4e89&#xff0c;\u63d0\u5347\u4e86\u6027\u80fd<\/h4>\n<h4><span style=\"color:#ff9900\">volatile<\/span>\u901a\u8fc7\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f&#xff0c;\u89e3\u51b3\u4e86\u5176\u4ed6\u7ebf\u7a0b\u770b\u5230&#039;\u534a\u6210\u54c1&#039;\u5bf9\u8c61\u7684\u95ee\u9898<\/h4>\n<h4>\u8fd9\u4e09\u8005\u5404\u53f8\u5176\u804c&#xff0c;\u5171\u540c\u4fdd\u8bc1\u4e86DCL\u5355\u4f8b\u7684\u5b89\u5168\u548c\u9ad8\u6548<\/h4>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260228041718-69a26c4e2f7e4.jpg\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ee7\u7eed\u5b66\u4e60\u591a\u7ebf\u7a0b\u7684\u77e5\u8bc6~&#x1f60a;\u4e00\u3001\u7ebf\u7a0b\u5b89\u5168\u95ee\u98981\u3001\u9020\u6210\u7ebf\u7a0b\u5b89\u5168\u7684\u4e94\u5927\u539f\u56e0&#x1f928;\u2460\u64cd\u4f5c\u7cfb\u7edf\u7684\u968f\u673a\u8c03\u5ea6(\u6839\u672c\u539f\u56e0)\u2461\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u540c\u4e00\u4e2a\u53d8\u91cf\u2462\u4fee\u6539\u53d8\u91cf\u64cd\u4f5c\u4e0d\u662f\u539f\u5b50\u7684\u2463\u5185\u5b58\u53ef\u89c1\u6027\u95ee\u9898\u2464\u6307\u4ee4\u91cd\u6392\u5e8f\u95ee\u9898\u7b2c\u2460\u8ddf\u7b2c\u2461\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u89e3\u51b3&#xff0c;\u4e8e\u662f\u5f80\u5f80\u4ece\u7b2c\u2462\u79cd\u7740\u624b2\u3001synchronized\u7684\u7279\u6027\u5f15\u5165synchronized\u672c\u8d28\u662f\u901a\u8fc7\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4ea7\u751f\u9501\u7ade\u4e89\u6765\u786e\u4fdd\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e0b\u7684\u4fee\u6539\u64cd\u4f5c\u662f\u539f\u5b50\u7684&#xff08;\u5305\u62ec\u8bfb\u6539\u5199\u4e09\u6b65&#xff09;\u53e6\u5916synchroniz<\/p>\n","protected":false},"author":2,"featured_media":78846,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,403,190],"topic":[],"class_list":["post-78848","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-javaee","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>02\u3001JAVAEE-\u591a\u7ebf\u7a0b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/78848.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"02\u3001JAVAEE-\u591a\u7ebf\u7a0b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u7ee7\u7eed\u5b66\u4e60\u591a\u7ebf\u7a0b\u7684\u77e5\u8bc6~&#x1f60a;\u4e00\u3001\u7ebf\u7a0b\u5b89\u5168\u95ee\u98981\u3001\u9020\u6210\u7ebf\u7a0b\u5b89\u5168\u7684\u4e94\u5927\u539f\u56e0&#x1f928;\u2460\u64cd\u4f5c\u7cfb\u7edf\u7684\u968f\u673a\u8c03\u5ea6(\u6839\u672c\u539f\u56e0)\u2461\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u540c\u4e00\u4e2a\u53d8\u91cf\u2462\u4fee\u6539\u53d8\u91cf\u64cd\u4f5c\u4e0d\u662f\u539f\u5b50\u7684\u2463\u5185\u5b58\u53ef\u89c1\u6027\u95ee\u9898\u2464\u6307\u4ee4\u91cd\u6392\u5e8f\u95ee\u9898\u7b2c\u2460\u8ddf\u7b2c\u2461\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u89e3\u51b3&#xff0c;\u4e8e\u662f\u5f80\u5f80\u4ece\u7b2c\u2462\u79cd\u7740\u624b2\u3001synchronized\u7684\u7279\u6027\u5f15\u5165synchronized\u672c\u8d28\u662f\u901a\u8fc7\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4ea7\u751f\u9501\u7ade\u4e89\u6765\u786e\u4fdd\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e0b\u7684\u4fee\u6539\u64cd\u4f5c\u662f\u539f\u5b50\u7684&#xff08;\u5305\u62ec\u8bfb\u6539\u5199\u4e09\u6b65&#xff09;\u53e6\u5916synchroniz\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78848.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-28T04:17:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260228041717-69a26c4d7c469.jpg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78848.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78848.html\",\"name\":\"02\u3001JAVAEE-\u591a\u7ebf\u7a0b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-28T04:17:19+00:00\",\"dateModified\":\"2026-02-28T04:17:19+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78848.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78848.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78848.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"02\u3001JAVAEE--\u591a\u7ebf\u7a0b(\u4e8c)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"02\u3001JAVAEE-\u591a\u7ebf\u7a0b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/78848.html","og_locale":"zh_CN","og_type":"article","og_title":"02\u3001JAVAEE-\u591a\u7ebf\u7a0b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u7ee7\u7eed\u5b66\u4e60\u591a\u7ebf\u7a0b\u7684\u77e5\u8bc6~&#x1f60a;\u4e00\u3001\u7ebf\u7a0b\u5b89\u5168\u95ee\u98981\u3001\u9020\u6210\u7ebf\u7a0b\u5b89\u5168\u7684\u4e94\u5927\u539f\u56e0&#x1f928;\u2460\u64cd\u4f5c\u7cfb\u7edf\u7684\u968f\u673a\u8c03\u5ea6(\u6839\u672c\u539f\u56e0)\u2461\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u540c\u4e00\u4e2a\u53d8\u91cf\u2462\u4fee\u6539\u53d8\u91cf\u64cd\u4f5c\u4e0d\u662f\u539f\u5b50\u7684\u2463\u5185\u5b58\u53ef\u89c1\u6027\u95ee\u9898\u2464\u6307\u4ee4\u91cd\u6392\u5e8f\u95ee\u9898\u7b2c\u2460\u8ddf\u7b2c\u2461\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u89e3\u51b3&#xff0c;\u4e8e\u662f\u5f80\u5f80\u4ece\u7b2c\u2462\u79cd\u7740\u624b2\u3001synchronized\u7684\u7279\u6027\u5f15\u5165synchronized\u672c\u8d28\u662f\u901a\u8fc7\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4ea7\u751f\u9501\u7ade\u4e89\u6765\u786e\u4fdd\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e0b\u7684\u4fee\u6539\u64cd\u4f5c\u662f\u539f\u5b50\u7684&#xff08;\u5305\u62ec\u8bfb\u6539\u5199\u4e09\u6b65&#xff09;\u53e6\u5916synchroniz","og_url":"https:\/\/www.wsisp.com\/helps\/78848.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-28T04:17:19+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260228041717-69a26c4d7c469.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78848.html","url":"https:\/\/www.wsisp.com\/helps\/78848.html","name":"02\u3001JAVAEE-\u591a\u7ebf\u7a0b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-28T04:17:19+00:00","dateModified":"2026-02-28T04:17:19+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78848.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78848.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78848.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"02\u3001JAVAEE--\u591a\u7ebf\u7a0b(\u4e8c)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=78848"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78848\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/78846"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78848"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}