{"id":54201,"date":"2025-08-12T23:49:47","date_gmt":"2025-08-12T15:49:47","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/54201.html"},"modified":"2025-08-12T23:49:47","modified_gmt":"2025-08-12T15:49:47","slug":"java%e5%9f%ba%e7%a1%80-%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/54201.html","title":{"rendered":"Java\u57fa\u7840-\u591a\u7ebf\u7a0b"},"content":{"rendered":"<h4>\u4e00\u3001\u7ebf\u7a0b\u521b\u5efa\u65b9\u5f0f&#xff08;\u4e24\u79cd\u5199\u6cd5&#xff0c;\u89e3\u51b3\u201c\u600e\u4e48\u5f00\u542f\u591a\u4efb\u52a1\u201d\u95ee\u9898 &#xff09;<\/h4>\n<p>Java \u91cc\u60f3\u8ba9\u7a0b\u5e8f\u540c\u65f6\u5e72\u591a\u4ef6\u4e8b&#xff08;\u6bd4\u5982\u4e00\u8fb9\u4e0b\u8f7d\u3001\u4e00\u8fb9\u64ad\u653e\u97f3\u4e50 &#xff09;&#xff0c;\u5c31\u5f97\u7528\u591a\u7ebf\u7a0b\u3002\u800c\u521b\u5efa\u7ebf\u7a0b&#xff0c;\u6700\u57fa\u7840\u7684\u5c31\u662f\u7ee7\u627f Thread \u7c7b\u548c\u5b9e\u73b0 Runnable \u63a5\u53e3\u8fd9\u4e24\u79cd\u65b9\u5f0f&#xff0c;\u672c\u8d28\u90fd\u662f\u5b9a\u4e49\u201c\u7ebf\u7a0b\u8981\u505a\u7684\u4e8b&#xff08;run() \u65b9\u6cd5 &#xff09;\u201d&#xff0c;\u518d\u542f\u52a8\u7ebf\u7a0b\u8ba9\u5b83\u6267\u884c\u3002<\/p>\n<h4>1. \u7ee7\u627f Thread \u7c7b\u65b9\u5f0f<\/h4>\n<p>\/\/ \u7b2c\u4e00\u6b65&#xff1a;\u81ea\u5b9a\u4e49\u7c7b\u7ee7\u627f Thread&#xff0c;\u91cd\u5199 run() \u65b9\u6cd5<br \/>\nclass MyThread extends Thread {<br \/>\n    \/\/ run() \u91cc\u5199\u7684\u662f\u201c\u8fd9\u4e2a\u7ebf\u7a0b\u8981\u6267\u884c\u7684\u4efb\u52a1\u201d<br \/>\n    &#064;Override<br \/>\n    public void run() {<br \/>\n        \/\/ \u8fd9\u91cc\u7b80\u5355\u6253\u5370\u7ebf\u7a0b\u540d\u79f0&#xff0c;\u5b9e\u9645\u53ef\u4ee5\u662f\u590d\u6742\u903b\u8f91&#xff08;\u6bd4\u5982\u6587\u4ef6\u4e0b\u8f7d\u3001\u6570\u636e\u5904\u7406\u7b49&#xff09;<br \/>\n        System.out.println(&#034;\u7ebf\u7a0b\u6267\u884c&#xff1a;&#034; &#043; Thread.currentThread().getName());<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u7b2c\u4e8c\u6b65&#xff1a;\u5728 main \u65b9\u6cd5&#xff08;\u4e3b\u7ebf\u7a0b&#xff09;\u91cc\u542f\u52a8\u7ebf\u7a0b<br \/>\npublic class Main {<br \/>\n    public static void main(String[] args) {<br \/>\n        \/\/ \u521b\u5efa\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u5bf9\u8c61<br \/>\n        MyThread t1 &#061; new MyThread();<br \/>\n        \/\/ \u542f\u52a8\u7ebf\u7a0b&#xff01;\u6ce8\u610f&#xff1a;\u5fc5\u987b\u7528 start()&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u8c03 run()&#xff01;<br \/>\n        \/\/ start() \u4f1a\u8ba9 JVM \u7ed9\u7ebf\u7a0b\u5206\u914d\u8d44\u6e90&#xff08;\u6bd4\u5982\u6808\u7a7a\u95f4&#xff09;&#xff0c;\u7136\u540e\u81ea\u52a8\u8c03\u7528 run() \u6267\u884c\u4efb\u52a1<br \/>\n        \/\/ \u5982\u679c\u76f4\u63a5\u8c03 run()&#xff0c;\u5c31\u548c\u666e\u901a\u65b9\u6cd5\u8c03\u7528\u4e00\u6837&#xff0c;\u4e0d\u4f1a\u5f00\u542f\u65b0\u7ebf\u7a0b&#xff0c;\u8fd8\u662f\u5728\u4e3b\u7ebf\u7a0b\u91cc\u6267\u884c<br \/>\n        t1.start();<br \/>\n    }<br \/>\n}<\/p>\n<ul>\n<li>\u5173\u952e\u70b9&#xff1a;\n<ul>\n<li>\u7ee7\u627f Thread \u540e&#xff0c;run() \u662f\u7ebf\u7a0b\u7684\u201c\u4efb\u52a1\u5165\u53e3\u201d&#xff0c;JVM \u4f1a\u5728\u542f\u52a8\u7ebf\u7a0b\u540e\u81ea\u52a8\u6267\u884c\u5b83\u3002<\/li>\n<li>start() \u662f\u771f\u6b63\u201c\u542f\u52a8\u65b0\u7ebf\u7a0b\u201d\u7684\u5173\u952e\u65b9\u6cd5&#xff0c;\u5b83\u4f1a\u89e6\u53d1 JVM \u7684\u7ebf\u7a0b\u8c03\u5ea6\u673a\u5236&#xff0c;\u7ed9\u7ebf\u7a0b\u5206\u914d CPU \u65f6\u95f4\u7247\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;Java \u662f\u5355\u7ee7\u627f&#xff0c;\u8981\u662f\u4f60\u7684\u7c7b\u5df2\u7ecf\u7ee7\u627f\u4e86\u5176\u4ed6\u7c7b&#xff08;\u6bd4\u5982 MyClass extends OtherClass &#xff09;&#xff0c;\u5c31\u6ca1\u6cd5\u518d\u7ee7\u627f Thread \u4e86&#xff0c;\u6240\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u7075\u6d3b\u6027\u7a0d\u5dee\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>2. \u5b9e\u73b0 Runnable \u63a5\u53e3\u65b9\u5f0f<\/h4>\n<p>\/\/ \u7b2c\u4e00\u6b65&#xff1a;\u81ea\u5b9a\u4e49\u7c7b\u5b9e\u73b0 Runnable \u63a5\u53e3&#xff0c;\u91cd\u5199 run() \u65b9\u6cd5<br \/>\nclass MyRunnable implements Runnable {<br \/>\n    &#064;Override<br \/>\n    public void run() {<br \/>\n        \/\/ \u540c\u6837&#xff0c;\u8fd9\u91cc\u5b9a\u4e49\u7ebf\u7a0b\u8981\u505a\u7684\u4e8b&#xff0c;\u6bd4\u5982\u6253\u5370\u7ebf\u7a0b\u540d<br \/>\n        System.out.println(&#034;\u7ebf\u7a0b\u6267\u884c&#xff1a;&#034; &#043; Thread.currentThread().getName());<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u7b2c\u4e8c\u6b65&#xff1a;\u5728 main \u65b9\u6cd5\u91cc&#xff0c;\u7528 Thread \u7c7b\u201c\u5305\u88c5\u201d Runnable \u5bf9\u8c61&#xff0c;\u518d\u542f\u52a8<br \/>\npublic class Main {<br \/>\n    public static void main(String[] args) {<br \/>\n        \/\/ \u521b\u5efa Runnable \u4efb\u52a1\u5bf9\u8c61&#xff08;\u53ea\u662f\u5b9a\u4e49\u4e86\u4efb\u52a1&#xff0c;\u8fd8\u6ca1\u7ebf\u7a0b\u6267\u884c\u5b83&#xff09;<br \/>\n        MyRunnable runnableTask &#061; new MyRunnable();<br \/>\n        \/\/ \u628a\u4efb\u52a1\u4e22\u7ed9 Thread&#xff0c;\u8ba9 Thread \u6765\u7ba1\u7406\u7ebf\u7a0b\u7684\u542f\u52a8\u3001\u8c03\u5ea6<br \/>\n        Thread t2 &#061; new Thread(runnableTask);<br \/>\n        \/\/ \u542f\u52a8\u7ebf\u7a0b&#xff0c;\u548c\u4e4b\u524d\u4e00\u6837&#xff0c;start() \u4f1a\u89e6\u53d1 JVM \u6267\u884c run()<br \/>\n        t2.start();<br \/>\n    }<br \/>\n}<\/p>\n<ul>\n<li>\u5173\u952e\u70b9&#xff1a;\n<ul>\n<li>Runnable \u662f\u4e2a\u63a5\u53e3&#xff0c;\u53ea\u89c4\u5b9a\u4e86 run() \u65b9\u6cd5&#xff0c;\u672c\u8eab\u4e0d\u6d89\u53ca\u7ebf\u7a0b\u7684\u201c\u542f\u52a8\u201d\u80fd\u529b\u3002\u6240\u4ee5\u5fc5\u987b\u501f\u52a9 Thread \u7c7b\u7684\u6784\u9020\u65b9\u6cd5&#xff0c;\u628a Runnable \u4efb\u52a1\u201c\u5305\u88c5\u201d\u8fdb\u53bb&#xff0c;\u624d\u80fd\u542f\u52a8\u7ebf\u7a0b\u3002<\/li>\n<li>\u4f18\u70b9&#xff1a;\u89e3\u51b3\u4e86\u5355\u7ee7\u627f\u9650\u5236&#xff01;\u6bd4\u5982MyRunnable \u8fd8\u80fd\u7ee7\u627f\u5176\u4ed6\u7c7b&#xff08;\u50cf class MyRunnable extends OtherClass implements Runnable &#xff09;&#xff0c;\u66f4\u7075\u6d3b&#xff1b;\u800c\u4e14&#xff0c;\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5171\u4eab\u540c\u4e00\u4e2a Runnable \u5bf9\u8c61&#xff08;\u6bd4\u5982\u591a\u4e2a\u7ebf\u7a0b\u4e00\u8d77\u5904\u7406\u540c\u4e00\u4e2a\u4efb\u52a1\u961f\u5217 &#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4e3e\u4e2a\u5171\u4eab\u7684\u4f8b\u5b50&#xff1a;<\/p>\n<p>MyRunnable sharedTask &#061; new MyRunnable();<br \/>\n\/\/ \u7ebf\u7a0b A \u548c \u7ebf\u7a0b B \u5171\u4eab\u540c\u4e00\u4e2a\u4efb\u52a1\u5bf9\u8c61<br \/>\nThread tA &#061; new Thread(sharedTask, &#034;\u7ebf\u7a0bA&#034;);<br \/>\nThread tB &#061; new Thread(sharedTask, &#034;\u7ebf\u7a0bB&#034;);<br \/>\ntA.start();<br \/>\ntB.start();<\/p>\n<p>\u8fd9\u6837&#xff0c;\u7ebf\u7a0b A \u548c B \u4f1a\u57fa\u4e8e\u540c\u4e00\u4e2a sharedTask \u6267\u884c\u4efb\u52a1&#xff0c;\u9002\u5408\u5904\u7406\u201c\u591a\u4e2a\u7ebf\u7a0b\u534f\u4f5c\u5b8c\u6210\u540c\u4e00\u4efd\u5de5\u4f5c\u201d\u7684\u573a\u666f&#xff08;\u6bd4\u5982\u5171\u540c\u5904\u7406\u4e00\u4e2a\u8ba1\u6570\u5668\u3001\u4e00\u4e2a\u6587\u4ef6\u5199\u5165\u64cd\u4f5c &#xff09;\u3002<\/p>\n<h4>\u4e8c\u3001\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f&#xff08;\u7406\u89e3\u7ebf\u7a0b\u201c\u4e00\u751f\u201d\u7684\u72b6\u6001\u53d8\u5316 &#xff09;<\/h4>\n<p>\u7ebf\u7a0b\u4ece\u521b\u5efa\u5230\u9500\u6bc1&#xff0c;\u4f1a\u7ecf\u5386\u4e00\u7cfb\u5217\u72b6\u6001\u53d8\u5316&#xff0c;\u56fe\u91cc\u603b\u7ed3\u7684\u6d41\u7a0b\u662f&#xff1a;<\/p>\n<p>\u65b0\u5efa&#xff08;New&#xff09;\u2192 \u53ef\u8fd0\u884c&#xff08;Runnable&#xff09;\u2192 \u8fd0\u884c&#xff08;Running&#xff09;\u2192&#xff08;\u963b\u585e\/\u7b49\u5f85\/\u8d85\u65f6\u7b49\u5f85&#xff09;\u2192 \u53ef\u8fd0\u884c \u2192 \u8fd0\u884c \u2192 \u7ec8\u6b62&#xff08;Terminated&#xff09;<\/p>\n<h4>1. \u65b0\u5efa&#xff08;New&#xff09;<\/h4>\n<ul>\n<li>\u573a\u666f&#xff1a;\u521a\u7528 new \u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61&#xff0c;\u8fd8\u6ca1\u8c03\u7528 start() \u7684\u65f6\u5019\u3002\u6bd4\u5982 MyThread t1 &#061; new MyThread(); \u8fd9\u4e00\u6b65&#xff0c;\u7ebf\u7a0b\u53ea\u662f\u4e2a\u201c\u7a7a\u58f3\u201d&#xff0c;JVM \u8fd8\u6ca1\u7ed9\u5b83\u5206\u914d\u771f\u6b63\u7684\u7ebf\u7a0b\u8d44\u6e90&#xff08;\u6bd4\u5982\u6808\u3001\u7a0b\u5e8f\u8ba1\u6570\u5668\u7b49 &#xff09;\u3002<\/li>\n<li>\u6b64\u65f6\u7a0b\u5e8f\u6ca1\u6709\u4efb\u4f55\u53d8\u5316&#xff0c;\u5fc5\u987b\u8c03 start() \u624d\u4f1a\u8fdb\u5165\u4e0b\u4e00\u6b65\u3002<\/li>\n<\/ul>\n<h4>2. \u53ef\u8fd0\u884c&#xff08;Runnable&#xff09;<\/h4>\n<ul>\n<li>\u573a\u666f&#xff1a;\u8c03\u7528 start() \u540e&#xff0c;\u7ebf\u7a0b\u8fdb\u5165\u8fd9\u4e2a\u72b6\u6001\u3002\u6b64\u65f6&#xff0c;\u7ebf\u7a0b\u5df2\u7ecf\u201c\u51c6\u5907\u597d\u6267\u884c\u4e86\u201d&#xff0c;\u4f46\u5f97\u7b49 CPU \u8c03\u5ea6&#xff08;JVM \u6709\u4e2a\u201c\u7ebf\u7a0b\u8c03\u5ea6\u5668\u201d&#xff0c;\u51b3\u5b9a\u54ea\u4e2a\u7ebf\u7a0b\u5148\u6267\u884c &#xff09;\u3002<\/li>\n<li>\u6ce8\u610f&#xff1a;\u201c\u53ef\u8fd0\u884c\u201d\u4e0d\u4ee3\u8868\u771f\u7684\u5728\u6267\u884c&#xff0c;\u53ea\u662f\u5728\u201c\u5c31\u7eea\u961f\u5217\u201d\u91cc\u6392\u961f&#xff0c;\u7b49 CPU \u65f6\u95f4\u7247\u3002\u6bd4\u5982\u7535\u8111\u540c\u65f6\u5f00\u4e86\u5f88\u591a\u7a0b\u5e8f&#xff0c;CPU \u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u5e72\u4e00\u4ef6\u4e8b&#xff08;\u591a\u6838 CPU \u53ef\u4ee5\u5e76\u884c&#xff0c;\u4f46\u8c03\u5ea6\u903b\u8f91\u66f4\u590d\u6742 &#xff09;&#xff0c;\u6240\u4ee5\u5f97\u6392\u961f\u3002<\/li>\n<\/ul>\n<h4>3. \u8fd0\u884c&#xff08;Running&#xff09;<\/h4>\n<ul>\n<li>\u573a\u666f&#xff1a;CPU \u9009\u4e2d\u4e86\u8fd9\u4e2a\u7ebf\u7a0b&#xff0c;\u5f00\u59cb\u6267\u884c run() \u65b9\u6cd5\u91cc\u7684\u4ee3\u7801&#xff0c;\u771f\u6b63\u201c\u5e72\u6d3b\u201d\u7684\u72b6\u6001\u3002<\/li>\n<li>\u80fd\u6301\u7eed\u591a\u4e45&#xff1a;\u53d6\u51b3\u4e8e CPU \u65f6\u95f4\u7247&#xff01;\u65f6\u95f4\u7247\u7528\u5b8c&#xff0c;\u7ebf\u7a0b\u4f1a\u56de\u5230\u53ef\u8fd0\u884c\u72b6\u6001&#xff0c;\u91cd\u65b0\u6392\u961f&#xff1b;\u5982\u679c run() \u91cc\u7684\u4ee3\u7801\u5f88\u5feb\u6267\u884c\u5b8c&#xff08;\u6bd4\u5982\u5c31\u6253\u5370\u4e00\u53e5\u8bdd &#xff09;&#xff0c;\u4e5f\u4f1a\u76f4\u63a5\u8fdb\u5165\u7ec8\u6b62\u72b6\u6001\u3002<\/li>\n<\/ul>\n<h4>4. \u963b\u585e\/\u7b49\u5f85\/\u8d85\u65f6\u7b49\u5f85&#xff08;Blocked \/ Waiting \/ Timed Waiting&#xff09;<\/h4>\n<p>\u8fd9\u4e09\u7c7b\u72b6\u6001\u90fd\u662f\u7ebf\u7a0b\u201c\u6682\u505c\u6267\u884c\u201d\u7684\u60c5\u51b5&#xff0c;\u539f\u56e0\u4e0d\u540c&#xff0c;\u5904\u7406\u65b9\u5f0f\u4e5f\u4e0d\u540c&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u963b\u585e&#xff08;Blocked&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u573a\u666f&#xff1a;\u7ebf\u7a0b\u60f3\u8fdb\u5165 synchronized \u4fee\u9970\u7684\u4ee3\u7801\u5757\/\u65b9\u6cd5&#xff0c;\u4f46\u9501\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5360\u7528\u4e86&#xff0c;\u5c31\u4f1a\u8fdb\u5165\u963b\u585e\u72b6\u6001&#xff0c;\u4e00\u76f4\u7b49\u5230\u9501\u91ca\u653e\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f8b\u5b50&#xff1a; \u7ebf\u7a0b B \u4f1a\u5361\u5728\u8fdb\u5165 synchronized \u7684\u5730\u65b9&#xff0c;\u76f4\u5230\u7ebf\u7a0b A \u91ca\u653e\u9501\u3002<\/p>\n<p>synchronized (lock) {<br \/>\n    \/\/ \u5047\u8bbe\u7ebf\u7a0b A \u5148\u62ff\u5230\u9501&#xff0c;\u7ebf\u7a0b B \u60f3\u8fdb\u6765\u5c31\u4f1a\u963b\u585e<br \/>\n}<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7b49\u5f85&#xff08;Waiting&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u573a\u666f&#xff1a;\u7ebf\u7a0b\u8c03\u7528\u4e86 wait()&#xff08;\u5fc5\u987b\u5728 synchronized \u91cc\u8c03\u7528 &#xff09;\u3001join() \u7b49\u65b9\u6cd5&#xff0c;\u4f1a\u8fdb\u5165\u201c\u65e0\u9650\u671f\u7b49\u5f85\u201d&#xff0c;\u76f4\u5230\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5524\u9192\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f8b\u5b50&#xff1a;<\/p>\n<p>synchronized (lock) {<br \/>\n    \/\/ \u7ebf\u7a0b\u8c03\u7528 wait()&#xff0c;\u91ca\u653e\u9501&#xff0c;\u8fdb\u5165\u7b49\u5f85<br \/>\n    lock.wait();<br \/>\n    \/\/ \u5fc5\u987b\u7b49\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528 lock.notify() \u6216 notifyAll()&#xff0c;\u624d\u4f1a\u88ab\u5524\u9192&#xff0c;\u56de\u5230\u53ef\u8fd0\u884c\u72b6\u6001<br \/>\n}<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8d85\u65f6\u7b49\u5f85&#xff08;Timed Waiting&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u573a\u666f&#xff1a;\u7ebf\u7a0b\u8c03\u7528\u4e86 sleep(long \u6beb\u79d2)\u3001wait(long \u6beb\u79d2)\u3001join(long \u6beb\u79d2) \u7b49\u65b9\u6cd5&#xff0c;\u4f1a\u8fdb\u5165\u201c\u6709\u9650\u671f\u7b49\u5f85\u201d&#xff0c;\u5230\u65f6\u95f4\u81ea\u52a8\u5524\u9192&#xff0c;\u6216\u8005\u63d0\u524d\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5524\u9192\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f8b\u5b50&#xff1a; \u6216\u8005&#xff1a;<\/p>\n<p>\/\/ \u7ebf\u7a0b\u7761 3 \u79d2&#xff0c;\u671f\u95f4\u4e0d\u5360\u7528 CPU&#xff0c;\u5230\u65f6\u95f4\u81ea\u52a8\u56de\u5230\u53ef\u8fd0\u884c\u72b6\u6001<br \/>\nThread.sleep(3000);<\/p>\n<p>synchronized (lock) {<br \/>\n    \/\/ \u6700\u591a\u7b49 5 \u79d2&#xff0c;\u8981\u662f\u6ca1\u4eba\u5524\u9192&#xff0c;\u5230\u65f6\u95f4\u4e5f\u4f1a\u81ea\u5df1\u9192<br \/>\n    lock.wait(5000);<br \/>\n}<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u56de\u5230\u53ef\u8fd0\u884c\u72b6\u6001&#xff1a;<\/p>\n<p>\u4e0d\u7ba1\u662f\u963b\u585e\u3001\u7b49\u5f85\u8fd8\u662f\u8d85\u65f6\u7b49\u5f85&#xff0c;\u53ea\u8981\u201c\u963b\u788d\u56e0\u7d20\u6d88\u5931\u201d&#xff08;\u6bd4\u5982\u9501\u91ca\u653e\u4e86\u3001\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5524\u9192\u4e86\u3001\u8d85\u65f6\u65f6\u95f4\u5230\u4e86 &#xff09;&#xff0c;\u7ebf\u7a0b\u5c31\u4f1a\u56de\u5230 \u53ef\u8fd0\u884c&#xff08;Runnable&#xff09; \u72b6\u6001&#xff0c;\u91cd\u65b0\u6392\u961f\u7b49 CPU \u8c03\u5ea6\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>5. \u7ec8\u6b62&#xff08;Terminated&#xff09;<\/h4>\n<ul>\n<li>\u573a\u666f&#xff1a;run() \u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5&#xff08;\u6b63\u5e38\u8dd1\u5b8c\u6240\u6709\u4ee3\u7801 &#xff09;&#xff0c;\u6216\u8005\u7ebf\u7a0b\u88ab\u5f3a\u5236\u4e2d\u65ad&#xff08;\u6bd4\u5982\u629b\u4e86\u672a\u6355\u83b7\u7684\u5f02\u5e38 &#xff09;&#xff0c;\u7ebf\u7a0b\u5c31\u4f1a\u8fdb\u5165\u7ec8\u6b62\u72b6\u6001&#xff0c;\u751f\u547d\u5468\u671f\u7ed3\u675f&#xff0c;\u4e0d\u80fd\u518d\u91cd\u542f\u3002<\/li>\n<li>\u6ce8\u610f&#xff1a;\u7ebf\u7a0b\u7ec8\u6b62\u540e&#xff0c;\u5c31\u7b97\u8c03\u7528 start() \u4e5f\u6ca1\u7528&#xff0c;\u4f1a\u62a5\u9519&#xff01;\u7ebf\u7a0b\u5bf9\u8c61\u4e00\u65e6\u7ec8\u6b62&#xff0c;\u5c31\u201c\u62a5\u5e9f\u201d\u4e86&#xff0c;\u60f3\u518d\u6267\u884c\u4efb\u52a1&#xff0c;\u5f97\u91cd\u65b0 new \u4e00\u4e2a\u3002<\/li>\n<\/ul>\n<h4>\u4e09\u3001\u7ebf\u7a0b\u5e38\u7528\u65b9\u6cd5<\/h4>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u662f Thread \u7c7b\u6216 Object \u7c7b&#xff08;wait\u3001notify \u7b49\u662f Object \u7684\u65b9\u6cd5 &#xff09;\u63d0\u4f9b\u7684&#xff0c;\u7528\u6765\u63a7\u5236\u7ebf\u7a0b\u7684\u72b6\u6001\u3001\u884c\u4e3a&#xff0c;\u89e3\u51b3\u201c\u7ebf\u7a0b\u4e4b\u95f4\u600e\u4e48\u914d\u5408\u3001\u600e\u4e48\u8c03\u5ea6\u201d\u7684\u95ee\u9898\u3002<\/p>\n<table>\n<tr>\u65b9\u6cd5\u4f5c\u7528 &amp; \u7ec6\u8282\u6ce8\u610f\u4e8b\u9879<\/tr>\n<tbody>\n<tr>\n<td>start()<\/td>\n<td>\u542f\u52a8\u7ebf\u7a0b&#xff01;\u8ba9 JVM \u4e3a\u7ebf\u7a0b\u5206\u914d\u8d44\u6e90&#xff08;\u6808\u3001PC \u7b49 &#xff09;&#xff0c;\u5e76\u628a\u7ebf\u7a0b\u653e\u5165\u201c\u53ef\u8fd0\u884c\u201d\u961f\u5217&#xff0c;\u7b49\u5f85 CPU \u8c03\u5ea6\u3002\u5fc5\u987b\u7528\u8fd9\u4e2a\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u8c03 run()&#xff08;\u76f4\u63a5\u8c03 run() \u5c31\u662f\u666e\u901a\u65b9\u6cd5\u8c03\u7528&#xff0c;\u4e0d\u4f1a\u5f00\u65b0\u7ebf\u7a0b &#xff09;\u3002<\/td>\n<td>\u7ebf\u7a0b\u53ea\u80fd start() \u4e00\u6b21&#xff0c;\u7b2c\u4e8c\u6b21\u8c03\u4f1a\u629b IllegalThreadStateException \u5f02\u5e38\u3002<\/td>\n<\/tr>\n<tr>\n<td>run()<\/td>\n<td>\u7ebf\u7a0b\u7684\u201c\u4efb\u52a1\u903b\u8f91\u201d\u5165\u53e3&#xff0c;JVM \u4f1a\u5728 start() \u540e\u81ea\u52a8\u8c03\u7528\u5b83\u3002\u4f60\u5728\u8fd9\u65b9\u6cd5\u91cc\u5199\u7ebf\u7a0b\u8981\u505a\u7684\u4e8b&#xff08;\u6bd4\u5982\u5faa\u73af\u3001IO \u64cd\u4f5c\u7b49 &#xff09;\u3002<\/td>\n<td>\u76f4\u63a5\u8c03\u7528 run() \u4e0d\u4f1a\u5f00\u65b0\u7ebf\u7a0b&#xff0c;\u5c31\u662f\u666e\u901a\u65b9\u6cd5\u6267\u884c&#xff0c;\u8c28\u614e\u4f7f\u7528&#xff01;<\/td>\n<\/tr>\n<tr>\n<td>sleep(long millis)<\/td>\n<td>\u8ba9\u5f53\u524d\u7ebf\u7a0b\u201c\u4f11\u7720\u201d\u6307\u5b9a\u6beb\u79d2\u6570&#xff0c;\u4e0d\u91ca\u653e\u9501&#xff08;\u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u62ff\u7740\u9501&#xff0c;sleep \u671f\u95f4\u9501\u4e0d\u4f1a\u653e &#xff09;&#xff0c;\u5230\u65f6\u95f4\u540e\u56de\u5230\u201c\u53ef\u8fd0\u884c\u201d\u72b6\u6001\u3002<\/td>\n<td>\u9759\u6001\u65b9\u6cd5&#xff0c;\u76f4\u63a5 Thread.sleep(1000); \u8c03\u7528&#xff1b;\u53ef\u80fd\u4f1a\u629b\u51fa InterruptedException&#xff08;\u6bd4\u5982\u4f11\u7720\u65f6\u88ab\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u65ad &#xff09;&#xff0c;\u9700\u8981\u5904\u7406\u5f02\u5e38\u3002<\/td>\n<\/tr>\n<tr>\n<td>join()<\/td>\n<td>\u8ba9\u5f53\u524d\u7ebf\u7a0b\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u3002\u6bd4\u5982\u7ebf\u7a0b A \u8c03\u7528 threadB.join()&#xff0c;\u7ebf\u7a0b A \u4f1a\u8fdb\u5165\u201c\u7b49\u5f85\u201d&#xff0c;\u76f4\u5230 threadB \u7684 run() \u6267\u884c\u5b8c&#xff0c;\u7ebf\u7a0b A \u624d\u4f1a\u7ee7\u7eed\u8dd1\u3002<\/td>\n<td>\u5e38\u7528\u6765\u201c\u534f\u8c03\u7ebf\u7a0b\u6267\u884c\u987a\u5e8f\u201d&#xff0c;\u6bd4\u5982\u4e3b\u7ebf\u7a0b\u7b49\u5b50\u7ebf\u7a0b\u8dd1\u5b8c\u518d\u6c47\u603b\u7ed3\u679c&#xff1b;\u4e5f\u4f1a\u629b InterruptedException\u3002<\/td>\n<\/tr>\n<tr>\n<td>wait() \/ notify() \/ notifyAll()<\/td>\n<td>&#8211; wait()&#xff1a;\u8ba9\u5f53\u524d\u7ebf\u7a0b\u91ca\u653e\u9501&#xff0c;\u8fdb\u5165\u201c\u7b49\u5f85\u201d\u72b6\u6001&#xff0c;\u5fc5\u987b\u5728 synchronized \u4ee3\u7801\u5757\/\u65b9\u6cd5\u91cc\u8c03\u7528\u3002&lt;br&gt;- notify()&#xff1a;\u5524\u9192\u4e00\u4e2a\u7b49\u5f85\u5728\u540c\u4e00\u4e2a\u9501\u4e0a\u7684\u7ebf\u7a0b&#xff08;\u968f\u673a\u9009\u4e00\u4e2a &#xff09;\u3002&lt;br&gt;- notifyAll()&#xff1a;\u5524\u9192\u6240\u6709\u7b49\u5f85\u5728\u540c\u4e00\u4e2a\u9501\u4e0a\u7684\u7ebf\u7a0b\u3002<\/td>\n<td>\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u90fd\u662f Object \u7c7b\u7684\u65b9\u6cd5&#xff08;\u56e0\u4e3a\u9501\u53ef\u4ee5\u662f\u4efb\u610f\u5bf9\u8c61 &#xff09;&#xff0c;\u5fc5\u987b\u5728 synchronized \u91cc\u7528&#xff0c;\u5426\u5219\u629b IllegalMonitorStateException \u5f02\u5e38\u3002<\/td>\n<\/tr>\n<tr>\n<td>interrupt()<\/td>\n<td>\u201c\u4e2d\u65ad\u201d\u7ebf\u7a0b&#xff0c;\u4f46\u4e0d\u662f\u5f3a\u5236\u7ec8\u6b62&#xff08;\u522b\u7406\u89e3\u6210\u6740\u6b7b\u7ebf\u7a0b &#xff09;\u3002\u4f1a\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u201c\u4e2d\u65ad\u6807\u5fd7\u4f4d\u201d&#xff0c;\u5982\u679c\u7ebf\u7a0b\u5728 sleep\u3001wait\u3001join \u7b49\u65b9\u6cd5\u91cc&#xff0c;\u4f1a\u629b\u51fa InterruptedException&#xff0c;\u8ba9\u7ebf\u7a0b\u611f\u77e5\u5230\u201c\u88ab\u4e2d\u65ad\u201d&#xff0c;\u7136\u540e\u81ea\u5df1\u51b3\u5b9a\u600e\u4e48\u5904\u7406&#xff08;\u6bd4\u5982\u63d0\u524d\u7ed3\u675f\u4efb\u52a1 &#xff09;\u3002<\/td>\n<td>\u53ea\u662f\u6253\u4e2a\u201c\u4e2d\u65ad\u6807\u8bb0\u201d&#xff0c;\u5177\u4f53\u600e\u4e48\u54cd\u5e94\u4e2d\u65ad&#xff0c;\u5f97\u770b\u7ebf\u7a0b\u5185\u90e8\u903b\u8f91\u3002\u6bd4\u5982&#xff1a;&lt;br&gt;java&lt;br&gt;if (Thread.currentThread().isInterrupted()) {&lt;br&gt; \/\/ \u81ea\u5df1\u5904\u7406\u4e2d\u65ad&#xff0c;\u6bd4\u5982\u7ed3\u675f\u5faa\u73af\u3001\u8fd4\u56de\u7ed3\u679c&lt;br&gt;}&lt;br&gt;<\/td>\n<\/tr>\n<tr>\n<td>setPriority(int priority)<\/td>\n<td>\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7&#xff08;1 &#8211; 10&#xff0c;\u9ed8\u8ba4 5 &#xff09;&#xff0c;\u53ea\u662f\u7ed9\u7ebf\u7a0b\u8c03\u5ea6\u5668\u201c\u5efa\u8bae\u201d&#xff0c;\u4e0d\u4fdd\u8bc1\u4f18\u5148\u7ea7\u9ad8\u7684\u7ebf\u7a0b\u4e00\u5b9a\u5148\u6267\u884c\u3002<\/td>\n<td>\u4f18\u5148\u7ea7\u9ad8\u7684\u7ebf\u7a0b\u88ab CPU \u9009\u4e2d\u7684\u6982\u7387\u5927\u4e00\u4e9b&#xff0c;\u4f46\u522b\u4f9d\u8d56\u5b83\u63a7\u5236\u6267\u884c\u987a\u5e8f&#xff08;\u4e0d\u540c\u7cfb\u7edf\u8c03\u5ea6\u7b56\u7565\u4e0d\u540c &#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>yield()<\/td>\n<td>\u8ba9\u5f53\u524d\u7ebf\u7a0b\u201c\u8ba9\u51fa\u201dCPU \u8d44\u6e90&#xff0c;\u56de\u5230\u201c\u53ef\u8fd0\u884c\u201d\u72b6\u6001&#xff0c;\u91cd\u65b0\u548c\u5176\u4ed6\u7ebf\u7a0b\u4e00\u8d77\u7ade\u4e89 CPU\u3002\u53ea\u662f\u5efa\u8bae&#xff0c;\u8c03\u5ea6\u5668\u53ef\u80fd\u4e0d\u7406\u4f1a\u3002<\/td>\n<td>\u5e38\u7528\u6765\u201c\u7ed9\u4f18\u5148\u7ea7\u4f4e\u7684\u7ebf\u7a0b\u673a\u4f1a\u201d&#xff0c;\u4f46\u5b9e\u9645\u6548\u679c\u770b JVM \u5fc3\u60c5&#xff0c;\u522b\u5f53\u7cbe\u786e\u63a7\u5236\u624b\u6bb5\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u56db\u3001\u7ebf\u7a0b\u540c\u6b65\u4e0e\u9501&#xff08;\u89e3\u51b3\u201c\u591a\u7ebf\u7a0b\u62a2\u8d44\u6e90\u201d\u95ee\u9898&#xff0c;\u907f\u514d\u6570\u636e\u6df7\u4e71 &#xff09;<\/h4>\n<p>\u591a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90&#xff08;\u6bd4\u5982\u540c\u4e00\u4e2a\u53d8\u91cf\u3001\u540c\u4e00\u4e2a\u6587\u4ef6 &#xff09;\u65f6&#xff0c;\u5bb9\u6613\u51fa\u73b0\u201c\u6570\u636e\u7ade\u4e89\u201d&#xff0c;\u5bfc\u81f4\u7ed3\u679c\u6df7\u4e71\u3002\u7ebf\u7a0b\u540c\u6b65\u5c31\u662f\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684&#xff0c;\u6838\u5fc3\u601d\u8def\u662f\u8ba9\u591a\u4e2a\u7ebf\u7a0b\u201c\u6709\u5e8f\u8bbf\u95ee\u201d\u5171\u4eab\u8d44\u6e90&#xff0c;\u5e38\u7528 synchronized \u548c Lock \u63a5\u53e3\u4e24\u79cd\u65b9\u5f0f\u3002<\/p>\n<h4>1. synchronized \u5173\u952e\u5b57&#xff08;\u5185\u7f6e\u9501&#xff0c;\u7b80\u5355\u76f4\u63a5 &#xff09;<\/h4>\n<p>synchronized \u53ef\u4ee5\u4fee\u9970\u65b9\u6cd5\u6216\u4ee3\u7801\u5757&#xff0c;\u4fdd\u8bc1\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u8fdb\u5165\u201c\u88ab\u4fdd\u62a4\u7684\u533a\u57df\u201d&#xff0c;\u4ece\u800c\u907f\u514d\u591a\u7ebf\u7a0b\u51b2\u7a81\u3002<\/p>\n<h4>&#xff08;1&#xff09;\u4fee\u9970\u4ee3\u7801\u5757&#xff08;\u6307\u5b9a\u9501\u5bf9\u8c61 &#xff09;<\/h4>\n<p>class BankAccount {<br \/>\n    private int balance &#061; 1000;<br \/>\n    \/\/ \u81ea\u5b9a\u4e49\u9501\u5bf9\u8c61&#xff08;\u4e5f\u53ef\u4ee5\u7528 this\u3001\u5b57\u8282\u7801\u5bf9\u8c61\u7b49\u5f53\u9501&#xff0c;\u770b\u9700\u6c42 &#xff09;<br \/>\n    private final Object lock &#061; new Object();<\/p>\n<p>    public void withdraw(int amount) {<br \/>\n        \/\/ \u540c\u6b65\u4ee3\u7801\u5757&#xff1a;\u53ea\u6709\u62ff\u5230 lock \u9501\u7684\u7ebf\u7a0b&#xff0c;\u624d\u80fd\u8fdb\u8fd9\u4e2a\u5757<br \/>\n        synchronized (lock) {<br \/>\n            if (balance &gt;&#061; amount) {<br \/>\n                balance -&#061; amount;<br \/>\n                System.out.println(<br \/>\n                    Thread.currentThread().getName() &#043; &#034; \u53d6\u6b3e &#034; &#043; amount &#043; &#034;&#xff0c;\u4f59\u989d&#xff1a;&#034; &#043; balance<br \/>\n                );<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<ul>\n<li>\u539f\u7406&#xff1a;\u7ebf\u7a0b\u60f3\u8fdb\u5165 synchronized (lock) \u4ee3\u7801\u5757&#xff0c;\u5fc5\u987b\u5148\u62ff\u5230 lock \u5bf9\u8c61\u7684\u201c\u9501\u201d\u3002\u5982\u679c\u9501\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5360\u7528&#xff0c;\u5f53\u524d\u7ebf\u7a0b\u5c31\u4f1a\u8fdb\u5165\u963b\u585e&#xff08;Blocked&#xff09; \u72b6\u6001&#xff0c;\u76f4\u5230\u9501\u91ca\u653e\u3002\u8fd9\u6837\u5c31\u4fdd\u8bc1\u4e86&#xff0c;\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u4fee\u6539 balance&#xff0c;\u907f\u514d\u591a\u7ebf\u7a0b\u540c\u65f6\u51cf\u4f59\u989d\u5bfc\u81f4\u6570\u636e\u9519\u8bef\u3002<\/li>\n<\/ul>\n<h4>&#xff08;2&#xff09;\u4fee\u9970\u65b9\u6cd5&#xff08;\u9690\u5f0f\u9501\u5bf9\u8c61 &#xff09;<\/h4>\n<ul>\n<li>\n<p>\u4fee\u9970\u5b9e\u4f8b\u65b9\u6cd5&#xff1a;<\/p>\n<p>public synchronized void withdraw(int amount) {<br \/>\n    \/\/ \u9501\u5bf9\u8c61\u662f this&#xff08;\u5f53\u524d\u5b9e\u4f8b&#xff09;<br \/>\n    if (balance &gt;&#061; amount) {<br \/>\n        balance -&#061; amount;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u76f8\u5f53\u4e8e synchronized (this) { &#8230; }&#xff0c;\u9501\u662f\u5f53\u524d\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/p>\n<\/li>\n<li>\n<p>\u4fee\u9970\u9759\u6001\u65b9\u6cd5&#xff1a;<\/p>\n<p>public static synchronized void withdraw(int amount) {<br \/>\n    \/\/ \u9501\u5bf9\u8c61\u662f \u7c7b\u7684\u5b57\u8282\u7801\u5bf9\u8c61&#xff08;\u6bd4\u5982 BankAccount.class &#xff09;<br \/>\n}<\/p>\n<p>\u76f8\u5f53\u4e8e synchronized (BankAccount.class) { &#8230; }&#xff0c;\u9501\u662f\u7c7b\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f18\u7f3a\u70b9&#xff1a;<\/p>\n<p>\u4f18\u70b9&#xff1a;\u7b80\u5355\u6613\u7528&#xff0c;JVM \u81ea\u52a8\u7ba1\u7406\u9501\u7684\u83b7\u53d6\u548c\u91ca\u653e&#xff08;\u8fdb\u5165\u540c\u6b65\u5757\u81ea\u52a8\u62ff\u9501&#xff0c;\u9000\u51fa\u81ea\u52a8\u653e\u9501&#xff0c;\u5305\u62ec\u629b\u5f02\u5e38\u7684\u60c5\u51b5 &#xff09;\u3002<\/p>\n<p>\u7f3a\u70b9&#xff1a;\u4e0d\u591f\u7075\u6d3b&#xff08;\u6bd4\u5982\u60f3\u5c1d\u8bd5\u83b7\u53d6\u9501\u3001\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4&#xff0c;synchronized \u505a\u4e0d\u5230 &#xff09;&#xff1b;\u5982\u679c\u540c\u6b65\u8303\u56f4\u592a\u5927&#xff0c;\u4f1a\u5f71\u54cd\u6027\u80fd&#xff08;\u591a\u4e2a\u7ebf\u7a0b\u6392\u961f\u592a\u4e45 &#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>2. Lock \u63a5\u53e3&#xff08;\u663e\u5f0f\u9501&#xff0c;\u66f4\u7075\u6d3b &#xff09;<\/h4>\n<p>java.util.concurrent.locks.Lock \u662f\u66f4\u7075\u6d3b\u7684\u9501\u673a\u5236&#xff0c;\u5e38\u7528\u5b9e\u73b0\u7c7b\u662f ReentrantLock&#xff08;\u53ef\u91cd\u5165\u9501&#xff0c;\u548c synchronized \u7c7b\u4f3c&#xff0c;\u540c\u4e00\u7ebf\u7a0b\u53ef\u4ee5\u591a\u6b21\u62ff\u9501 &#xff09;\u3002<\/p>\n<h4>\u57fa\u672c\u7528\u6cd5&#xff1a;<\/h4>\n<p>class BankAccount {<br \/>\n    private int balance &#061; 1000;<br \/>\n    \/\/ \u521b\u5efa Lock \u5bf9\u8c61&#xff0c;\u5e38\u7528 ReentrantLock<br \/>\n    private Lock lock &#061; new ReentrantLock();<\/p>\n<p>    public void withdraw(int amount) {<br \/>\n        \/\/ \u624b\u52a8\u52a0\u9501<br \/>\n        lock.lock();<br \/>\n        try {<br \/>\n            \/\/ \u4e34\u754c\u533a&#xff1a;\u64cd\u4f5c\u5171\u4eab\u8d44\u6e90\u7684\u4ee3\u7801<br \/>\n            if (balance &gt;&#061; amount) {<br \/>\n                balance -&#061; amount;<br \/>\n                System.out.println(<br \/>\n                    Thread.currentThread().getName() &#043; &#034; \u53d6\u6b3e &#034; &#043; amount &#043; &#034;&#xff0c;\u4f59\u989d&#xff1a;&#034; &#043; balance<br \/>\n                );<br \/>\n            }<br \/>\n        } finally {<br \/>\n            \/\/ \u624b\u52a8\u91ca\u653e\u9501&#xff01;\u5fc5\u987b\u5728 finally \u91cc\u653e&#xff0c;\u5426\u5219\u4e00\u65e6\u4ee3\u7801\u629b\u5f02\u5e38&#xff0c;\u9501\u6c38\u8fdc\u4e0d\u4f1a\u91ca\u653e&#xff0c;\u5bfc\u81f4\u6b7b\u9501<br \/>\n            lock.unlock();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<ul>\n<li>\u539f\u7406&#xff1a;\u548c synchronized \u7c7b\u4f3c&#xff0c;lock() \u4f1a\u5c1d\u8bd5\u83b7\u53d6\u9501&#xff0c;\u62ff\u5230\u9501\u7684\u7ebf\u7a0b\u624d\u80fd\u6267\u884c\u4e34\u754c\u533a\u4ee3\u7801&#xff1b;\u6267\u884c\u5b8c\u540e&#xff0c;\u5fc5\u987b\u5728 finally \u91cc\u8c03\u7528 unlock() \u91ca\u653e\u9501&#xff0c;\u4fdd\u8bc1\u9501\u4e00\u5b9a\u80fd\u91ca\u653e&#xff08;\u5373\u4f7f\u4ee3\u7801\u629b\u5f02\u5e38 &#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u4f18\u70b9&#xff08;\u5bf9\u6bd4 synchronized &#xff09;&#xff1a;<\/h4>\n<ul>\n<li>\n<p>\u66f4\u7075\u6d3b&#xff1a;<\/p>\n<ul>\n<li>\u53ef\u4ee5\u5c1d\u8bd5\u83b7\u53d6\u9501&#xff08;tryLock() &#xff09;&#xff0c;\u62ff\u4e0d\u5230\u9501\u53ef\u4ee5\u4e0d\u7b49\u5f85&#xff0c;\u53bb\u505a\u522b\u7684\u4e8b&#xff1b;<\/li>\n<li>\u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4&#xff08;tryLock(long time, TimeUnit unit) &#xff09;&#xff0c;\u907f\u514d\u7ebf\u7a0b\u65e0\u9650\u963b\u585e&#xff1b;<\/li>\n<li>\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u9501\u903b\u8f91&#xff08;\u6bd4\u5982\u8bfb\u5199\u9501 ReentrantReadWriteLock&#xff0c;\u8bfb\u9501\u5171\u4eab\u3001\u5199\u9501\u72ec\u5360&#xff0c;\u9002\u5408\u8bfb\u591a\u5199\u5c11\u7684\u573a\u666f &#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u53ef\u4e2d\u65ad&#xff1a;<\/p>\n<p>lockInterruptibly() \u65b9\u6cd5\u5141\u8bb8\u5728\u83b7\u53d6\u9501\u7684\u8fc7\u7a0b\u4e2d\u54cd\u5e94\u4e2d\u65ad&#xff08;\u6bd4\u5982\u7b49\u5f85\u9501\u65f6&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528\u4e86 interrupt()&#xff0c;\u53ef\u4ee5\u63d0\u524d\u653e\u5f03\u7b49\u5f85<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb745\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf24\u6b21\u3002Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u6458\u8981\uff1a \u7ebf\u7a0b\u521b\u5efa\u65b9\u5f0f\uff1a \u7ee7\u627fThread\u7c7b\uff1a\u91cd\u5199run()\u65b9\u6cd5\u5b9a\u4e49\u4efb\u52a1\uff0c\u901a\u8fc7start()\u542f\u52a8\u7ebf\u7a0b \u5b9e\u73b0Runnable\u63a5\u53e3\uff1a\u66f4\u7075\u6d3b\uff0c\u652f\u6301\u591a\u7ebf\u7a0b\u5171\u4eab\u4efb\u52a1\u5bf9\u8c61 \u7ebf\u7a0b\u751f\u547d\u5468\u671f\uff1a \u5305\u542b\u65b0\u5efa\u3001\u53ef\u8fd0\u884c\u3001\u8fd0\u884c\u3001\u963b\u585e\/\u7b49\u5f85\/\u8d85\u65f6\u7b49\u5f85\u3001\u7ec8\u6b62\u4e94\u79cd\u72b6\u6001\uff0cstart()\u89e6\u53d1\u72b6\u6001\u8f6c\u6362 \u6838\u5fc3\u65b9\u6cd5\uff1a start()\/run()\uff1a\u7ebf\u7a0b\u542f\u52a8\u4e0e\u4efb\u52a1\u5165\u53e3 sleep()\/join()\uff1a\u63a7\u5236\u7ebf\u7a0b\u6267\u884c\u65f6\u5e8f wait()\/notify()\uff1a\u7ebf\u7a0b\u95f4\u901a\u4fe1 synchronized\/Lock\uff1a\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u89e3\u51b3\u8d44\u6e90\u5171\u4eab\u51b2\u7a81\u95ee\u9898 \u7ebf\u7a0b\u540c\u6b65<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,586,190],"topic":[],"class_list":["post-54201","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-586","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java\u57fa\u7840-\u591a\u7ebf\u7a0b - \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\/54201.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java\u57fa\u7840-\u591a\u7ebf\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb745\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf24\u6b21\u3002Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u6458\u8981\uff1a \u7ebf\u7a0b\u521b\u5efa\u65b9\u5f0f\uff1a \u7ee7\u627fThread\u7c7b\uff1a\u91cd\u5199run()\u65b9\u6cd5\u5b9a\u4e49\u4efb\u52a1\uff0c\u901a\u8fc7start()\u542f\u52a8\u7ebf\u7a0b \u5b9e\u73b0Runnable\u63a5\u53e3\uff1a\u66f4\u7075\u6d3b\uff0c\u652f\u6301\u591a\u7ebf\u7a0b\u5171\u4eab\u4efb\u52a1\u5bf9\u8c61 \u7ebf\u7a0b\u751f\u547d\u5468\u671f\uff1a \u5305\u542b\u65b0\u5efa\u3001\u53ef\u8fd0\u884c\u3001\u8fd0\u884c\u3001\u963b\u585e\/\u7b49\u5f85\/\u8d85\u65f6\u7b49\u5f85\u3001\u7ec8\u6b62\u4e94\u79cd\u72b6\u6001\uff0cstart()\u89e6\u53d1\u72b6\u6001\u8f6c\u6362 \u6838\u5fc3\u65b9\u6cd5\uff1a start()\/run()\uff1a\u7ebf\u7a0b\u542f\u52a8\u4e0e\u4efb\u52a1\u5165\u53e3 sleep()\/join()\uff1a\u63a7\u5236\u7ebf\u7a0b\u6267\u884c\u65f6\u5e8f wait()\/notify()\uff1a\u7ebf\u7a0b\u95f4\u901a\u4fe1 synchronized\/Lock\uff1a\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u89e3\u51b3\u8d44\u6e90\u5171\u4eab\u51b2\u7a81\u95ee\u9898 \u7ebf\u7a0b\u540c\u6b65\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/54201.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-12T15:49:47+00:00\" \/>\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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/54201.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/54201.html\",\"name\":\"Java\u57fa\u7840-\u591a\u7ebf\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-12T15:49:47+00:00\",\"dateModified\":\"2025-08-12T15:49:47+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/54201.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/54201.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/54201.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\u57fa\u7840-\u591a\u7ebf\u7a0b\"}]},{\"@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":"Java\u57fa\u7840-\u591a\u7ebf\u7a0b - \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\/54201.html","og_locale":"zh_CN","og_type":"article","og_title":"Java\u57fa\u7840-\u591a\u7ebf\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb745\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf24\u6b21\u3002Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u6458\u8981\uff1a \u7ebf\u7a0b\u521b\u5efa\u65b9\u5f0f\uff1a \u7ee7\u627fThread\u7c7b\uff1a\u91cd\u5199run()\u65b9\u6cd5\u5b9a\u4e49\u4efb\u52a1\uff0c\u901a\u8fc7start()\u542f\u52a8\u7ebf\u7a0b \u5b9e\u73b0Runnable\u63a5\u53e3\uff1a\u66f4\u7075\u6d3b\uff0c\u652f\u6301\u591a\u7ebf\u7a0b\u5171\u4eab\u4efb\u52a1\u5bf9\u8c61 \u7ebf\u7a0b\u751f\u547d\u5468\u671f\uff1a \u5305\u542b\u65b0\u5efa\u3001\u53ef\u8fd0\u884c\u3001\u8fd0\u884c\u3001\u963b\u585e\/\u7b49\u5f85\/\u8d85\u65f6\u7b49\u5f85\u3001\u7ec8\u6b62\u4e94\u79cd\u72b6\u6001\uff0cstart()\u89e6\u53d1\u72b6\u6001\u8f6c\u6362 \u6838\u5fc3\u65b9\u6cd5\uff1a start()\/run()\uff1a\u7ebf\u7a0b\u542f\u52a8\u4e0e\u4efb\u52a1\u5165\u53e3 sleep()\/join()\uff1a\u63a7\u5236\u7ebf\u7a0b\u6267\u884c\u65f6\u5e8f wait()\/notify()\uff1a\u7ebf\u7a0b\u95f4\u901a\u4fe1 synchronized\/Lock\uff1a\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u89e3\u51b3\u8d44\u6e90\u5171\u4eab\u51b2\u7a81\u95ee\u9898 \u7ebf\u7a0b\u540c\u6b65","og_url":"https:\/\/www.wsisp.com\/helps\/54201.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-12T15:49:47+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/54201.html","url":"https:\/\/www.wsisp.com\/helps\/54201.html","name":"Java\u57fa\u7840-\u591a\u7ebf\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-12T15:49:47+00:00","dateModified":"2025-08-12T15:49:47+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/54201.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/54201.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/54201.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java\u57fa\u7840-\u591a\u7ebf\u7a0b"}]},{"@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\/54201","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=54201"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/54201\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=54201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=54201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=54201"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=54201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}