{"id":17598,"date":"2025-06-12T10:08:45","date_gmt":"2025-06-12T02:08:45","guid":{"rendered":"https:\/\/92it.top\/?p=17598"},"modified":"2025-06-12T10:08:45","modified_gmt":"2025-06-12T02:08:45","slug":"%e6%b7%b1%e5%ba%a6%e5%89%96%e6%9e%90%ef%bc%9ajava%e7%ba%bf%e7%a8%8b%e8%bf%90%e8%a1%8c%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17598","title":{"rendered":"\u6df1\u5ea6\u5256\u6790\uff1aJava\u7ebf\u7a0b\u8fd0\u884c\u673a\u5236"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247484253&amp;idx=1&amp;sn=4df8111a0d3c8b1c7d9c2d64cc3aac9b&amp;chksm=96c72f61a1b0a67749c5f2c1377f24294e61e8791d363db206a91f7887f0e8bc899388a89e86&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">\u6df1\u5ea6\u5256\u6790\uff1aJava\u7ebf\u7a0b\u8fd0\u884c\u673a\u5236\uff0c\u7a0b\u5e8f\u5458\u5fc5\u770b\u7684\u77e5\u8bc6\u70b9\uff01<\/a><\/p>\n\n\n\n<p>\u5728 Java19 \u4e4b\u524d\uff0c\u7ebf\u7a0b\u662f Java \u8fd0\u884c\u7684\u6700\u5c0f\u5355\u5143\uff0c\u7ebf\u7a0b\u4f5c\u4e3a Java \u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\uff0c\u5728 Java \u7684\u53d1\u5c55\u53f2\u4e0a\u8d77\u7740\u4e3e\u8db3\u8f7b\u91cd\u7684\u4f5c\u7528\uff0c\u56e0\u6b64\uff0c\u4eca\u5929\u6211\u4eec\u5c31\u6765\u804a\u804a Java \u7ebf\u7a0b\u7684\u76f8\u5173\u77e5\u8bc6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u7533\u660e\uff1a\u672c\u6587\u57fa\u4e8e jdk-11.0.15\uff0c\u64cd\u4f5c\u7cfb\u7edf\u57fa\u4e8e Linux\uff0cJVM \u57fa\u4e8e hotspot \u6e90\u7801\uff0chotspot \u6e90\u7801\u4e0b\u8f7d\u5730\u5740: https:\/\/github.com\/openjdk\/jdk<\/pre>\n\n\n\n<p>\u672c\u6587\u4f1a\u4ece \u7ebf\u7a0b\u5b9a\u4e49\u3001\u7ebf\u7a0b\u521b\u5efa\u3001\u7ebf\u7a0b\u72b6\u6001\u3001\u7ebf\u7a0b\u5de5\u4f5c\u539f\u7406\u3001\u7ebf\u7a0b\u7ec4\u3001\u7ebf\u7a0b\u4f18\u5148\u7ea7\u3001\u7ebf\u7a0b\u901a\u4fe1 7 \u4e2a\u90e8\u5206\u5bf9\u7ebf\u7a0b\u8fdb\u884c\u5168\u9762\u5206\u6790\u3002<\/p>\n\n\n\n<p><strong>\u4e00\u3001\u4ec0\u4e48\u662f\u7ebf\u7a0b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u7ef4\u57fa\u767e\u79d1\u4e2d\u6587\u7248\u5bf9\u7ebf\u7a0b\u7684\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"359\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76-1024x359.png\" alt=\"\" class=\"wp-image-17599\" style=\"width:606px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76-1024x359.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76-300x105.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76-768x269.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76-830x291.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76-230x81.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76-350x123.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76-480x168.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-76.png 1296w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u901a\u8fc7\u7ef4\u57fa\u767e\u79d1\u7684\u63cf\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u6267\u884c\u7684\u6700\u5c0f\u5355\u5143\uff0c\u4e00\u6761\u7ebf\u7a0b\u6307\u7684\u662f\u8fdb\u7a0b\u4e2d\u4e00\u4e2a\u5355\u4e00\u987a\u5e8f\u7684\u63a7\u5236\u6d41\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u53ef\u4ee5\u5e76\u53d1\u591a\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u6761\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\u3002\u5728 UnixSystem V \u53ca SunOS \u4e2d\u4e5f\u88ab\u79f0\u4e3a\u8f7b\u91cf\u8fdb\u7a0b\uff08lightweight processes\uff09\uff0c\u4f46\u8f7b\u91cf\u8fdb\u7a0b\u66f4\u591a\u6307\u5185\u6838\u7ebf\u7a0b\uff08kernel thread\uff09\uff0c\u800c\u628a\u7528\u6237\u7ebf\u7a0b\uff08userthread\uff09\u79f0\u4e3a\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\uff0cJava\u4e2d\u7684\u7ebf\u7a0b\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n<p>\u5728 Java \u4e2d\uff0c\u7ebf\u7a0b\u5bf9\u5e94\u7684\u662f java.lang.Thread \u7c7b\uff0c\u4e0b\u9762\u662f Thread \u7c7b\u7684\u6e90\u7801\u63cf\u8ff0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.<br><br>Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.<br><br>When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:<br><br>The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.<br><br>All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.<\/pre>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5728 Java \u4e2d\uff0c\u7ebf\u7a0b\u662f\u6307\u7a0b\u5e8f\u4e2d\u7684\u6267\u884c\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2a\u4f18\u5148\u7ea7\uff0c\u9ad8\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u6267\u884c\u8981\u4f18\u5148\u4e8e\u4f4e\u4f18\u5148\u7ea7\u7ebf\u7a0b\uff0c\u5728 Java \u865a\u62df\u673a\u4e2d\uff0c\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u6267\u884c\u7ebf\u7a0b\uff0c\u5f53 JVM \u542f\u52a8\u65f6\uff0c\u901a\u5e38\u4f1a\u7ef4\u62a4\u4e00\u4e2a \u975e\u5b88\u62a4\u7ebf\u7a0b\uff08main \u65b9\u6cd5\u5bf9\u5e94\u7684\u7ebf\u7a0b\uff09\u3002<\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u7ebf\u7a0b\u521b\u5efa\u65b9\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Java \u4e2d\uff0c\u521b\u5efa\u7ebf\u7a0b\u6709 3 \u79cd\u65b9\u5f0f\uff1a\u7ee7\u627f Thread \u7c7b\u3001\u5b9e\u73b0 Runnable \u63a5\u53e3\u3001Callable\/Future\u3002<\/p>\n\n\n\n<p><strong>2.1\u3001\u7ee7\u627f Thread \u7c7b<\/strong><\/p>\n\n\n\n<p>\u521b\u5efa\u7ebf\u7a0b\u7684\u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u7c7b\u58f0\u660e\u4e3a Thread \u7684\u5b50\u7c7b\u3002\u8be5\u5b50\u7c7b\u9700\u8981\u91cd\u5199 Thread \u7c7b\u7684 run()\u65b9\u6cd5\uff0c\u7136\u540e\u521b\u5efa\u5b50\u7c7b\u7684\u5b9e\u4f8b\uff0c\u6700\u540e\u8c03\u7528\u5b9e\u4f8b\u7684 start()\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class MyThread extends Thread {\n\n  @Override\n  public void run() {\n    System.out.println(\"MyThread extends Thread!\");\n  }\n\n  public static void main(String[] args) {\n    MyThread thread = new MyThread();\n    thread.start();\n  }\n}<\/pre>\n\n\n\n<p>\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MyThread extends Thread!<\/pre>\n\n\n\n<p><strong>2.2\u3001\u5b9e\u73b0 Runnable \u63a5\u53e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u521b\u5efa\u7ebf\u7a0b\u7684\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u58f0\u660e\u4e00\u4e2a\u5b9e\u73b0 Runnable \u63a5\u53e3\u7684\u7c7b\u3002\u7136\u540e\u5728\u7c7b\u4e2d\u5b9e\u73b0 run()\u65b9\u6cd5\uff0c\u5e76\u4e14\u5c06\u8be5\u7c7b\u7684\u5b9e\u4f8b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u4e00\u4e2a\u7ebf\u7a0b\u7c7b\uff0c\u6700\u540e\uff0c\u8c03\u7528 Thread.start()\u542f\u52a8\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class MyRunnable implements Runnable {\n\n  @Override\n  public void run() {\n    System.out.println(\"MyRunnable implements Runnable!\");\n  }\n\n  public static void main(String[] args) {\n    MyRunnable runnable = new MyRunnable();\n    Thread thread = new Thread(runnable);\n    thread.start();\n  }\n}<\/pre>\n\n\n\n<p>\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MyRunnable implements Runnable!<\/pre>\n\n\n\n<p><strong>2.3\u3001Callable\/Future<\/strong><\/p>\n\n\n\n<p>\u521b\u5efa\u7ebf\u7a0b\u7684\u7b2c\u4e09\u79cd\u65b9\u6cd5\u662f\u5b9e\u73b0 Callable \u63a5\u53e3\uff0c\u7136\u540e\u901a\u8fc7 Future \u83b7\u53d6\u7ed3\u679c\u503c\u3002\u4e00\u822c\u6765\u8bf4 Callable \u9700\u8981\u914d\u5408\u7ebf\u7a0b\u6c60\u5de5\u5177\u7c7b ExecutorService \u6765\u4f7f\u7528\uff0c\u5982\u4e0b\u4ee3\u7801\uff0c\u5b9a\u4e49\u4e00\u4e2a MyTask \u7c7b\uff0c\u7136\u540e\u8ba9\u8be5\u7c7b\u53bb\u5b9e\u73b0 Callable \u63a5\u53e3\uff0c\u5e76\u4e14\u5b9e\u73b0 call()\u65b9\u6cd5\uff0c\u6700\u540e\u901a\u8fc7 executor.submit()\u63d0\u4ea4\u4efb\u52a1\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class MyThread implements Callable {\n  @Override\n  public Object call() throws Exception {\n    return \"MyThread implements Callable\";\n  }\n\n  public static void main(String[] args) throws Exception {\n    ExecutorService executor = Executors.newCachedThreadPool();\n    MyTask task = new MyTask();\n    Future&lt;Integer> future = executor.submit(task);\n    \/\/ get()\u65b9\u6cd5\u4f1a\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\uff0c\u76f4\u5230\u5f97\u5230\u7ed3\u679c\u3002\n    System.out.println(future.get());\n  }\n}<\/pre>\n\n\n\n<p>\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MyThread implements Callable!<\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u770b\u770b Callable \u7684\u6e90\u7801\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"652\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77-1024x652.png\" alt=\"\" class=\"wp-image-17600\" style=\"width:570px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77-1024x652.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77-300x191.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77-768x489.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77-830x528.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77-230x146.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77-350x223.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77-480x306.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-77.png 1310w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u901a\u8fc7 Callable \u7684\u6e90\u7801\u53ef\u4ee5\u5f97\u77e5\uff0c Callable \u662f\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u5e76\u4e14\u53ea\u6709\u4e00\u4e2a call() \u62bd\u8c61\u65b9\u6cd5\uff0cCallable \u63a5\u53e3\u4e0e Runnable \u7c7b\u4f3c\uff0c\u4e0d\u8fc7\uff0cRunnable \u4e0d\u8fd4\u56de\u7ed3\u679c\uff0c\u4e5f\u4e0d\u80fd\u629b\u51fa\u68c0\u67e5\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u518d\u770b\u4e0b Future.get()\u662f\u5982\u4f55\u83b7\u53d6\u6570\u636e\u7684\uff1f<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"949\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79-1024x949.png\" alt=\"\" class=\"wp-image-17602\" style=\"width:496px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79-1024x949.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79-300x278.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79-768x712.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79-830x769.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79-230x213.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79-350x324.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79-480x445.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-79.png 1340w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u901a\u8fc7\u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0cget()\u662f\u4e00\u4e2a\u963b\u585e\u65b9\u6cd5\uff0c\u4f1a\u4e00\u76f4\u7b49\u5f85\u76f4\u5230\u5904\u7406\u5b8c\u6210\u8fd4\u56de\u7ed3\u679c\u6216\u8005\u7b49\u5f85\u8d85\u65f6\u4e2d\u65ad\u3002<\/p>\n\n\n\n<p><strong>Thread\u3001Runnable\u3001Callable\/Future \u6bd4\u8f83<\/strong><\/p>\n\n\n\n<ul>\n<li>\u7531\u4e8e Java \u662f\u5355\u7ee7\u627f\uff0c\u56e0\u6b64 Runnable \u63a5\u53e3\u6bd4 Thread \u66f4\u7075\u6d3b\uff1b<\/li>\n\n\n\n<li>Runnable \u63a5\u53e3\u66f4\u7b26\u5408\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff1b<\/li>\n\n\n\n<li>Thread \u7c7b\u7684\u65b9\u6cd5\u6bd4\u8f83\u4e30\u5bcc\uff0c\u672c\u8eab\u4e5f\u5b9e\u73b0\u4e86 Runnable \u63a5\u53e3\uff0c\u800c Runnable \u63a5\u53e3\u66f4\u4e3a\u8f7b\u91cf\uff1b<\/li>\n\n\n\n<li>Thread\u3001Runnable \u90fd\u65e0\u6cd5\u8fd4\u56de\u503c\uff0cCallable\/Future \u53ef\u4ee5\u62ff\u5230\u7ebf\u7a0b\u6267\u884c\u7684\u7ed3\u679c\u503c\uff1b<\/li>\n\n\n\n<li>Future.get()\u662f\u901a\u8fc7\u963b\u585e\u65b9\u6cd5\u6765\u83b7\u53d6\u7ed3\u679c\u503c\uff1b<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4e09\u3001\u7ebf\u7a0b\u7684\u72b6\u6001<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Java \u4e2d\uff0c\u7ebf\u7a0b\u7684\u72b6\u6001\u4e5f\u53eb\u505a\u7ebf\u7a0b\u751f\u547d\u5468\u671f\uff0c\u4e3b\u8981\u6709 6 \u79cd\uff0c\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class Thread implements Runnable {\n  public enum State {  \n    NEW,\n    RUNNABLE,\n    BLOCKED,\n    WAITING,\n    TIMED_WAITING,\n    TERMINATED;\n  }\n}<\/pre>\n\n\n\n<p><strong>3.1\u3001NEW<\/strong><\/p>\n\n\n\n<p>NEW\uff1a\u5c1a\u672a\u542f\u52a8\u7684\u7ebf\u7a0b\u5904\u4e8e\u6b64\u72b6\u6001\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u9a8c\u8bc1 NEW\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\npublic void testStateNew(){ \n  Thread thread = new Thread(()->{});\n  System.out.println(thread.getState()); \/\/ \u8f93\u51fa NEW\n}<\/pre>\n\n\n\n<p><strong>3.2\u3001RUNNABLE<\/strong><\/p>\n\n\n\n<p>RUNNABLE\uff1a\u5904\u4e8e RUNNABLE(\u53ef\u8fd0\u884c)\u72b6\u6001\u7684\u7ebf\u7a0b\u5728 Java \u865a\u62df\u673a\u4e2d\u6267\u884c\uff0c\u4f46\u5b83\u53ef\u80fd\u6b63\u5728\u7b49\u5f85\u6765\u81ea\u64cd\u4f5c\u7cfb\u7edf\u7684\u5176\u4ed6\u8d44\u6e90\uff0c\u4f8b\u5982\u5904\u7406\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u9a8c\u8bc1 RUNNABLE\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\npublic void testStateNew(){\n  \/\/ \u53ea\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5e76\u6ca1\u6709\u8c03\u7528 start()\u65b9\u6cd5\n  Thread thread = new Thread(()->{});\n  thread.start(); \n  System.out.println(thread.getState());  \/\/ \u8f93\u51fa RUNNABLE\n}<\/pre>\n\n\n\n<p><strong>3.3\u3001BLOCKED<\/strong><\/p>\n\n\n\n<p>BLOCKED\uff1a\u5904\u4e8e\u963b\u585e\u72b6\u6001\u7684\u7ebf\u7a0b\u6b63\u5728\u7b49\u5f85\u76d1\u89c6\u5668\u9501\u8fdb\u5165\u540c\u6b65\u5757\/\u65b9\u6cd5\u6216\u8c03\u7528 Object.wait \u540e\u91cd\u65b0\u8fdb\u5165\u540c\u6b65\u5757\/\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><strong>3.4\u3001WAITING<\/strong><\/p>\n\n\n\n<p>WAITING\uff1a\u7b49\u5f85\u72b6\u6001\u3002\u8c03\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff0c\u7ebf\u7a0b\u5c31\u4f1a\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\uff1a<\/p>\n\n\n\n<ul>\n<li>Object.wait()<\/li>\n\n\n\n<li>Thread.join()<\/li>\n\n\n\n<li>LockSupport.park()<\/li>\n<\/ul>\n\n\n\n<p>\u5904\u4e8e WAITING \u7b49\u5f85\u72b6\u6001\u7684\u7ebf\u7a0b\uff0c\u9700\u8981\u5176\u4ed6\u7ebf\u7a0b\u5bf9\u5176\u5bf9\u8c61\u6267\u884c\u4e0b\u9762\u4efb\u4e00\u65b9\u6cd5\u624d\u80fd\u5207\u6362\u6210 RUNNABLE \u72b6\u6001\uff1a<\/p>\n\n\n\n<ul><\/ul>\n\n\n\n<ul>\n<li>notify()<\/li>\n\n\n\n<li>notifyAll()<\/li>\n\n\n\n<li>LockSupport.unpark()<\/li>\n<\/ul>\n\n\n\n<p><strong>3.5\u3001TIMED_WAITING<\/strong><\/p>\n\n\n\n<p>TIMED_WAITING\uff1a\u8d85\u65f6\u7b49\u5f85\u72b6\u6001\u3002\u7ebf\u7a0b\u7b49\u5f85\u7ed9\u5b9a\u7684\u65f6\u95f4\u540e\u4f1a\u88ab\u81ea\u52a8\u5524\u9192\u3002\u8c03\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4f1a\u4f7f\u7ebf\u7a0b\u8fdb\u5165\u8d85\u65f6\u7b49\u5f85\u72b6\u6001\uff1a<\/p>\n\n\n\n<ul>\n<li>Thread.sleep(long millis)\uff1a\u4f7f\u5f53\u524d\u7ebf\u7a0b\u7761\u7720\u6307\u5b9a\u65f6\u95f4\uff1b<\/li>\n\n\n\n<li>Object.wait(long timeout)\uff1a\u7ebf\u7a0b\u4f11\u7720\u6307\u5b9a\u65f6\u95f4\uff0c\u7b49\u5f85\u671f\u95f4\u53ef\u4ee5\u901a\u8fc7 notify()\/notifyAll()\u5524\u9192\uff1b<\/li>\n\n\n\n<li>Thread.join(long millis)\uff1a\u7b49\u5f85\u5f53\u524d\u7ebf\u7a0b\u6700\u591a\u6267\u884c millis \u6beb\u79d2\uff0c\u5982\u679c millis \u4e3a 0\uff0c\u5219\u4f1a\u4e00\u76f4\u6267\u884c\uff1b<\/li>\n\n\n\n<li>LockSupport.parkNanos(long nanos)\uff1a\u9664\u975e\u83b7\u5f97\u8c03\u7528\u8bb8\u53ef\uff0c\u5426\u5219\u7981\u7528\u5f53\u524d\u7ebf\u7a0b\u8fdb\u884c\u7ebf\u7a0b\u8c03\u5ea6\u6307\u5b9a\u65f6\u95f4\uff1b<\/li>\n\n\n\n<li>LockSupport.parkUntil(long deadline)\uff1a\u540c\u4e0a\uff0c\u4e5f\u662f\u7981\u6b62\u7ebf\u7a0b\u8fdb\u884c\u8c03\u5ea6\u6307\u5b9a\u65f6\u95f4\uff1b<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<p>\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u9a8c\u8bc1\u72b6\u6001\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class ThreadBlocked extends Thread {\n  @Override \n  public void run() { \n    while (true) {\n      try {\n        \/\/ \u7ebf\u7a0b\u8fdb\u5165 TIMED_WAITING \u72b6\u6001\n        TimeUnit.SECONDS.sleep(100);\n      } catch (InterruptedException e) { \n          e.printStackTrace();\n      }\n    }\n  }\n\n  public static void main(String[] args) {\n    Thread threadBlocked = new Thread(new ThreadBlocked(), \"ThreadBlocked\");\n    threadBlocked.start(); \n  }\n}<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u8ba9\u7ebf\u7a0b sleep \u7761\u7720 100s\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7 jstack pid \u6307\u4ee4\u67e5\u770b\u5806\u6808\u4fe1\u606f\uff0c\u4ece\u800c\u89c2\u6d4b\u7ebf\u7a0b\u72b6\u6001\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"173\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80-1024x173.png\" alt=\"\" class=\"wp-image-17607\" style=\"width:490px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80-1024x173.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80-300x51.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80-768x130.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80-830x140.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80-230x39.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80-350x59.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80-480x81.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-80.png 1338w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>3.6\u3001TERMINATED<\/strong><\/p>\n\n\n\n<p>TERMINATED\uff1a\u5df2\u7ec8\u6b62\u72b6\u6001\uff0c\u4ee3\u8868\u7ebf\u7a0b\u5df2\u5b8c\u6210\u6267\u884c\u3002<\/p>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u6587\u7684\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u628a Java\u7ebf\u7a0b\u7684 6 \u79cd\u72b6\u6001\u53ca\u5176\u8f6c\u6362\u5173\u7cfb\u603b\u7ed3\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"544\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81-1024x544.png\" alt=\"\" class=\"wp-image-17608\" style=\"width:566px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81-1024x544.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81-300x159.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81-768x408.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81-830x441.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81-230x122.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81-350x186.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81-480x255.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-81.png 1348w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u56db\u3001\u7ebf\u7a0b\u7684\u5de5\u4f5c\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>4.1\u3001\u542f\u52a8\u7ebf\u7a0b<\/strong><\/p>\n\n\n\n<p>\u5728\u4e0a\u6587\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 Thread.start()\u65b9\u6cd5\u5c31\u80fd\u542f\u52a8\u7ebf\u7a0b\uff0c\u90a3\u4e48 start()\u65b9\u6cd5\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u5462\uff1f<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u6765\u5206\u6790 Thread\u548c Runnable\u7684\u6e90\u7801\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"871\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83-1024x871.png\" alt=\"\" class=\"wp-image-17610\" style=\"width:570px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83-1024x871.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83-300x255.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83-768x654.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83-830x706.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83-230x196.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83-350x298.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83-480x409.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-83.png 1316w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"687\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84-1024x687.png\" alt=\"\" class=\"wp-image-17611\" style=\"width:562px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84-1024x687.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84-300x201.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84-768x516.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84-830x557.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84-230x154.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84-350x235.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84-480x322.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-84.png 1290w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u901a\u8fc7 Runnable \u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0cRunnable \u662f\u4e00\u4e2a\u51fd\u6570\u5f0f\u63a5\u53e3\uff0c\u5185\u90e8\u53ea\u6709\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5 run()\uff0c\u4efb\u4f55\u5b9e\u73b0\u8be5\u63a5\u53e3\u7684\u7ebf\u7a0b\u7c7b\u90fd\u5fc5\u987b\u5b9e\u73b0 run()\u65b9\u6cd5\uff0c\u800c Thread \u6e90\u7801\u663e\u793a\uff0cThread \u672c\u8eab\u5b9e\u73b0\u4e86 Runnable \u63a5\u53e3\uff0c\u5e76\u4e14\u5b9e\u73b0\u4e86&nbsp;Runnable\u63a5\u53e3\u7684run()\u65b9\u6cd5\uff0cThread.start()\u6700\u7ec8\u8c03\u7528\u4e86native start0()\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u8bf4 start0()\u662f\u5728 JVM \u4e2d\u5b9e\u73b0\u7684\uff0c\u56e0\u6b64\uff0c\u7ee7\u7eed\u67e5\u9605 JVM \u7684\u6e90\u7801\uff0c\u770b\u770b start0()\u65b9\u6cd5\u5230\u5e95\u505a\u4e86\u4ec0\u4e48\u3002<\/p>\n\n\n\n<p>\u4ece JVM\u7684Thread.c \u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0cstart0()\u4f1a\u6620\u5c04\u5230 JVM \u4e2d\u7684 JVM_StartThread \u65b9\u6cd5\uff0c\u6620\u5c04\u5173\u7cfb\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"591\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85-1024x591.png\" alt=\"\" class=\"wp-image-17612\" style=\"width:548px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85-1024x591.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85-300x173.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85-768x443.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85-830x479.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85-230x133.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85-350x202.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85-480x277.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-85.png 1366w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5728 jvm.cpp \u6e90\u7801\u4e2d\uff0cJVM_StartThread \u65b9\u6cd5\u6e90\u7801\u5b9e\u73b0\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"823\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87-1024x823.png\" alt=\"\" class=\"wp-image-17614\" style=\"width:554px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87-1024x823.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87-300x241.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87-768x617.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87-830x667.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87-230x185.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87-350x281.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87-480x386.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-87.png 1110w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u00a0\u901a\u8fc7\u6e90\u7801\uff0c\u53ef\u4ee5 JVM_StartThread \u5b9e\u73b0\u903b\u8f91\u603b\u7ed3\u4e3a\u4e0b\u5217\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ul>\n<li>1. \u5224\u65ad Java \u7ebf\u7a0b\u662f\u5426\u5df2\u7ecf\u542f\u52a8\uff0c\u5982\u679c\u542f\u52a8\u8fc7\uff0c\u5219\u629b\u5f02\u5e38\uff1b<\/li>\n\n\n\n<li>2. \u5982\u679c Java \u7ebf\u7a0b\u6ca1\u6709\u542f\u52a8\u8fc7\uff0c\u5219\u901a\u8fc7 new JavaThread()\u521b\u5efa Java \u7ebf\u7a0b\uff1b<\/li>\n\n\n\n<li>3. \u8c03\u7528 Thread::start(native_thread); \u542f\u52a8\u6b65\u9aa4 2 \u521b\u5efa\u7684\u7ebf\u7a0b\uff1b<\/li>\n<\/ul>\n\n\n\n<p>\u63a5\u4e0b\u6765\u8981\u5206\u6790JVM\u4e2d\u7ebf\u7a0b\u662f\u5982\u4f55\u521b\u5efa\u7684\uff0c\u6e90\u7801\u4f4d\u4e8e javaThread.cpp \u4e2d\uff0c\u901a\u8fc7 new JavaThread()\u521b\u5efa\u7ebf\u7a0b\u5b9e\u9645\u662f\u4f1a\u8c03\u7528 os::create_thread()\u65b9\u6cd5\u6765\u521b\u5efa Java \u7ebf\u7a0b\u5bf9\u5e94\u7684\u5185\u6838\u7ebf\u7a0b\uff0c\u5176\u6e90\u7801\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"579\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89-1024x579.png\" alt=\"\" class=\"wp-image-17616\" style=\"width:554px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89-1024x579.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89-300x170.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89-768x434.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89-830x469.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89-230x130.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89-350x198.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89-480x271.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-89.png 1404w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>os::create_thread()\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u5462\uff1f\u6211\u4eec\u5c06\u76ee\u5149\u8f6c\u5411\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u6e90\u7801\uff0c\u8fd9\u91cc\u4ee5Linux\u5185\u6838\u7684os_linux.cpp \u6e90\u7801\u4e3a\u4f8b\uff0cos::create_thread() \u662f\u5229\u7528 pthread_create()\u6765\u521b\u5efa\u5185\u6838\u7ebf\u7a0b\uff0c\u65b9\u6cd5\u5171 4 \u4e2a\u53c2\u6570\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"719\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91-1024x719.png\" alt=\"\" class=\"wp-image-17618\" style=\"width:544px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91-1024x719.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91-300x211.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91-768x539.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91-830x583.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91-230x161.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91-350x246.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91-480x337.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-91.png 1336w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5230\u6b64\uff0c\u7ebf\u7a0b\u5df2\u7ecf\u521b\u5efa\u5b8c\u6210\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u5982\u4f55\u542f\u52a8\u7ebf\u7a0b\uff0c\u5728 JVM\u4e2d\u8c03\u7528 \u7684\u662fThread::start(native_thread)\uff0cstart()\u65b9\u6cd5\u4f1a\u8c03\u7528\u5e73\u53f0\u542f\u52a8\u7ebf\u7a0b\u7684\u65b9\u6cd5: os::start_thread(thread);\uff0c\u6700\u7ec8\u8f6c\u5411 thread.cpp \u6587\u4ef6\u4e2d\u7684 JavaThread::run()\u65b9\u6cd5\uff0c\u6e90\u7801\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"439\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92-1024x439.png\" alt=\"\" class=\"wp-image-17619\" style=\"width:544px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92-1024x439.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92-300x129.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92-768x329.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92-830x356.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92-230x99.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92-350x150.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92-480x206.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-92.png 1284w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5728 thread.cpp \u6e90\u7801\u4e2d\uff0cthread->call_run() \u8c03\u7528\u4e86 thread_main_inner()\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"430\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94-1024x430.png\" alt=\"\" class=\"wp-image-17621\" style=\"width:558px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94-1024x430.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94-300x126.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94-768x323.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94-830x349.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94-230x97.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94-350x147.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94-480x202.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-94.png 1290w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5728 javaThread.cpp \u6e90\u7801\u4e2d\uff0cthread_main_inner()\u65b9\u6cd5\u4e2d\u7684 this->entry_point(this, this)\u8fd4\u56de\u7684\u5176\u5b9e\u5c31\u662f\u5728 new JavaThread(&amp;thread_entry, sz) \u65f6\u4f20\u5165\u7684 thread_entry\u3002\u56e0\u6b64\uff0cthread_main_inner()\u5c31\u76f8\u5f53\u4e8e\u8c03\u7528\u4e86 thread_entry(this\uff0cthis)\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"700\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96-1024x700.png\" alt=\"\" class=\"wp-image-17623\" style=\"width:546px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96-1024x700.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96-300x205.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96-768x525.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96-830x567.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96-230x157.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96-350x239.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96-480x328.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-96.png 1364w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5728 jvm.cpp \u6e90\u7801\u4e2d\uff0cthread_entry \u65b9\u6cd5\u4e2d\uff0cJVM \u901a\u8fc7 JavaCalls \u6a21\u5757\u8c03\u7528 call_virtual\u65b9\u6cd5\uff0c\u800c JavaCalls \u662f C++\u8c03\u7528 Java\u4ee3\u7801\u7684\u4e00\u4e2a\u529f\u80fd\u6a21\u5757\uff0c\u6240\u4ee5\uff0c\u901a\u8fc7JavaCalls\u6a21\u5757\u6700\u540e\u80fd\u8c03\u7528\u5230 Java \u4e2d\u7684 run()\u65b9\u6cd5\uff0c\u6e90\u7801\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"482\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98-1024x482.png\" alt=\"\" class=\"wp-image-17625\" style=\"width:538px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98-1024x482.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98-300x141.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98-768x362.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98-830x391.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98-230x108.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98-350x165.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98-480x226.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-98.png 1372w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u901a\u8fc7\u4e0a\u6587\u5bf9 JDK \u548c JVM \u7684\u6e90\u7801\u5206\u6790\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\uff1astart()\u65b9\u6cd5\u5148\u662f JDK\u901a\u8fc7 JNI\u7684\u65b9\u5f0f\u8c03\u7528 JVM\u672c\u5730C++\u65b9\u6cd5\uff0c\u7136\u540e JVM \u518d\u548c os\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\uff0c\u6700\u540e JVM \u518d\u56de\u8c03 Java\u7684run()\u65b9\u6cd5\uff0c\u5b8c\u6210\u4e86\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\uff0c\u6574\u4e2a\u8fc7\u7a0b\u6574\u7406\u6210\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"326\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99-1024x326.png\" alt=\"\" class=\"wp-image-17626\" style=\"width:550px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99-1024x326.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99-300x95.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99-768x244.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99-830x264.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99-230x73.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99-350x111.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99-480x153.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-99.png 1150w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u4e0a\u6587\u63cf\u8ff0\u4e86\u7ebf\u7a0b\u7684\u542f\u52a8\u8fc7\u7a0b\uff0c\u90a3\u4e48\uff0c\u7ebf\u7a0b\u662f\u5982\u4f55\u505c\u6b62\u7684\u5462\uff1f<\/strong><\/p>\n\n\n\n<p><strong>4.2\u3001\u505c\u6b62\u7ebf\u7a0b<\/strong><\/p>\n\n\n\n<p>\u8bf4\u8d77\u505c\u6b62\u7ebf\u7a0b\uff0c\u60f3\u5fc5\u5927\u5bb6\u5f88\u81ea\u7136\u4f1a\u60f3\u5230 Thread.stop() \u6216\u8005 Thread.suspend()\uff0c\u4e0d\u8fc7\uff0c\u5f88\u9057\u61be\u7684\u662f\uff0cThread.stop() \u548c Thread.suspend()\u4ece JDK 1.2\u5c31\u5df2\u7ecf\u88ab\u5e9f\u5f03\u4e86\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u5b89\u5168\uff0c\u4f7f\u7528\u5b83\u4eec\u6765\u505c\u6b62\u7ebf\u7a0b\u4f1a\u5bfc\u81f4\u5df2\u9501\u5b9a\u7684\u76d1\u89c6\u5668\u4f1a\u88ab\u89e3\u9501\uff0c\u56e0\u6b64\u63a8\u8350\u4f7f\u7528 Thread.interrupt()\u6765\u4e2d\u65ad\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528 interrupt()\u65b9\u6cd5\u6709\u4e24\u4e2a\u91cd\u8981\u4f5c\u7528\uff1a<\/p>\n\n\n\n<ul>\n<li>\u8bbe\u7f6e\u4e00\u4e2a\u7ebf\u7a0b\u7ec8\u6b62\u7684\u6807\u8bb0\uff08\u5171\u4eab\u53d8\u91cf\u7684\u503c true\uff09<\/li>\n\n\n\n<li>\u5524\u9192\u5904\u4e8e\u963b\u585e\u72b6\u6001\u4e0b\u7684\u7ebf\u7a0b<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u4e0b\u4ee3\u7801\uff0c\u5728 run()\u65b9\u6cd5\u4e2d\u8ba9\u7ebf\u7a0b\u7761\u7720 300s\uff0c\u7136\u540e\u5728\u7ebf\u7a0b\u7761\u7720\u8fc7\u7a0b\u4e2d\u8c03\u7528\u7ebf\u7a0b\u4e2d\u65ad\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class MyThread extends Thread {\n    public static void main(String[] args) {\n        MyThread thread = new MyThread();\n        thread.start();\n        \/\/ \u8c03\u7528interrupt\n        thread.interrupt();\n    }\n\n    @Override\n    public void run() {\n        try {\n            TimeUnit.SECONDS.sleep(300);\n        } catch (InterruptedException e) {\n            \/\/ \u6253\u5370\u51fa java.lang.InterruptedException: sleep interrupted\n            e.printStackTrace(); \n        }\n    }\n}<\/pre>\n\n\n\n<p>\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u53d1\u73b0\u7ebf\u7a0b\u88ab\u6b63\u5e38\u4e2d\u65ad\uff0ccatch\u4e2d\u4f1a\u6253\u5370\u51fa\u4ee5\u4e0b\u5806\u6808\u4fe1\u606f\uff1ajava.lang.InterruptedException:&nbsp;sleep&nbsp;interrupted<\/p>\n\n\n\n<p>\u90a3\u4e48\uff0cThread.interrupt() \u662f\u5982\u4f55\u4e2d\u65ad\u7ebf\u7a0b\u7684\uff1f<\/p>\n\n\n\n<p>\u6211\u4eec\u4ece Thread.interrupt() \u8fdb\u884c\u5206\u6790\uff0c\u53d1\u73b0 interrupt()\u8c03\u7528\u4e86 native interrupt0()\uff0c\u4ece\u800c\u8f6c\u5411\u4e86 JVM\u7684\u5b9e\u73b0\uff0cinterrupt()\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"705\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100-1024x705.png\" alt=\"\" class=\"wp-image-17627\" style=\"width:412px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100-1024x705.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100-300x206.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100-768x528.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100-830x571.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100-230x158.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100-350x241.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100-480x330.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-100.png 1218w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5728 JVM \u7684 Thread.c \u6e90\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 interrupt0()\u6620\u5c04\u5230 JVM_Interrupt\u65b9\u6cd5\uff0c\u6e90\u7801\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"549\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102-1024x549.png\" alt=\"\" class=\"wp-image-17629\" style=\"width:596px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102-1024x549.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102-300x161.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102-768x412.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102-830x445.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102-230x123.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102-350x188.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102-480x257.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-102.png 1372w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u63a5\u7740\u6211\u4eec\u8fdb\u5165 JVM_Interrupt \u65b9\u6cd5\u7684\u6e90\u7801\u7c7bjvm.cpp\uff0c \u5728 JVM_Interrupt\u4e2d\u4f1a\u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u5b58\u6d3b\uff0c\u5982\u679c\u5b58\u6d3b\uff0c\u624d\u4f1a\u8c03\u7528 JavaThread.interrupt()\u65b9\u6cd5\u8fdb\u884c\u7ebf\u7a0b\u4e2d\u65ad\u64cd\u4f5c\uff0c\u5426\u5219\u76f4\u63a5\u7ed3\u675f\u65b9\u6cd5\uff0c\u6e90\u7801\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"363\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103-1024x363.png\" alt=\"\" class=\"wp-image-17630\" style=\"width:588px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103-1024x363.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103-300x106.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103-768x272.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103-830x294.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103-230x82.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103-350x124.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103-480x170.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-103.png 1376w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u8fdb\u5165 javaThread.cpp \uff0cinterrupt() \u65b9\u6cd5\u9488\u5bf9\u7ebf\u7a0b\u4e0d\u540c\u7684\u72b6\u6001\uff0c\u4f1a\u91c7\u53d6\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u4e2d\u65ad\uff0c\u5177\u4f53\u5b9e\u73b0\u903b\u8f91\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"761\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105-1024x761.png\" alt=\"\" class=\"wp-image-17632\" style=\"width:568px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105-1024x761.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105-300x223.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105-768x571.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105-830x617.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105-230x171.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105-350x260.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105-480x357.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-105.png 1388w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u4e3a\u4ec0\u4e48interrupt()\u65b9\u6cd5\u9700\u8981\u4f7f\u7528 unpark()\u5462\uff1f\u8fd9\u662f\u56e0\u4e3a\u5728\u6f14\u793a\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u662fThread.sleep()\u65b9\u6cd5\u8fdb\u884c\u7ebf\u7a0b\u7761\u7720\uff0c\u800c sleep()\u6700\u7ec8\u4f1a\u8c03\u7528park \u51fd\u6570\uff0c\u6e90\u7801\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"506\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106-1024x506.png\" alt=\"\" class=\"wp-image-17633\" style=\"width:550px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106-1024x506.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106-300x148.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106-768x380.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106-830x410.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106-350x173.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106-480x237.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-106.png 1376w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"949\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107-1024x949.png\" alt=\"\" class=\"wp-image-17634\" style=\"width:536px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107-1024x949.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107-300x278.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107-768x712.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107-830x769.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107-230x213.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107-350x324.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107-480x445.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-107.png 1370w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u6240\u4ee5 interrupt()\u9700\u8981\u4f7f\u7528unpark()\u5524\u9192\u7761\u7720\u7684\u7ebf\u7a0b\uff0cpark()\u65b9\u6cd5\u5176\u5b9e\u662f\u8c03\u7528\u7cfb\u7edf\u5c42\u9762\u7684\u9501\u6302\u8d77\u7ebf\u7a0b\uff0c\u800cunpark()\u65b9\u6cd5\u8c03\u7528\u7cfb\u7edf\u5c42\u9762\u7684\u5524\u9192\u6761\u4ef6\u53d8\u91cf\u8fbe\u5230\u5524\u9192\u7ebf\u7a0b\u7684\u76ee\u7684\uff0cSleep()\u65b9\u6cd5\u68c0\u6d4b\u5230\u7ebf\u7a0b\u4e2d\u65ad\u6807\u8bc6\uff0c\u629b\u51fa sleep interrupted\u5f02\u5e38\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1012\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109-1024x1012.png\" alt=\"\" class=\"wp-image-17636\" style=\"width:552px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109-1024x1012.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109-300x297.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109-768x759.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109-830x821.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109-230x227.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109-350x346.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109-480x475.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-109.png 1398w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>4.3\u3001start() \u548c run()\u6bd4\u8f83<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">start()\u548c run()\u65b9\u6cd5\u662f\u5b9e\u9645\u5de5\u4f5c\u4e2d\u6bd4\u8f83\u5bb9\u6613\u641e\u6df7\u7684\u4e24\u4e2a\u65b9\u6cd5\uff0c\u56e0\u6b64\u8fd9\u91cc\u7279\u522b\u62ff\u51fa\u6765\u8fdb\u884c\u6bd4\u8f83\uff0c\u6211\u4eec\u5148\u770b\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class MyThread extends Thread {\n  @Override\n  public void run() {\n    System.out.println(\"ThreadName:\" + Thread.currentThread().getName());\n  }\n\n  public static void main(String[] args) {\n    MyThread thread = new MyThread();\n    thread.run();   \/\/ \u8f93\u51fa ThreadName:main\n    thread.run();   \/\/ \u8f93\u51fa ThreadName:main\n    thread.start(); \/\/ \u8f93\u51fa ThreadName:Thread-0\n    thread.start(); \/\/ \u629b\u51fa java.lang.IllegalThreadStateException \u5f02\u5e38\n  }\n}<\/pre>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\uff1a<\/p>\n\n\n\n<ul>\n<li>start(): \u4f1a\u542f\u52a8\u4e00\u4e2a\u65b0\u7ebf\u7a0b\uff0c\u5728\u65b0\u7ebf\u7a0b\u91cc\u4f1a\u6267\u884c\u76f8\u5e94\u7684 run()\u65b9\u6cd5\uff1bstart()\u4e0d\u80fd\u88ab\u91cd\u590d\u8c03\u7528\uff0cJVM\u6e90\u7801\u4f1a\u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u5df2\u7ecf\u542f\u52a8\u8fc7\u3002<\/li>\n\n\n\n<li>run() : \u76f4\u63a5\u8c03\u7528\u5b9e\u4f8b\u7684 run()\u65b9\u6cd5\uff0c run()\u5c31\u662f\u4e00\u4e2a\u666e\u901a\u65b9\u6cd5\uff0c\u4e0d\u4f1a\u542f\u52a8\u65b0\u7ebf\u7a0b\uff0c\u800c\u662f\u76f4\u63a5\u5728\u5f53\u524d\u7ebf\u7a0b\u4e2d\u6267\u884c\uff1brun()\u53ef\u91cd\u590d\u8c03\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>native void yield()<\/td><td>\u5f53\u524d\u7ebf\u7a0b\u5c06\u653e\u5f03\u5bf9\u5904\u7406\u5668\u7684\u4f7f\u7528\uff0c\u4e00\u822c\u8c03\u8bd5\u548c\u6d4b\u8bd5\u4e2d\u6bd4\u8f83\u5e38\u7528<\/td><\/tr><tr><td>native void sleep()<\/td><td>\u7ebf\u7a0b\u7761\u7720\u6307\u5b9a\u7684\u65f6\u957f\uff0c\u7ebf\u7a0b\u4e0d\u4f1a\u91ca\u653e\u5bf9\u76d1\u89c6\u5668\u7684\u6240\u6709\u6743<\/td><\/tr><tr><td>synchronizedvoid join()<\/td><td>\u6700\u591a\u7b49\u5f85\u6307\u5b9a\u6beb\u79d2\u540e\u8ba9\u7ebf\u7a0b\u7ec8\u6b62\u30020\uff1a\u6c38\u8fdc\u7b49\u5f85<\/td><\/tr><tr><td>void setDaemon()<\/td><td>\u7528\u5c06\u7ebf\u7a0b\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u6216\u7528\u6237\u7ebf\u7a0b\uff0c\u9700\u5728\u7ebf\u7a0b\u542f\u52a8\u524d\u8bbe\u7f6e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5bf9\u7167 JVM\u6e90\u7801\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728 Thread\u7c7b\u4e2d\u7684\u5f88\u591a\u65b9\u6cd5\u6700\u7ec8\u90fd\u4f1a\u6620\u5c04\u5230 JVM\u5185\u90e8\u7684 C++\u65b9\u6cd5\uff0c\u751a\u81f3\u8fd8\u9700\u8981\u548c OS\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n\n\n\n<p><strong>\u4e94\u3001Java\u7ebf\u7a0b\u7ec4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>ThreadGroup\uff1a\u7ebf\u7a0b\u7ec4\uff0c\u5728 Java\u4e2d\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5fc5\u987b\u5c5e\u4e8e\u4e00\u4e2a\u7ec4\uff0c\u4e0d\u80fd\u72ec\u7acb\u5b58\u5728\uff0c\u5982\u679c\u5728 new Thread()\u65f6\u6ca1\u6709\u663e\u5f0f\u6307\u5b9a\u7ebf\u7a0b\u7ec4\uff0c\u90a3\u4e48\u9ed8\u8ba4\u5c06\u7236\u7ebf\u7a0b\uff08\u5f53\u524d\u6267\u884cnew Thread()\u7684\u7ebf\u7a0b\uff09\u7ebf\u7a0b\u7ec4\u8bbe\u7f6e\u4e3a\u81ea\u5df1\u7684\u7ebf\u7a0b\u7ec4\u3002public static void main(String[] args)\u65b9\u6cd5\u7684\u7ebf\u7a0b\u7ec4\u9ed8\u8ba4\u4e3a main\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4ee5\u4e00\u4e2a\u5b9e\u4f8b\u6765\u611f\u53d7\u4e0b\u7ebf\u7a0b\u7ec4\uff0c\u5982\u4e0b\u4ee3\u7801\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u7c7b\uff0c\u7136\u540e\u83b7\u53d6\u7ebf\u7a0b\u5bf9\u5e94\u7684\u7ebf\u7a0b\u7ec4\u540d\u79f0\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class MyThread extends Thread { \n  @Override\n  public void run() { \n    System.out.println(\"MyThread extends Thread!\"); \n  }\n\n  public static void main(String[] args) { \n    MyThread thread = new MyThread(); \n    thread.start(); \n    \/\/ \u8f93\u51fa threadGroup:main \n    System.out.println(\"threadGroup:\" + thread.getThreadGroup().getName()); \n  }\n}<\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u7ed9 MyThread\u7ebf\u7a0b\u5b9e\u4f8b\u8bbe\u7f6e\u8fc7\u7ebf\u7a0b\u7ec4\u4fe1\u606f\uff0c\u90a3\u4e48\uff0c\u83b7\u53d6\u5230\u7684\u7ebf\u7a0b\u7ec4\u662f\u5728\u4ec0\u4e48\u65f6\u5019\u8bbe\u7f6e\u7684\u5462\uff1f<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u770b\u770b Thread\u7c7b\u5728\u521b\u5efa\u7ebf\u7a0b\u5b9e\u4f8b\u65f6\u505a\u4e86\u4ec0\u4e48\uff0c\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class Thread implements Runnable {\n    \/\/ \u65e0\u53c2\u6784\u9020\u5668 \u521b\u5efa\u7ebf\u7a0b\n  public Thread() {\n    this(null, null, \"Thread-\" + nextThreadNum(), 0);\n  }\n\n  \/\/ \u6307\u5b9a\u7ebf\u7a0b\u7ec4\uff0cRunnable\u548c\u7ebf\u7a0b\u540d\u6765 \u521b\u5efa\u7ebf\u7a0b\n  public Thread(ThreadGroup group, Runnable target, String name, long stackSize) {\n    this(group, target, name, stackSize, null, true);\n  }\n\n  \/\/ \u6700\u5e95\u5c42\u7ebf\u7a0b\u521b\u5efa\u903b\u8f91\n  private Thread(ThreadGroup g, Runnable target, String name,\n                 long stackSize, AccessControlContext acc,\n                 boolean inheritThreadLocals) {\n    if (name == null) {\n      throw new NullPointerException(\"name cannot be null\");\n    }\n\n    this.name = name;\n\n    Thread parent = currentThread();\n    SecurityManager security = System.getSecurityManager();\n    if (g == null) {\n      \/* Determine if it's an applet or not *\/\n\n            \/* If there is a security manager, ask the security manager\n               what to do. *\/\n      if (security != null) {\n        g = security.getThreadGroup();\n      }\n\n            \/* If the security manager doesn't have a strong opinion\n               on the matter, use the parent thread group.\n              \u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u6ca1\u6709\u8bbe\u7f6e\u7ec4\uff0c\u5219\u83b7\u53d6\u7236\u7ebf\u7a0b\u7684\u7ebf\u7a0b\u7ec4\n               *\/\n      if (g == null) {\n        g = parent.getThreadGroup();\n      }\n      \/* checkAccess regardless of whether or not threadgroup is\n           explicitly passed in. *\/\n      g.checkAccess();\n\n      \/*\n       * Do we have the required permissions?\n       *\/\n      if (security != null) {\n        if (isCCLOverridden(getClass())) {\n          security.checkPermission(\n            SecurityConstants.SUBCLASS_IMPLEMENTATION_PERMISSION);\n        }\n      }\n\n      g.addUnstarted();\n\n      this.group = g;\n      this.daemon = parent.isDaemon();\n      this.priority = parent.getPriority();\n      if (security == null || isCCLOverridden(parent.getClass()))\n        this.contextClassLoader = parent.getContextClassLoader();\n      else\n        this.contextClassLoader = parent.contextClassLoader;\n      this.inheritedAccessControlContext =\n        acc != null ? acc : AccessController.getContext();\n      this.target = target;\n      setPriority(priority);\n      if (inheritThreadLocals &amp;&amp; parent.inheritableThreadLocals != null)\n        this.inheritableThreadLocals =\n          ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);\n      \/* Stash the specified stack size in case the VM cares *\/\n      this.stackSize = stackSize;\n\n      \/* Set thread ID *\/\n      this.tid = nextThreadID();\n    }\n  }\n}<\/pre>\n\n\n\n<p>\u901a\u8fc7Thread\u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u521b\u5efa\u7ebf\u7a0b\u65f6\u672a\u6307\u5b9a\u7ebf\u7a0b\u7ec4\uff0c\u90a3\u4e48\u4f1a\u5c06\u7236\u7c7b\u7684\u7ebf\u5c42\u7ec4\u4fe1\u606f\u8bbe\u7f6e\u4e3a\u81ea\u5df1\u7684\u7ebf\u7a0b\u7ec4\uff0c\u63a5\u7740\u5c31\u6765\u770b\u4e0b ThreadGroup\u6e90\u7801\u7c7b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"809\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111-1024x809.png\" alt=\"\" class=\"wp-image-17639\" style=\"width:492px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111-1024x809.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111-300x237.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111-768x607.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111-830x656.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111-230x182.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111-350x277.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111-480x379.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-111.png 1374w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u901a\u8fc7\u6e90\u7801\u7684\u63cf\u8ff0\u53ef\u4ee5\u5f97\u77e5\uff1aThreadGroup\uff08\u7ebf\u7a0b\u7ec4\uff09\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u7ebf\u7a0b\u7684\u96c6\u5408\uff0c\u5b83\u662f\u4e00\u4e2a\u6811\u5f62\u7ed3\u6784\uff0c\u5e76\u5448\u7236\u5b50\u5173\u7cfb\uff0c\u9664\u4e86 system \u7ebf\u7a0b\u7ec4\uff08\u53ea\u6709\u5b50\u7ebf\u7a0b\u7ec4\uff09\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u7ec4\u65e2\u53ef\u4ee5\u6709\u7236\u7ebf\u7a0b\u7ec4\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u6709\u5b50\u7ebf\u7a0b\u7ec4\u3002\u4f46\u662f\uff0c\u7ebf\u7a0b\u53ea\u80fd\u8bbf\u95ee\u672c\u7ebf\u7a0b\u7ec4\u7684\u4fe1\u606f\uff0c\u4e0d\u80fd\u8bbf\u95ee\u5176\u7236\u7c7b\u6216\u8005\u5176\u4ed6\u7ebf\u7a0b\u7ec4\u7684\u4fe1\u606f\u3002\u5728 Java \u4e2d\u7ebf\u7a0b\u7ec4\u7684\u6811\u72b6\u7ed3\u6784\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"521\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112-1024x521.png\" alt=\"\" class=\"wp-image-17640\" style=\"width:488px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112-1024x521.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112-300x153.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112-768x391.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112-830x422.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112-230x117.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112-350x178.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112-480x244.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/06\/image-112.png 1230w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<ul>\n<li>system \u7ebf\u7a0b\u7ec4\uff0c\u5b83\u662f\u7528\u6765\u5904\u7406 JVM \u7cfb\u7edf\u4efb\u52a1\u7684\u7ebf\u7a0b\u7ec4\uff0c\u6bd4\u5982\u5bf9\u8c61\u9500\u6bc1\u7b49\uff1b<\/li>\n\n\n\n<li>main \u7ebf\u7a0b\u7ec4\uff0c\u5b83\u662f system \u7ebf\u7a0b\u7ec4\u7684\u76f4\u63a5\u5b50\u7ebf\u7a0b\u7ec4\uff0c\u8fd9\u4e2a\u7ebf\u7a0b\u7ec4\u81f3\u5c11\u5305\u542b\u4e00\u4e2a main \u7ebf\u7a0b\uff0c\u7528\u4e8e\u6267\u884c main \u65b9\u6cd5\uff1b<\/li>\n\n\n\n<li>sub \u7ebf\u7a0b\u7ec4\uff0c\u5b83\u662f main \u7ebf\u7a0b\u7ec4\u7684\u5b50\u7ebf\u7a0b\u7ec4\uff0c\u662f\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u7684\u7ebf\u7a0b\u7ec4\uff1b<\/li>\n<\/ul>\n\n\n\n<p>\u6240\u4ee5\uff0cThreadGroup \u7ebf\u7a0b\u7ec4\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u5411\u4e0b\u5f15\u7528\u7684\u6811\u72b6\u7ed3\u6784\uff0c\u8fd9\u6837\u8bbe\u8ba1\u7684\u76ee\u7684\u662f\u9632\u6b62&#8221;\u4e0a\u7ea7&#8221;\u7ebf\u7a0b\u88ab&#8221;\u4e0b\u7ea7&#8221;\u7ebf\u7a0b\u5f15\u7528\u800c\u65e0\u6cd5\u6709\u6548\u5730\u88ab GC \u56de\u6536\u3002<\/p>\n\n\n\n<p>ThreadGroup \u5176\u4ed6\u4e00\u4e9b\u91cd\u8981\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>void checkAccess()<\/td><td>\u5224\u65ad\u7ebf\u7a0b\u80fd\u5426\u8bbf\u95ee\u8be5\u7ebf\u7a0b\u7ec4<\/td><\/tr><tr><td>int activeCount()<\/td><td>\u8fd4\u56de\u7ebf\u7a0b\u7ec4\u53ca\u5176\u5b50\u7ec4\u4e2d\u6d3b\u52a8\u7ebf\u7a0b\u6570\u7684\u4f30\u8ba1\u503c<\/td><\/tr><tr><td>void destroy()<\/td><td>\u9500\u6bc1\u7ebf\u7a0b\u7ec4\u53ca\u5b50\u7ebf\u7a0b\u7ec4<\/td><\/tr><tr><td>void interrupt()<\/td><td>\u4e2d\u65ad\u7ebf\u7a0b\u7ec4\u4e2d\u6240\u6709\u7ebf\u7a0b<\/td><\/tr><tr><td>void stop()<\/td><td>\u505c\u6b62\u7ebf\u7a0b\u7ec4\u4e2d\u6240\u6709\u7ebf\u7a0b<\/td><\/tr><tr><td>void suspend()<\/td><td>\u6302\u8d77\u7ebf\u7a0b\u7ec4\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b<\/td><\/tr><tr><td>ThreadLocal.ThreadLocalMap threadLocals<\/td><td>\u5b58\u653e\u7ebf\u7a0b\u53d8\u91cf\u526f\u672c\uff0c\u548c ThreadLocal \u914d\u5408\u4f7f\u7528<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u516d\u3001Java\u7ebf\u7a0b\u4f18\u5148\u7ea7<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Thread \u7c7b\u4e2d\uff0c\u5173\u4e8e\u7ebf\u7a0b\u4f18\u5148\u7ea7\u7684\u6e90\u7801\u5982\u4e0b\uff0c\u901a\u8fc7\u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u5728 1~10\uff0cJava \u9ed8\u8ba4\u7684\u7ebf\u7a0b\u4f18\u5148\u7ea7\u4e3a 5\uff0c\u53ef\u4ee5\u901a\u8fc7 setPriority()\u8fdb\u884c\u4f18\u5148\u7ea7\u7684\u8bbe\u7f6e\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u9ad8\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u4f1a\u6bd4\u4f4e\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u66f4\u4f18\u5148\u6267\u884c\uff0c\u4f46\u662f\u6700\u7ec8\u6267\u884c\u7684\u987a\u5e8f\u8fd8\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u7684\u8c03\u5ea6\u5668\u6765\u51b3\u5b9a\u3002\u6240\u4ee5\u8bf4\uff0cJava \u4e2d\u7684\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u6765\u662f\u4e00\u4e2a\u53c2\u8003\u503c\uff0c\u5b83\u53ea\u662f\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u4e00\u4e2a\u5efa\u8bae\uff0c\u6700\u7ec8\u7684\u8c03\u7528\u987a\u5e8f\uff0c\u662f\u7531\u64cd\u4f5c\u7cfb\u7edf\u7684\u7ebf\u7a0b\u8c03\u5ea6\u7b97\u6cd5\u51b3\u5b9a\u7684\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class Thread implements Runnable { \n    private int priority;\n    \/**\n     * The minimum priority that a thread can have.\n     *\/\n    public static final int MIN_PRIORITY = 1;\n\n    \/**\n     * The default priority that is assigned to a thread.\n     *\/\n    public static final int NORM_PRIORITY = 5;\n\n    \/**\n     * The maximum priority that a thread can have.\n     *\/\n    public static final int MAX_PRIORITY = 10;\n}\n\n\n<\/pre>\n\n\n\n<p>\u4e0a\u6587\u6211\u4eec\u5bf9 Thread \u7684\u5de5\u4f5c\u539f\u7406\uff0c\u7ebf\u7a0b\u7ec4\u7b49\u76f8\u5173\u77e5\u8bc6\u8fdb\u884c\u4e86\u8bb2\u89e3\uff0c\u90a3\u4e48\uff0c\u7ebf\u7a0b\u4f5c\u4e3a Java19 \u4e4b\u524d\u7684\u6700\u5c0f\u8fd0\u884c\u5355\u5143\uff0c\u4ed6\u4eec\u4e4b\u95f4\u662f\u5982\u4f55\u901a\u4fe1\u7684\u5462\uff1f<\/p>\n\n\n\n<p><strong>\u4e03\u3001Java\u7ebf\u7a0b\u901a\u4fe1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Java \u4e2d\uff0c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u7684\u65b9\u5f0f\u4e3b\u8981\u6709 2 \u79cd\uff1a\u5171\u4eab\u5185\u5b58 \u548c \u6d88\u606f\u4f20\u9012\u3002<\/p>\n\n\n\n<p><strong>7.1\u3001\u5171\u4eab\u5185\u5b58<\/strong><\/p>\n\n\n\n<p>\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u7a0b\u5e8f\u7684\u516c\u5171\u72b6\u6001\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u901a\u8fc7\u5199-\u8bfb\u5185\u5b58\u4e2d\u7684\u516c\u5171\u72b6\u6001\u6765\u9690\u5f0f\u8fdb\u884c\u901a\u4fe1\uff0c\u6bd4\u5982\uff1avolatile \u548c synchronized \u5173\u952e\u5b57\u3002<\/p>\n\n\n\n<p>volatile \u5173\u952e\u5b57\u4fdd\u8bc1\u4e86\u5171\u4eab\u53d8\u91cf\u7684\u53ef\u89c1\u6027\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4efb\u4f55\u7ebf\u7a0b\u66f4\u65b0\u4e86\u4e3b\u5185\u5b58\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c31\u5fc5\u987b\u5931\u6548\u672c\u5730\u5de5\u4f5c\u5185\u5b58\uff0c\u7136\u540e\u4ece\u4e3b\u5185\u5b58\u4e2d\u83b7\u53d6\u6700\u65b0\u503c\u540c\u6b65\u5230\u81ea\u5df1\u7684\u5de5\u4f5c\u5185\u5b58\uff0c\u8fd9\u6837\u7ebf\u7a0b\u4e4b\u95f4\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u5b9e\u73b0\u901a\u4fe1\u3002<\/p>\n\n\n\n<p>synchronized \u5173\u952e\u5b57\u901a\u8fc7\u5411\u5bf9\u8c61\u65bd\u52a0 Monitor \u9501\uff0c\u4ece\u800c\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u62ff\u5230\u9501\uff0c\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u8bbf\u95ee\u7684\u53ef\u89c1\u6027\u548c\u6392\u4ed6\u6027\u3002<\/p>\n\n\n\n<p><strong>7.2\u3001\u6d88\u606f\u4f20\u9012<\/strong><strong><\/strong><\/p>\n\n\n\n<p>\u7ebf\u7a0b\u4e4b\u95f4\u901a\u8fc7\u660e\u786e\u7684\u53d1\u9001\u6d88\u606f\u6765\u663e\u5f0f\u8fdb\u884c\u901a\u4fe1\uff0c\u5728 Java \u4e2d\u5178\u578b\u7684\u6d88\u606f\u4f20\u9012\u65b9\u5f0f\u5c31\u662f wait()\u3001notify()\u3001notifyAll()\u3002<\/p>\n\n\n\n<p>\u5f53\u7ebf\u7a0b A \u8c03\u7528\u4e86\u5bf9\u8c61 Object \u7684 wait()\u65b9\u6cd5\u540e\uff0c\u4f1a\u91ca\u653e Object \u7684 monitor \u6240\u6709\u6743\uff0c\u7136\u540e\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\uff0c\u5f53\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b B \u8c03\u7528\u4e86\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684 notify()\u6216\u8005 notifyAll()\u65b9\u6cd5\uff0c\u7ebf\u7a0b A \u6536\u5230\u901a\u77e5\u540e\u4f1a\u7ee7\u7eed\u62a2\u5360\u8fd0\u884c\u9700\u8981\u7684\u8d44\u6e90\uff0c\u56e0\u6b64\uff0c\u7ebf\u7a0b A\uff0cB \u5c31\u901a\u8fc7\u6d88\u606f\u4f20\u9012\u7684\u65b9\u5f0f\u5b9e\u73b0\u4e86\u901a\u4fe1\u3002<\/p>\n\n\n\n<p><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5230\u6b64\uff0c\u672c\u6587\u7684\u5206\u6790\u5c31\u5168\u90e8\u7ed3\u675f\u4e86\uff0c\u56e0\u4e3a\u6587\u7ae0\u7bc7\u5e45\u6709\u9650\uff0c\u6587\u7ae0\u65e0\u6cd5\u5bf9\u7ebf\u7a0b\u7684\u6bcf\u4e2a\u77e5\u8bc6\u70b9\u5c55\u5f00\u5206\u6790\uff0c\u56e0\u6b64\uff0c\u5efa\u8bae\u5927\u5bb6\u5229\u7528\u7a7a\u4f59\u65f6\u95f4\u53bb\u4e0b\u8f7d JVM \u6e90\u7801\uff08C++\uff09\uff0c\u5bf9\u7167\u672c\u6587\u7ed9\u7684\u6e90\u7801\u67e5\u770b\u7ebf\u7d22\uff0c\u7ed3\u5408 JDK\u6e90\u7801\uff0c\u7136\u540e\u518d\u4ece JVM \u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u89d2\u5ea6\u5168\u9762\u6df1\u5165\u638c\u63e1\u7ebf\u7a0b\u7684\u8fd0\u884c\u673a\u5236\uff0c\u76f8\u4fe1\u4f60\u7684\u6295\u4eba\u4e00\u5b9a\u4f1a\u53d7\u76ca\u532a\u6d45\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u6df1\u5ea6\u5256\u6790\uff1aJava\u7ebf\u7a0b\u8fd0\u884c\u673a\u5236\uff0c\u7a0b\u5e8f\u5458\u5fc5\u770b\u7684\u77e5\u8bc6\u70b9\uff01 \u5728 Java19 \u4e4b\u524d\uff0c\u7ebf\u7a0b\u662f Java \u8fd0\u884c\u7684\u6700 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17598"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17598"}],"version-history":[{"count":6,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17598\/revisions"}],"predecessor-version":[{"id":17641,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17598\/revisions\/17641"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}