{"id":3968,"date":"2018-06-09T08:59:08","date_gmt":"2018-06-09T00:59:08","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3968"},"modified":"2018-06-09T08:59:08","modified_gmt":"2018-06-09T00:59:08","slug":"java%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%bc%96%e7%a8%8b%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3968.html","title":{"rendered":"Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u5b66\u4e60"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5b66\u4e60\u6574\u7406\u4e00\u4e0bJava\u4e2d\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u6837\u4f8b\u4ee3\u7801\uff0c\u65b9\u4fbf\u4ee5\u540e\u4f7f\u7528\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6><strong>\u4e00\u4e2a\u7ebf\u7a0b\u5b8c\u6574\u7684\u751f\u547d\u5468\u671f\uff1a<\/strong><\/h6>\n<figure style=\"width: 1232px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full\" src=\"https:\/\/www.runoob.com\/wp-content\/uploads\/2014\/01\/java-thread.jpg\" width=\"1232\" height=\"862\" \/><figcaption class=\"wp-caption-text\">\u4e00\u4e2aJava\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f<\/figcaption><\/figure>\n<h6><strong>Java \u63d0\u4f9b\u4e86\u4e09\u79cd\u521b\u5efa\u7ebf\u7a0b\u7684\u65b9\u6cd5\uff1a<\/strong><\/h6>\n<ul>\n<li>\u901a\u8fc7\u5b9e\u73b0 Runnable \u63a5\u53e3\uff1b<\/li>\n<li>\u901a\u8fc7\u7ee7\u627f Thread \u7c7b\u672c\u8eab\uff1b<\/li>\n<li>\u901a\u8fc7 Callable \u548c Future \u521b\u5efa\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<h6><strong>\u4ee3\u7801\u6837\u4f8b\uff1a<\/strong><\/h6>\n<pre class=\"lang:default decode:true\" title=\"\u901a\u8fc7\u5b9e\u73b0 Runnable \u63a5\u53e3\u6765\u521b\u5efa\u7ebf\u7a0b\">package com.ixyzero.learn.misc;\r\n\r\n\/**\r\n * Created by ixyzero.com on 2018\/6\/6.\r\n *\/\r\npublic class TestThread {\r\n    public static void main(String args[]) {\r\n        long t1 = System.currentTimeMillis();\r\n        System.out.println(\"Start @ \" + t1);\r\n        RunnableDemo R1 = new RunnableDemo(\"Thread-1\");\r\n        R1.start();\r\n\r\n        RunnableDemo R2 = new RunnableDemo(\"Thread-2\");\r\n        R2.start();\r\n        System.out.println(\"spend \" + (System.currentTimeMillis() - t1) + \" ms\");\r\n        System.out.println(\"END @ \" + System.currentTimeMillis());\r\n    }\r\n}\r\n\r\n\/**\r\n * Java\u4e2d\u521b\u5efa\u7ebf\u7a0b\u7684\u65b9\u6cd5\u4e00:\r\n * \u901a\u8fc7\u5b9e\u73b0 Runnable \u63a5\u53e3\u6765\u521b\u5efa\u7ebf\u7a0b\r\n * \u5b9e\u73b0 run()\/start() \u65b9\u6cd5\r\n * \u7136\u540e\u7528 new Thread() \u521b\u5efa\u7ebf\u7a0b\uff0c\u518d\u8c03\u7528\u521a\u624d\u7684 start() \u65b9\u6cd5\u8ba9\u5b83\u8fd0\u884c\r\n *\/\r\nclass RunnableDemo implements Runnable {\r\n    private Thread t;\r\n    private String threadName;\r\n\r\n    RunnableDemo( String name) {\r\n        threadName = name;\r\n        System.out.println(\"Creating \" +  threadName );\r\n    }\r\n\r\n    public void run() {\r\n        System.out.println(\"Running \" +  threadName );\r\n        try {\r\n            for(int i = 4; i &gt; 0; i--) {\r\n                System.out.println(\"Thread: \" + threadName + \", \" + i + \", \" + System.currentTimeMillis());\r\n                \/\/ \u8ba9\u7ebf\u7a0b\u7761\u7720\u4e00\u4f1a\r\n                Thread.sleep(50);\r\n            }\r\n        }catch (InterruptedException e) {\r\n            System.out.println(\"Thread \" +  threadName + \" interrupted.\");\r\n        }\r\n        System.out.println(\"Thread \" +  threadName + \" exiting.\");\r\n    }\r\n\r\n    public void start () {\r\n        System.out.println(\"Starting \" +  threadName );\r\n        if (t == null) {\r\n            t = new Thread (this, threadName);\r\n            t.start ();\r\n        }\r\n    }\r\n}<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \" title=\"\u901a\u8fc7\u7ee7\u627f Thread \u6765\u521b\u5efa\u7ebf\u7a0b\">package com.ixyzero.learn.misc;\r\n\r\n\/**\r\n * Created by ixyzero.com on 2018\/6\/6.\r\n *\/\r\npublic class TestThread2 {\r\n    public static void main(String args[]) {\r\n        long t1 = System.currentTimeMillis();\r\n        System.out.println(\"Start @ \" + t1);\r\n        ThreadDemo T1 = new ThreadDemo( \"Thread-1\");\r\n        T1.start();\r\n\r\n        ThreadDemo T2 = new ThreadDemo( \"Thread-2\");\r\n        T2.start();\r\n        System.out.println(\"spend \" + (System.currentTimeMillis() - t1) + \" ms\");\r\n        System.out.println(\"END @ \" + System.currentTimeMillis());\r\n    }\r\n}\r\n\r\n\/**\r\n * \u901a\u8fc7\u7ee7\u627f Thread \u6765\u521b\u5efa\u7ebf\u7a0b\r\n *\/\r\nclass ThreadDemo extends Thread {\r\n    private Thread t;\r\n    private String threadName;\r\n\r\n    ThreadDemo( String name) {\r\n        threadName = name;\r\n        System.out.println(\"Creating \" +  threadName );\r\n    }\r\n\r\n    public void run() {\r\n        System.out.println(\"Running \" +  threadName );\r\n        try {\r\n            for(int i = 4; i &gt; 0; i--) {\r\n                System.out.println(\"Thread: \" + threadName + \", \" + i + \", \" + System.currentTimeMillis());\r\n                \/\/ \u8ba9\u7ebf\u7a0b\u7761\u7720\u4e00\u4f1a\r\n                Thread.sleep(50);\r\n            }\r\n        }catch (InterruptedException e) {\r\n            System.out.println(\"Thread \" +  threadName + \" interrupted.\");\r\n        }\r\n        System.out.println(\"Thread \" +  threadName + \" exiting.\");\r\n    }\r\n\r\n    public void start () {\r\n        System.out.println(\"Starting \" +  threadName );\r\n        if (t == null) {\r\n            t = new Thread (this, threadName);\r\n            t.start ();\r\n        }\r\n    }\r\n}<\/pre>\n<p>&amp;<\/p>\n<p>\u5355\u7ebf\u7a0b\u5bf9\u6bd4\u6d4b\u8bd5\u2014\u2014new\u4e00\u4e2a\u5e38\u89c4\u7684class<\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.misc;\r\n\r\n\/**\r\n * Created by ixyzero.com on 2018\/6\/6.\r\n *\/\r\npublic class TestNewMethod {\r\n    public static void main(String[] args){\r\n        MyThread myThread = new MyThread();\r\n        myThread.run();\r\n        while(true) {\r\n            System.out.println(\"Main\u65b9\u6cd5\u5728\u8fd0\u884c\");\r\n        }\r\n    }\r\n}\r\n\r\nclass MyThread{\r\n    public void run(){\r\n        while(true){\r\n            try {\r\n                Thread.sleep(1000);\r\n            } catch (InterruptedException e) {\r\n                e.printStackTrace();\r\n            }\r\n            System.out.println(\"MyThread \u7c7b\u7684 run() \u65b9\u6cd5\u5728\u8fd0\u884c\");\r\n        }\r\n    }\r\n}<\/pre>\n<p>\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u7684main()\u65b9\u6cd5\u4e2d\u7684print\u8bed\u53e5\u5c06\u6c38\u8fdc\u65e0\u6cd5\u5f97\u5230\u6267\u884c\uff0c\u56e0\u4e3a\u8be5\u7a0b\u5e8f\u662f\u4e00\u4e2a\u5355\u7ebf\u7a0b\u7a0b\u5e8f\uff0c\u5f53\u8c03\u7528MyThread\u7c7b\u7684run()\u65b9\u6cd5\u65f6\uff0c\u4f1a\u9047\u5230\u6b7b\u5faa\u73af\uff0c\u5faa\u73af\u5c06\u4e00\u76f4\u8fdb\u884c\u3002<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e00\u6bb5\u4ee3\u7801\u76f8\u6bd4\u800c\u8a00\u5c31\u662f\u591a\u4e86\u4e00\u4e2aextends\u7ee7\u627f\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u4e0a\u9762\u7684\u90a3\u4e2a\u60c5\u51b5\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.misc;\r\n\r\n\/**\r\n * Created by ixyzero.com on 2018\/6\/6.\r\n *\/\r\npublic class TestNewThreadMethod {\r\n    public static void main(String[] args) {\r\n        MyThread2 myThread2 = new MyThread2();\r\n        myThread2.start();\r\n        while (true) {\r\n            System.out.println(\"main() method running...\");\r\n            try {\r\n                Thread.sleep(500);\r\n            } catch (InterruptedException e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\nclass MyThread2 extends Thread{\r\n    public void run(){\r\n        while(true){\r\n            System.out.println(\"run() in MyThread2\");\r\n            try {\r\n                Thread.sleep(1000);\r\n            } catch (InterruptedException e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h6><strong>\u591a\u7ebf\u7a0b\u4f7f\u7528\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/h6>\n<div class=\"article-body\">\n<div id=\"content\" class=\"article-intro\">\n<ul>\n<li><span style=\"color: #ff0000;\"><strong>\u6709\u6548\u5229\u7528\u591a\u7ebf\u7a0b\u7684\u5173\u952e\u662f\u7406\u89e3\u7a0b\u5e8f\u662f\u5e76\u53d1\u6267\u884c\u800c\u4e0d\u662f\u4e32\u884c\u6267\u884c\u7684\u3002<\/strong><\/span>\u4f8b\u5982\uff1a\u7a0b\u5e8f\u4e2d\u6709\u4e24\u4e2a\u5b50\u7cfb\u7edf\u9700\u8981\u5e76\u53d1\u6267\u884c\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u5229\u7528\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002<\/li>\n<li>\u901a\u8fc7\u5bf9\u591a\u7ebf\u7a0b\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u975e\u5e38\u9ad8\u6548\u7684\u7a0b\u5e8f\u3002\u4e0d\u8fc7\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u4f60\u521b\u5efa\u592a\u591a\u7684\u7ebf\u7a0b\uff0c\u7a0b\u5e8f\u6267\u884c\u7684\u6548\u7387\u5b9e\u9645\u4e0a\u662f\u964d\u4f4e\u4e86\uff0c\u800c\u4e0d\u662f\u63d0\u5347\u4e86\u3002<\/li>\n<li><span style=\"color: #ff0000;\">\u8bf7\u8bb0\u4f4f\uff0c\u4e0a\u4e0b\u6587\u7684\u5207\u6362\u5f00\u9500\u4e5f\u5f88\u91cd\u8981\uff0c\u5982\u679c\u4f60\u521b\u5efa\u4e86\u592a\u591a\u7684\u7ebf\u7a0b\uff0cCPU \u82b1\u8d39\u5728\u4e0a\u4e0b\u6587\u7684\u5207\u6362\u7684\u65f6\u95f4\u5c06\u591a\u4e8e\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u95f4\uff01<\/span><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.runoob.com\/java\/java-multithreading.html\">Java \u591a\u7ebf\u7a0b\u7f16\u7a0b<\/a> #nice<\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/qq_32823673\/article\/details\/78657281\">java&#8211;\uff08\u591a\u7ebf\u7a0b\u521b\u5efa\u7684\u4e24\u79cd\u65b9\u5f0f\u2014\u2014\u7ee7\u627fThread\u7c7b\u548c\u5b9e\u73b0Runnable\u63a5\u53e3\uff09<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/renhui\/p\/6066852.html\">Java Thread \u7684\u4f7f\u7528<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5b66\u4e60\u6574\u7406\u4e00\u4e0bJava\u4e2d\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u6837\u4f8b\u4ee3\u7801\uff0c\u65b9\u4fbf\u4ee5\u540e\u4f7f\u7528\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a \u4e00\u4e2a\u7ebf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[61,1147,124,1148],"class_list":["post-3968","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-java","tag-runnable","tag-thread","tag-1148"],"views":3206,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3968"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3968\/revisions"}],"predecessor-version":[{"id":3969,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3968\/revisions\/3969"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}