{"id":162,"date":"2015-04-10T19:31:18","date_gmt":"2015-04-10T11:31:18","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=162"},"modified":"2017-08-18T08:16:34","modified_gmt":"2017-08-18T00:16:34","slug":"java%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=162","title":{"rendered":"Java\u591a\u7ebf\u7a0b\u673a\u5236"},"content":{"rendered":"<h2>\u4e00 \u7ebf\u7a0b\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u7ebf\u7a0b\u662f\u4e00\u4e2a\u7a0b\u5e8f\u5185\u90e8\u7684\u987a\u5e8f\u63a7\u5236\u6d41.\u4e00\u4e2a\u8fdb\u7a0b\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4efb\u52a1,\u4e00\u4e2a\u7ebf\u7a0b\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4efb\u52a1\u4e2d\u7684\u4e00\u6761\u6267\u884c\u8def\u5f84.<br \/>\n\u591a\u8fdb\u7a0b:\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u80fd\u540c\u65f6<code><\/code>\u8fd0\u884c\u591a\u4e2a\u4efb\u52a1(\u7a0b\u5e8f)<br \/>\n\u591a\u7ebf\u7a0b:\u5728\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6709\u591a\u4e2a\u987a\u5e8f\u6d41\u540c\u65f6\u6267\u884c<br \/>\nJava\u7684\u7ebf\u7a0b\u662f\u901a\u8fc7java.lang.Thread\u7c7b\u6765\u5b9e\u73b0\u7684<br \/>\n<!--more--><\/p>\n<p>JVM\u542f\u52a8\u65f6\u4f1a\u6709\u4e00\u4e2a\u7531\u4e3b\u65b9\u6cd5(public static void main(){})\u6240\u5b9a\u4e49\u7684\u7ebf\u7a0b<br \/>\n\u53ef\u4ee5\u901a\u8fc7\u521b\u5efaThread\u7684\u5b9e\u4f8b\u6765\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b<br \/>\n\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u662f\u901a\u8fc7\u67d0\u4e2a\u7279\u5b9aThread\u5bf9\u8c61\u6240\u5bf9\u5e94\u7684\u65b9\u6cd5run()\u6765\u5b8c\u6210\u5176\u64cd\u4f5c\u7684,\u65b9\u6cd5run()\u79f0\u4e3a\u7ebf\u7a0b\u4f53<br \/>\n\u901a\u8fc7\u8c03\u7528Thread\u7c7b\u7684start()\u65b9\u6cd5\u6765\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n<h2>\u4e8c \u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u542f\u52a8<\/h2>\n<p>\u53ef\u4ee5\u6709\u4e24\u79cd\u65b9\u5f0f\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b:<br \/>\n\u7b2c\u4e00\u79cd:<br \/>\n1.\u5b9a\u4e49\u7ebf\u7a0b\u7c7b\u5b9e\u73b0Runnable\u63a5\u53e3<br \/>\n2.Thread myThread = new Thread(target); \/\/target\u4e3aRunnable\u63a5\u53e3\u7c7b\u578b<br \/>\n3.Runnable\u4e2d\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5:public void run();\u7528\u4ee5\u5b9a\u4e49\u7ebf\u7a0b\u8fd0\u884c\u4f53<br \/>\n4.\u4f7f\u7528Runnable\u63a5\u53e3\u53ef\u4ee5\u4e3a\u591a\u4e2a\u7ebf\u7a0b\u63d0\u4f9b\u5171\u4eab\u7684\u6570\u636e<br \/>\n5.\u5728\u5b9e\u73b0Runnable\u63a5\u53e3\u7684\u7c7b\u7684run()\u65b9\u6cd5\u5b9a\u4e49\u4e2d\u53ef\u4ee5\u4f7f\u7528Thread\u7684\u9759\u6001\u65b9\u6cd5public static Thread currentThread();\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684\u5f15\u7528<\/p>\n<p>\u7b2c\u4e8c\u79cd:<br \/>\n1.\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2aThread\u7684\u5b50\u7c7b\u5e76\u91cd\u5199\u5176run\u65b9\u6cd5\u5982:<br \/>\n[java]class MyThread extends Thread {<br \/>\npublic void run() {&#8230;}<\/p>\n<p>}[\/java]<br \/>\n2.\u7136\u540e\u751f\u6210\u8be5\u7c7b\u7684\u5bf9\u8c61:<br \/>\nMyThread myThread = new MyThread();<\/p>\n<h2>\u4e09 \u7ebf\u7a0b\u63a7\u5236\u7684\u57fa\u672c\u65b9\u6cd5<\/h2>\n<p>isAlive():\u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u8fd8&#8221;\u6d3b&#8221;\u7740<br \/>\ngetPriority():\u83b7\u5f97\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u6570\u503c<br \/>\nsetPriority():\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u6570\u503c<br \/>\nThread.sleep():\u5c06\u5f53\u524d\u7ebf\u7a0b\u7761\u7720\u6307\u5b9a\u6beb\u79d2\u6570<br \/>\njoin():\u8c03\u7528\u67d0\u7ebf\u7a0b\u7684\u8be5\u65b9\u6cd5,\u5c06\u5f53\u524d\u7ebf\u7a0b\u4e0e\u8be5\u7ebf\u7a0b&#8221;\u5408\u5e76&#8221;,\u5373\u7b49\u5f85\u8be5\u7ebf\u7a0b\u7ed3\u675f,\u518d\u6062\u590d\u5f53\u524d\u7ebf\u7a0b\u7684\u8fd0\u884c<br \/>\nyield():\u8ba9\u51facpu,\u5f53\u524d\u7ebf\u7a0b\u8fdb\u5165\u5c31\u7eea\u961f\u5217\u7b49\u5f85\u8c03\u5ea6<br \/>\nwait():\u5f53\u524d\u7ebf\u7a0b\u8fdb\u5165\u5bf9\u8c61\u7684wait pool<br \/>\nnotify()\/notifyAll():\u5524\u9192\u5bf9\u8c61\u7684wait pool\u4e2d\u7684\u4e00\u4e2a\/\u6240\u6709\u7b49\u5f85\u7ebf\u7a0b<\/p>\n<h2>\u56db \u7ebf\u7a0b\u95f4\u901a\u8baf<\/h2>\n<p>\u5b9e\u73b0\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u95ee\u9898\u6765\u8bf4\u660e\u7ebf\u7a0b\u901a\u8baf\u95ee\u9898,\u4e3e\u4f8b\u5982\u4e0b\u6240\u793a:<\/p>\n<p>[java]\/**<br \/>\n * \u751f\u4ea7\u8005\u6d88\u8d39\u8005\u95ee\u9898<br \/>\n *\/<br \/>\npackage com.basic.thread;<\/p>\n<p>public class ProducerConsumer {<\/p>\n<p>    \/**<br \/>\n     * @param args<br \/>\n     *\/<br \/>\n    public static void main(String[] args) {<br \/>\n        ProductBox pb = new ProductBox();<br \/>\n        Producer p = new Producer(pb);<br \/>\n        Consumer c = new Consumer(pb);<\/p>\n<p>        Thread pThread = new Thread(p);<br \/>\n        Thread cThread = new Thread(c);<br \/>\n        pThread.setPriority(Thread.MAX_PRIORITY);<\/p>\n<p>        pThread.start();<br \/>\n        cThread.start();<br \/>\n    }<\/p>\n<p>}<br \/>\n\/**<br \/>\n * \u4ea7\u54c1\u5bf9\u8c61<br \/>\n *\/<br \/>\nclass Product {<br \/>\n    int id;<\/p>\n<p>    public Product(int id) {<br \/>\n        super();<br \/>\n        this.id = id;<br \/>\n    }<\/p>\n<p>    public String toString(){<br \/>\n        return &quot;Product:&quot; + id;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \u4ea7\u54c1\u76d2\u5bf9\u8c61<br \/>\n *\/<br \/>\nclass ProductBox {<\/p>\n<p>    Product[] productbox = new Product[6];<br \/>\n    int index = 0;<br \/>\n    public ProductBox() {<br \/>\n        super();<br \/>\n    }<\/p>\n<p>    public synchronized void push(Product p) {<br \/>\n        while (index == productbox.length) {<br \/>\n            try {<br \/>\n                this.wait();<br \/>\n            } catch (InterruptedException e) {<br \/>\n                \/\/ TODO Auto-generated catch block<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }<br \/>\n        this.notify();<br \/>\n        productbox[index] = p;<br \/>\n        index ++;<br \/>\n    }<\/p>\n<p>    public synchronized Product pop() {<br \/>\n        while (index == 0) {<br \/>\n            try {<br \/>\n                this.wait();<br \/>\n            } catch (InterruptedException e) {<br \/>\n                \/\/ TODO Auto-generated catch block<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }<br \/>\n        this.notify();<br \/>\n        index &#8211;;<br \/>\n        return productbox[index];<\/p>\n<p>    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \u751f\u4ea7\u8005<br \/>\n *\/<br \/>\nclass Producer implements Runnable {<\/p>\n<p>    ProductBox productbox = null;<\/p>\n<p>    public Producer(ProductBox productbox) {<br \/>\n        super();<br \/>\n        this.productbox = productbox;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public void run() {<br \/>\n        \/\/ TODO Auto-generated method stub<br \/>\n        for (int i=0; i&lt;10; i++) {<br \/>\n            Product p = new Product(i);<br \/>\n            productbox.push(p);<br \/>\n            System.out.println(&quot;produce:&quot; + p);<\/p>\n<p>            try {<br \/>\n                Thread.sleep((int)(Math.random() * 200));<br \/>\n            } catch (InterruptedException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>\/**<br \/>\n * \u6d88\u8d39\u8005<br \/>\n *\/<br \/>\nclass Consumer implements Runnable {<\/p>\n<p>    ProductBox productbox = null;<\/p>\n<p>    public Consumer(ProductBox productbox) {<br \/>\n        super();<br \/>\n        this.productbox = productbox;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public void run() {<br \/>\n        \/\/ TODO Auto-generated method stub<br \/>\n        for (int i=0; i&lt;10; i++) {<br \/>\n            Product p = productbox.pop();<br \/>\n            System.out.println(&quot;consume:&quot; + p);<\/p>\n<p>            try {<br \/>\n                Thread.sleep((int)(Math.random() * 1000));<br \/>\n            } catch (InterruptedException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>}[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00 \u7ebf\u7a0b\u7684\u57fa\u672c\u6982\u5ff5 \u7ebf\u7a0b\u662f\u4e00\u4e2a\u7a0b\u5e8f\u5185\u90e8\u7684\u987a\u5e8f\u63a7\u5236\u6d41.\u4e00\u4e2a\u8fdb\u7a0b\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4efb\u52a1,\u4e00\u4e2a\u7ebf\u7a0b\u76f8\u5f53\u4e8e\u4e00\u4e2a\u4efb\u52a1\u4e2d\u7684\u4e00\u6761\u6267\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/162"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=162"}],"version-history":[{"count":7,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":2128,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/162\/revisions\/2128"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}