{"id":78,"date":"2023-02-13T10:39:21","date_gmt":"2023-02-13T02:39:21","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=78"},"modified":"2023-02-13T10:42:42","modified_gmt":"2023-02-13T02:42:42","slug":"java-multi-threading-programming-basic","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/13\/java-multi-threading-programming-basic\/","title":{"rendered":"Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e00\uff08\u4e00\u4e9b\u7ebf\u7a0b\u7684\u77e5\u8bc6\uff09"},"content":{"rendered":"<h2>\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u7684\u6982\u5ff5\u53ca\u7ebf\u7a0b\u7684\u4f18\u70b9<\/h2>\n<p>\u8fdb\u7a0b\uff1a\u53ef\u7b80\u5355\u7406\u89e3\u4e3aWindow\u4e0a\u7684\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5982\uff1aqq.exe<br \/>\n\u7ebf\u7a0b\uff1a\u53ef\u7b80\u5355\u7406\u89e3\u4e3aqq.exe\u4e2d\u7684\u4e00\u4e2a\u540e\u53f0\u4e0b\u8f7d\u7a0b\u5e8f\u7b49<\/p>\n<p>\u7ebf\u7a0b\u7684\u4f18\u70b9\uff1a\u5728\u540c\u4e00\u65f6\u95f4\u53ef\u4ee5\u540c\u65f6\u8fdb\u884c\u591a\u4e2a\u4efb\u52a1\u7684\u5de5\u4f5c\uff0c\u5f88\u65b9\u4fbf\uff0c\u9ad8\u6548\u7387\u7684\u8282\u7701\u5de5\u4f5c\u65f6\u95f4\u3002\u5982\u4f60\u5728\u804aq\u7684\u540c\u65f6\uff0c\u542c\u6b4c\uff0c\u4e0b\u8f7d\u6587\u4ef6\uff0c\u804a\u5929\u3002\u3002\u3002<\/p>\n<h2>\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h2>\n<p>\uff081\uff09\u7ee7\u627fThread\u7c7b\u91cd\u5199run()\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">public class MyThread extends Thread {\n    @Override\n    public void run() {\n        super.run();\n        System.out.println(&quot;\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1&quot;);\n    }\n    public static void main(String[] args) {\n        Thread thread = new MyThread();\n        thread.start();\n        System.out.println(&quot;\u8fd0\u884c\u7ed3\u675f\uff01&quot;);\n   }\n}<\/code><\/pre>\n<p>\uff082\uff09\u5b9e\u73b0Runnable\u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"language-java\">public class MyThread implements Runnable {\n    public void run() {\n        System.out.println(&quot;\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1&quot;);\n    }\n\n    public static void main(String[] args) {\n        Thread thread = new Thread(new MyThread());\n        thread.start();\n        System.out.println(&quot;\u8fd0\u884c\u7ed3\u675f\uff01&quot;)\uff1b\n    }\n}<\/code><\/pre>\n<h2>\u5b9e\u4f8b\u53d8\u91cf\u548c\u7ebf\u7a0b\u5b89\u5168<\/h2>\n<p>\u81ea\u5b9a\u4e49\u7684\u7ebf\u7a0b\u7c7b\u4e2d\u5b9e\u4f8b\u53d8\u91cf\u9488\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u6709\u5171\u4eab\u548c\u4e0d\u5171\u4eab\u4e4b\u5206\uff0c\u8fd9\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u8fdb\u884c\u4ea4\u4e92\u65f6\u662f\u4e2a\u91cd\u8981\u7684\u6280\u672f\u70b9\u3002<\/p>\n<p>\u4e0d\u5171\u4eab\u6570\u636e\u7684\u60c5\u51b5\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public class MyThread extends Thread {\n\n    private int count = 5;\n\n    public MyThread(String name) {\n        this.setName(name);\n    }\n\n    @Override\n    public void run() {\n        System.out.println(&quot;\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1&quot;);\n        while (count &gt; 0) {\n            count--;\n            System.out.println(&quot;\u7531\uff1a&quot; + Thread.currentThread().getName() + &quot;\u8ba1\u7b97\uff0ccount=&quot; + count);\n        }\n    }\n\n    public static void main(String[] args)  {\n        MyThread a = new MyThread(&quot;A&quot;);\n        MyThread b = new MyThread(&quot;B&quot;);\n        MyThread c = new MyThread(&quot;C&quot;);\n        a.start();\n        b.start();\n        c.start();\n        System.out.println(&quot;\u8fd0\u884c\u7ed3\u675f\uff01&quot;);\n    }\n}<\/code><\/pre>\n<p>\u7531\u4e8e\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u6709\u5c5e\u4e8e\u81ea\u5df1\u7684\u53d8\u91cfcount\uff0c\u8ba1\u7b97\u4e92\u4e0d\u5e72\u6270\uff0c\u65e2\u4e0d\u5171\u4eab\u6570\u636e\uff0c\u6240\u6709\u4e5f\u5c31\u4e0d\u4f1a\u51fa\u73b0\u9519\u8bef\u6570\u636e\u7684\u60c5\u51b5\uff0c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<p>\u5171\u4eab\u6570\u636e\u7684\u60c5\u51b5\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public class MyThread extends Thread {\n    private static int count = 5;\n\n    @Override\n    public void run() {\n        System.out.println(&quot;\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1&quot;);\n        while (count &gt; 0) {\n            count--;\n            System.out.println(&quot;\u7531\uff1a&quot; + Thread.currentThread().getName() + &quot;\u8ba1\u7b97\uff0ccount=&quot; + count);\n        }\n    }\n\n    public static void main(String[] args) {\n        MyThread myThread = new MyThread();\n        Thread a = new Thread(myThread, &quot;A&quot;);\n        Thread b = new Thread(myThread, &quot;B&quot;);\n        Thread c = new Thread(myThread, &quot;C&quot;);\n        a.start();\n        b.start();\n        c.start();\n        System.out.println(&quot;\u8fd0\u884c\u7ed3\u675f\uff01&quot;);\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=3\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=3\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=1\n\u7531\uff1aB\u8ba1\u7b97\uff0ccount=1\n\u7531\uff1aA\u8ba1\u7b97\uff0ccount=0\n\u8fd0\u884c\u7ed3\u675f\uff01\n\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1<\/code><\/pre>\n<p>\u7531\u7ed3\u679c\u6211\u4eec\u53d1\u73b0\uff0c\u51fa\u73b0\u4e86\u7ebf\u7a0bA\u548c\u7ebf\u7a0bB\u53d6\u51fa\u7684\u503c\u4e3a\u540c\u4e00\u4e2a\u503c\uff0c\u4ea7\u751f\u4e86\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002\u5728JVM\u4e2d\uff0c<code>i--<\/code>\u5206\u4e3a\u5982\u4e0b\u4e09\u6b65\uff1a<\/p>\n<ul>\n<li>1\u3001\u53d6\u5f97\u539f\u6709\u503ci<\/li>\n<li>2\u3001\u8ba1\u7b97i-1<\/li>\n<li>3\u3001\u5bf9i\u8fdb\u884c\u8d4b\u503c<\/li>\n<\/ul>\n<p>\u5728\u8fd9\u4e09\u4e2a\u6b65\u9aa4\u4e2d\uff0c\u5982\u679c\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\uff0c\u90a3\u4e48\u4e00\u5b9a\u4f1a\u51fa\u73b0\u975e\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/p>\n<h2>\u90e8\u5206\u5e38\u7528\u65b9\u6cd5<\/h2>\n<ul>\n<li><code>currentThread()<\/code>: \u8fd4\u56de\u4ee3\u7801\u6b63\u5728\u88ab\u54ea\u4e2a\u7ebf\u7a0b\u8c03\u7528\u7684\u4fe1\u606f<\/li>\n<li><code>isAlive()<\/code>: \u5224\u65ad\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\uff0c\u6d3b\u52a8\u72b6\u6001\u5c31\u662f\u7ebf\u7a0b\u5df2\u7ecf\u542f\u52a8\u4e14\u5c1a\u672a\u7ec8\u6b62\uff0c\u7ebf\u7a0b\u5904\u4e8e\u6b63\u5728\u8fd0\u884c\u6216\u51c6\u5907\u5f00\u59cb\u8fd0\u884c\u7684\u72b6\u6001\u5c31\u8ba4\u4e3a\u662f&quot;\u5b58\u6d3b&quot;\u7684<\/li>\n<li><code>sleep()<\/code>: \u6307\u5b9a\u6beb\u79d2\u6570\u5185\uff0c\u8ba9\u2018\u5f53\u524d\u2019\u6b63\u5728\u6267\u884c\u7684\u7ebf\u7a0b\u4f11\u7720\uff08\u6682\u505c\u6267\u884c\uff09<\/li>\n<li><code>getId()<\/code>: \u83b7\u5f97\u7ebf\u7a0b\u7684\u552f\u4e00\u6807\u8bc6\u3002<\/li>\n<\/ul>\n<h2>\u505c\u6b62\u7ebf\u7a0b<\/h2>\n<ul>\n<li>interrupt(): \u4ec5\u4ec5\u662f\u5728\u5f53\u524d\u7ebf\u7a0b\u6253\u4e86\u4e00\u4e2a\u4e2d\u65ad\u7684\u6807\u8bb0\uff0c\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u505c\u6b62\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u505c\u6b62\u72b6\u6001\uff1a<\/p>\n<pre><code class=\"language-java\">this.interrupt(); \/\/\u6d4b\u8bd5\u5f53\u524d\u7ebf\u7a0b\u662f\u5426\u5df2\u7ecf\u4e2d\u65ad\uff0c\u7ebf\u7a0b\u7684\u4e2d\u65ad\u72b6\u6001\u7531\u8be5\u65b9\u6cd5\u6e05\u695a\uff0c\u5f53\u524d\u7ebf\u7a0b\u662f\u6307\u8fd0\u884cthis.interrupt()\u65b9\u6cd5\u7684\u7ebf\u7a0b\nthis.isInterrupted(); \/\/\u6d4b\u8bd5\u7ebf\u7a0b\u662f\u5426\u5df2\u7ecf\u4e2d\u65ad<\/code><\/pre>\n<ul>\n<li>\u5f02\u5e38\u6cd5\uff08\u629b\u51fa\u5f02\u5e38\u6765\u7ec8\u6b62\u7ebf\u7a0b\u7684\u8fd0\u884c\uff09<\/li>\n<li>\u5982\u679c\u6c89\u7761\u4e2d\u505c\u6b62\u7ebf\u7a0b\uff0c\u4f1a\u53d1\u751f\u5f02\u5e38<\/li>\n<li>\u66b4\u529b\u505c\u6b62<code>stop()<\/code>\u5e9f\u5f03<\/li>\n<li>return\u505c\u6b62\u7ebf\u7a0b<\/li>\n<li>\u53d8\u91cf\u63a7\u5236\uff0c\u8df3\u51fa\u5faa\u73af<\/li>\n<\/ul>\n<h2>\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7<\/h2>\n<p>\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u5212\u5206\u4f18\u5148\u7ea7\uff0c\u4f18\u5148\u7ea7\u8f83\u9ad8\u7684\u7ebf\u7a0b\u5f97\u5230cpu\u7684\u8d44\u6e90\u8f83\u591a\uff0c\u4e5f\u5c31\u662fcpu\u4f18\u5148\u6267\u884c\u4f18\u5148\u7ea7\u8f83\u9ad8\u7684\u7ebf\u7a0b\u5bf9\u8c61\u4e2d\u7684\u4efb\u52a1\u3002<br \/>\n\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u6709\u52a9\u4e8e\u5e2e\u201c\u7ebf\u7a0b\u89c4\u5212\u5668\u201d\u786e\u5b9a\u5728\u4e0b\u4e00\u6b21\u9009\u62e9\u54ea\u4e00\u4e2a\u7ebf\u7a0b\u6765\u4f18\u5148\u6267\u884c\u3002<br \/>\n\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u4f7f\u7528<code>setPriority()<\/code>\u65b9\u6cd5\u3002<br \/>\nJDK\u4f7f\u75283\u4e2a\u5e38\u91cf\u6765\u9884\u7f6e\u5b9a\u4e49\u4f18\u5148\u7ea7\u7684\u503c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">\/**\n * The minimum priority that a thread can have.\n *\/\npublic final static int MIN_PRIORITY = 1;\n\n\/**\n * The default priority that is assigned to a thread.\n *\/\npublic final static int NORM_PRIORITY = 5;\n\n\/**\n * The maximum priority that a thread can have.\n *\/\npublic final static int MAX_PRIORITY = 10;<\/code><\/pre>\n<ul>\n<li>\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u5177\u6709\u7ee7\u627f\u6027\uff1a\u7ebf\u7a0bA\u542f\u52a8\u7ebf\u7a0bB\uff0cA\u4e0eB\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7\u3002<\/li>\n<li>\u4f18\u5148\u7ea7\u5177\u6709\u89c4\u5219\u6027\uff1a\u5927\u90e8\u5206\u9ad8\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\u6bd4\u4f4e\u4f18\u5148\u7ea7\u7ebf\u7a0b\u5148\u6267\u884c\uff0c\u5e76\u4e0d\u662f\u4e00\u5b9a\u5c31\u4f1a\u6bd4\u4f4e\u4f18\u5148\u7ea7\u7684\u5148\u6267\u884c\u5b8c\u3002<\/li>\n<\/ul>\n<h2>\u5b88\u62a4\u7ebf\u7a0b<\/h2>\n<p>\u5b88\u62a4\u7ebf\u7a0b\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7ebf\u7a0b\uff0c\u4ed6\u7684\u7279\u6027\u6709\u201c\u966a\u4f34\u201d\u7684\u542b\u4e49\uff0c\u5f53\u8fdb\u7a0b\u4e0d\u5b58\u5728\u975e\u5b88\u62a4\u7ebf\u7a0b\u4e86\uff0c\u5219\u5b88\u62a4\u7ebf\u7a0b\u81ea\u52a8\u9500\u6bc1\u3002\u5178\u578b\u7684\u5b88\u62a4\u7ebf\u7a0b\u5c31\u662f\u5783\u573e\u56de\u6536\u7ebf\u7a0b\uff0c\u5f53\u8fdb\u7a0b\u4e2d\u6ca1\u6709\u975e\u5b88\u62a4\u7ebf\u7a0b\u4e86\uff0c\u90a3\u4e48\u5783\u573e\u56de\u6536\u7ebf\u7a0b\u4e5f\u5c31\u6ca1\u6709\u5b58\u5728\u7684\u5fc5\u8981\u4e86\uff0c\u81ea\u52a8\u9500\u6bc1\u3002<\/p>\n<p>\u201c\u5b88\u62a4\u7ebf\u7a0b\u201d\uff1a\u4efb\u4f55\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b\u90fd\u662f\u6574\u4e2aJVM\u4e2d\u6240\u6709\u975e\u5b88\u62a4\u7ebf\u7a0b\u7684\u201c\u4fdd\u59c6\u201d\uff0c\u53ea\u8981\u5f53\u524dJVM\u5b9e\u4f8b\u4e2d\u5b58\u5728\u4efb\u4f55\u4e00\u4e2a\u975e\u5b88\u62a4\u7ebf\u7a0b\u6ca1\u6709\u7ed3\u675f\uff0c\u5b88\u62a4\u7ebf\u7a0b\u5c31\u5728\u5de5\u4f5c\uff0c\u53ea\u6709\u5f53\u6700\u540e\u4e00\u4e2a\u975e\u5b88\u62a4\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u5b88\u62a4\u7ebf\u7a0b\u624d\u4f1a\u968f\u7740JVM\u4e00\u540c\u7ed3\u675f\u5de5\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u7684\u6982\u5ff5\u53ca\u7ebf\u7a0b\u7684\u4f18\u70b9 \u8fdb\u7a0b\uff1a\u53ef\u7b80\u5355\u7406\u89e3\u4e3aWindow\u4e0a\u7684\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5982\uff1aqq.exe \u7ebf\u7a0b\uff1a\u53ef\u7b80\u5355\u7406\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[51],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-multi-threads"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}