{"id":462,"date":"2019-08-06T17:02:03","date_gmt":"2019-08-06T09:02:03","guid":{"rendered":"http:\/\/47.101.202.111\/?p=462"},"modified":"2019-12-16T18:26:28","modified_gmt":"2019-12-16T10:26:28","slug":"java%e7%89%b9%e6%80%a7%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=462","title":{"rendered":"[Java\u7279\u6027]\u591a\u7ebf\u7a0b\u64cd\u4f5c"},"content":{"rendered":"<h2>\u591a\u7ebf\u7a0b\u64cd\u4f5c<\/h2>\n<p>\u591a\u7ebf\u7a0b\u8fd0\u884c\u72b6\u6001\u4e0d\u786e\u5b9a\uff0c\u901a\u8fc7\u540d\u79f0\u64cd\u4f5c\u8be5\u7ebf\u7a0b<br \/>\n\u64cd\u4f5c\u65b9\u6cd5\u5728Thread\u7c7b\u4e2d<\/p>\n<pre lang=\"java\">\npublic Thread (Runnable target, String name)                            \u6784\u9020\u65b9\u6cd5\u8bbe\u7f6e\u7ebf\u7a0b\u540d\npublic final void setName (String name)                                 \u8bbe\u7f6e\u540d\u5b57\uff0c\u4e0d\u53ef\u4fee\u6539\npublic final String getName ()                                                  \u53d6\u5f97\u540d\u5b57\npublic static Thread currentThread ()                                           \u83b7\u53d6\u7ebf\u7a0b\npublic static void sleep (long millis) throws InterruptedException              \u7ebf\u7a0b\u4f11\u7720\npublic static void sleep (long millis, int nanos) throws InterruptedException   \u7ebf\u7a0b\u4f11\u7720\npublic boolean isInterrupted ()                                         \u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u88ab\u4e2d\u65ad\npublic void interrupt ()                                                    \u4e2d\u65ad\u64cd\u4f5c\u6267\u884c\npublic final void join() throws InterruptedException                            \u5f3a\u5236\u6267\u884c\npublic static void yield ()                                                     \u7ebf\u7a0b\u793c\u8ba9\npublic final void setPriority (int newPriority)                             \u8bbe\u7f6e\u7ebf\u7a0b\u4f18\u5148\u7ea7\npublic final int getPriority ()                                             \u83b7\u53d6\u7ebf\u7a0b\u4f18\u5148\u7ea7\n<\/pre>\n<p>[toc]<\/p>\n<h4>\u8bbe\u7f6e\u7ebf\u7a0b\u540d<\/h4>\n<pre><code class=\"language-java \">    class CustomThread implements Runnable{\n        @Override\n        public void run(){ }\n    }\n    public static void main(String[] args){\n        CustomThread thread = new CustomThread();\n        new Thread(thread, \"custom-thread\"); \/\/ \u6784\u9020\u65b9\u6cd5\u8bbe\u7f6e\u7ebf\u7a0b\u540d\n        new Thread(thread); \/\/ \u672a\u8bbe\u7f6e\u7ebf\u7a0b\u540d\u81ea\u52a8\u8bbe\u7f6e\u540d\u5b57\n    }\n<\/code><\/pre>\n<h4>\u83b7\u53d6\u7ebf\u7a0b\u540d<\/h4>\n<p>\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u4fe1\u606f\uff0c\u5199\u5728run()\u65b9\u6cd5\u4e2d\uff0c\u6bcf\u6b21\u7ebf\u7a0b\u542f\u52a8\u65f6\u7531start()\u8c03\u7528<\/p>\n<pre><code class=\"language-java \">    class CustomThread implements Runnable{\n        @Override\n        public void run(){\n            Thread.currentThread().getName();\n        }\n    }\n<\/code><\/pre>\n<p>\u672a\u8d77\u540d\u4f1a\u81ea\u52a8\u975e\u91cd\u547d\u540d\uff0c\u4f7f\u7528static\u5c5e\u6027\u548c\u65b9\u6cd5\u81ea\u52a8\u7f16\u53f7<\/p>\n<pre lang=\"java\">   \n    private static int threadInitNumber;\n    private static synchronized int nextThreadNum() {\n        return threadInitNumber++;\n    }\n<\/pre>\n<pre><code class=\"language-java \">    public static void main(String[] args){\n        CustomThread thread = new CustomThread();\n        new Thread(thread, \"custom-thread\"); \/\/ \u8bbe\u7f6e\u7ebf\u7a0b\u540d\n        new Thread(thread); \/\/ \u672a\u8bbe\u7f6e\u7ebf\u7a0b\u540d\u81ea\u52a8\u8bbe\u7f6e\u540d\u5b57\n}\n<\/code><\/pre>\n<h4>\u4e3b\u65b9\u6cd5\u4e5f\u662f\u7ebf\u7a0b<\/h4>\n<p>\u5728new\u5b50\u7ebf\u7a0b\u4e4b\u540e\u5199main\u65b9\u6cd5\u903b\u8f91\uff0c\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u62a2\u5360\u8d44\u6e90\u5e76\u53d1\u6267\u884c<\/p>\n<pre><code class=\"language-java \">    public static void main(String[] args){\n        CustomThread thread = new CustomThread();\n        new Thread(thread, \"custom-thread\").start();\n        thread.run(); \/\/ \u6267\u884cCustomThread\u4e2d\u8986\u5199\u7684run()\u65b9\u6cd5\n}\n<\/code><\/pre>\n<pre><code>custom-thread\nmain \/\/main()\u65b9\u6cd5\u4e2d\u76f4\u63a5\u8c03\u7528\u7ebf\u7a0b\u7c7b\u5bf9\u8c61\u4e2d\u7684run()\u65b9\u6cd5\uff0c\u7ebf\u7a0b\u540d\u4e3amain\n<\/code><\/pre>\n<p>\u6267\u884cjava\u7a0b\u5e8f\u65f6\u542f\u52a8\u4e00\u4e2aJVM\u8fdb\u7a0b-java.exe\u3002\u540c\u65f6\u542f\u52a8\u591a\u4e2a\u8fdb\u7a0b\uff0c\u6bcf\u4e2aJVM\u8fdb\u7a0b\u4e2d\u4f1a\u6709\u5404\u81ea\u7684\u7ebf\u7a0b\uff0c\u8fdb\u7a0b\u4e2d\u4e3b\u65b9\u6cd5\u662f\u4e3b\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u521b\u5efa\u82e5\u5e72\u5b50\u7ebf\u7a0b<br \/>\n<strong>\u4e3b\u7ebf\u7a0b\u8d1f\u8d23\u6574\u4f53\u6d41\u7a0b\uff0c\u5b50\u7ebf\u7a0b\u8d1f\u8d23\u8017\u65f6\u903b\u8f91<\/strong><\/p>\n<h4>\u7ebf\u7a0b\u4f11\u7720<\/h4>\n<p>\u7ebf\u7a0b\u6682\u7f13\u6267\u884c\uff0c\u4f11\u7720\u65f6\u957f\u5230\u540e\u7ebf\u7a0b\u5524\u9192\uff0c\u7ee7\u7eed\u8fdb\u884c\u540e\u7eed\u5904\u7406<br \/>\n\u7ebf\u7a0b\u4f11\u7720\u53ef\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u6253\u65ad\uff0c\u629b\u51fa\u4e2d\u65ad\u5f02\u5e38InterruptedException\uff0c\u4e3aException\u5b50\u7c7b\uff0c\u5fc5\u987b\u8fdb\u884c\u5f02\u5e38\u5904\u7406<\/p>\n<pre><code class=\"language-java \">    new Thread(() -&gt; {\n        try{\n            Thread.sleep(1000);\n        }catch(InterruptedException e){\n            \/\/ \u4e2d\u65ad\u5904\u7406\n        }\n    }), \"\u7ebf\u7a0b\u540d\").start();\n<\/code><\/pre>\n<p>\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u4e4b\u95f4\u6267\u884c\u64cd\u4f5c\u6709\u5148\u540e\u987a\u5e8f\uff0c\u591a\u4e2a\u7ebf\u7a0b\u4f11\u7720\u5f7c\u6b64\u4e4b\u95f4\u4ecd\u6709\u5148\u540e\u987a\u5e8f<\/p>\n<h4>\u7ebf\u7a0b\u4e2d\u65ad<\/h4>\n<p>\u6240\u6709\u6b63\u5728\u6267\u884c\u7684\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u88ab\u4e2d\u65ad\uff0c\u4e2d\u65ad\u7ebf\u7a0b\u5f3a\u5236\u8fdb\u884c\u5f02\u5e38\u5904\u7406<\/p>\n<pre><code class=\"language-java \">    Thread thread = new Thread(() -&gt; {\n        try {\n            System.out.println(Thread.currentThread().getName() + \"\u7ebf\u7a0b\u5f00\u59cb\u4f11\u7720\");\n            Thread.sleep(5000);\n        } catch (InterruptedException e) {\n            System.out.println(Thread.currentThread().getName() + \"\u7ebf\u7a0b\u88ab\u4e2d\u65ad\");\n        }\n    }, \"\u88ab\u52a8\u4e2d\u65ad\u7ebf\u7a0b\");\n    thread.start();\n    try {\n        thread.sleep(2000);\n        if (!thread.isInterrupted()) {\n            thread.interrupt();\n            System.out.println(\"\u4e2d\u65ad\u6210\u529f\");\n        }\n    } catch (InterruptedException e) {\n        e.printStackTrace();\n    }\n<\/code><\/pre>\n<pre><code>\u88ab\u52a8\u4e2d\u65ad\u7ebf\u7a0b\u7ebf\u7a0b\u5f00\u59cb\u4f11\u7720\n\u4e2d\u65ad\u6210\u529f\n\u88ab\u52a8\u4e2d\u65ad\u7ebf\u7a0b\u7ebf\u7a0b\u88ab\u4e2d\u65ad\n<\/code><\/pre>\n<h4>\u7ebf\u7a0b\u5f3a\u5236\u6267\u884c<\/h4>\n<p>\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u4e00\u76f4\u72ec\u5360\u8d44\u6e90\u76f4\u5230\u8be5\u7ebf\u7a0b\u7a0b\u5e8f\u6267\u884c\u7ed3\u675f<br \/>\n\u83b7\u53d6\u5f3a\u5236\u6267\u884c\u7ebf\u7a0b\u5bf9\u8c61\u540e\uff0c\u6267\u884cjoin\u65b9\u6cd5\u7684\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5176\u4ed6\u7ebf\u7a0b\u624d\u53ef\u4ee5\u83b7\u5f97\u8ba1\u7b97\u8d44\u6e90\u6267\u884c<\/p>\n<pre><code class=\"language-java \">    Thread mainThread = Thread.currentThread();\n    new Thread(() -&gt; {\n        for (int i = 0; i &lt; 10; i++) {\n            if (i == 3) {\n                try {\n                    mainThread.join();\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n            try {\n                Thread.sleep(100);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(Thread.currentThread().getName() + i);\n        }\n    }, \"\u88ab\u62a2\u5360\u8d44\u6e90\u7ebf\u7a0b\").start();\n\n    for (int j = 0; j &lt; 5; j++) {\n        try {\n            Thread.sleep(1000);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n        System.out.println(mainThread.getName() + j);\n    }\n\n<\/code><\/pre>\n<pre><code>\u88ab\u62a2\u5360\u8d44\u6e90\u7ebf\u7a0b0\n\u88ab\u62a2\u5360\u8d44\u6e90\u7ebf\u7a0b1\n\u88ab\u62a2\u5360\u8d44\u6e90\u7ebf\u7a0b2\nmain0\nmain1\nmain2\nmain3\nmain4\n\u88ab\u62a2\u5360\u8d44\u6e90\u7ebf\u7a0b3\n\u88ab\u62a2\u5360\u8d44\u6e90\u7ebf\u7a0b4\n<\/code><\/pre>\n<h4>\u7ebf\u7a0b\u793c\u8ba9<\/h4>\n<p>\u6682\u65f6\u8ba9\u51fa\u8ba1\u7b97\u8d44\u6e90\u7ed9\u76f8\u540c\u6216\u66f4\u9ad8\u4f18\u5148\u7ea7\u7ebf\u7a0b\uff0c\u6bcf\u6b21\u8c03\u7528yeild()\u53ea\u793c\u8ba9\u4e00\u6b21<\/p>\n<pre><code class=\"language-java \">    Thread mainThread = Thread.currentThread();\n    new Thread(() -&gt; {\n        for (int i = 0; i &lt; 5; i++) {\n            if (i == 1) {\n                Thread.currentThread().yield();\n                System.out.println(\"i\u5230\" + i + \"\u7ebf\u7a0b\u793c\u8ba9\u4e00\u6b21\");\n            }\n            try {\n                Thread.sleep(100);\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n            System.out.println(Thread.currentThread().getName() + i);\n        }\n    }, \"\u793c\u8ba9\u7ebf\u7a0b\").start();\n\n    for (int j = 0; j &lt; 5; j++) {\n        try {\n            Thread.sleep(100);\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n        System.out.println(mainThread.getName() + j);\n    }\n<\/code><\/pre>\n<pre><code>main0\n\u793c\u8ba9\u7ebf\u7a0b0\ni\u52301\u7ebf\u7a0b\u793c\u8ba9\u4e00\u6b21\nmain1\n\u793c\u8ba9\u7ebf\u7a0b1\nmain2\n\u793c\u8ba9\u7ebf\u7a0b2\nmain3\n\u793c\u8ba9\u7ebf\u7a0b3\nmain4\n\u793c\u8ba9\u7ebf\u7a0b4\n<\/code><\/pre>\n<h4>\u7ebf\u7a0b\u4f18\u5148\u7ea7<\/h4>\n<p>\u4f18\u5148\u7ea7\u9ad8\u5148\u6267\u884c\u6982\u7387\u66f4\u5927<\/p>\n<pre lang=\"java\">\npublic static final int MAX_PRIORITY    \u6700\u9ad8\u4f18\u5148\u7ea7 10\npublic static final int NORM_PRIORITY   \u4e2d\u7b49\u4f18\u5148\u7ea7  5\npublic static final int MIN_PRIORITY    \u6700\u4f4e\u4f18\u5148\u7ea7  1\n<\/pre>\n<p>\u4e3b\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u9ed8\u8ba4\u662f<strong>\u4e2d\u7b49\u4f18\u5148\u7ea7<\/strong>\uff0c\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u5e76\u53d1\u6267\u884c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ebf\u7a0b\u64cd\u4f5c \u591a\u7ebf\u7a0b\u8fd0\u884c\u72b6\u6001\u4e0d\u786e\u5b9a\uff0c\u901a\u8fc7\u540d\u79f0\u64cd\u4f5c\u8be5\u7ebf\u7a0b \u64cd\u4f5c\u65b9\u6cd5\u5728Thread\u7c7b\u4e2d public Thread  &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=462\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c[Java\u7279\u6027]\u591a\u7ebf\u7a0b\u64cd\u4f5c\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/462"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=462"}],"version-history":[{"count":12,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/462\/revisions"}],"predecessor-version":[{"id":612,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/462\/revisions\/612"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}