{"rsdb":{"rid":"309156","subhead":"","postdate":"0","aid":"224318","fid":"120","uid":"1","topic":"1","content":"
\n \n \n
\n

\u4e3b\u8981\u5185\u5bb9
\n[1]\u7ebf\u7a0b\u540c\u6b65
\n[2]\u751f\u4ea7\u8005\u4e0e\u6d88\u8d39\u8005
\n[3]\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5176\u5b83\u65b9\u5f0f\uff0c\u7ebf\u7a0b\u6c60\uff0c\u7ebf\u7a0b\u7ec4<\/p>\n\n


\n23.1\u4e3a\u4ec0\u4e48\u9700\u8981\u7ebf\u7a0b\u7684\u540c\u6b65\uff1f
\n\u591a\u7ebf\u7a0b\u5728\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u5bb9\u6613\u51fa\u73b0\u201c\u6570\u636e\u9519\u4e71\/\u810f\u6570\u636e\u201d\u7684\u60c5\u51b5\u3002
\n\u540c\u6b65\uff1a\u5b8c\u6210\u4e00\u4e2a\u529f\u80fd\uff0c\u9700\u8981N\u591a\u53e5\u4ee3\u7801\u7ec4\u6210\uff0c\u8fd9N\u591a\u53e5\u4ee3\u7801\u5fc5\u987b\u4e00\u8d77\u6267\u884c\u5b8c\u6bd5\uff0c\u624d\u4ee3\u8868\u4e00\u4e2a\u529f\u80fd\u7ed3\u675f\uff0c\u5728\u5b8c\u6210\u8fd9\u4e2a\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4ee3\u7801\u4e0d\u5141\u8bb8\u88ab\u201c\u6253\u65ad\u201d\uff0c\u540c\u6b65
\n\u540c\u6b65\"\u9501\"<\/p>\n\n

(1)\u5b9e\u73b0\u540c\u6b65\u65b9\u5f0f
\n 1.\u540c\u6b65\u4ee3\u7801\u5757<\/p>\n\n

\npackage com.bjsxt.titcket;\n\npublic class Ticetk2 implements Runnable{\/\/Ticetk\u8fd9\u4e2a\u7c7b\u5177\u5907\u4e86\u4e00\u4e2a\u80fd\u529b\uff0c\u591a\u7ebf\u7a0b\u64cd\u4f5c\u7684\u80fd\u529b\n        private int ticket=5;\/\/5\u5f20\u7968\n        @Override\n        public void run() {\n        \n                \/\/\u7ebf\u7a0b\u4f53\n                for (int i = 0; i <100; i++) {\n                        \/\/\u540c\u6b65\u4ee3\u7801\u5757\n                        synchronized (this) {\/\/\u5c0f\u62ec\u53f7\u4e2d\uff0c\u5171\u4eab\u8d44\u6e90\u7684\u5bf9\u8c61\n                                if (ticket>0) {\n                                        try {\n                                                Thread.sleep(1000);\n                                        } catch (InterruptedException e) {\n                                                \/\/ TODO Auto-generated catch block\n                                                e.printStackTrace();\n                                        }\n                                        System.out.println(Thread.currentThread().getName()+\"\u5356\u7b2c\"+ticket+\"\u5f20\u7968\");\n                                        ticket--;\n                                }\n                        }\n                }\n                \n        }\n        \n}<\/code><\/pre>\n\n

\u6d4b\u8bd5\uff1a<\/p>\n\n

\npackage com.bjsxt.titcket;\n\npublic class TestTicket {\n        public static void main(String[] args) {\n                \/\/\u521b\u5efa\u7ebf\u7a0b\u7c7b\u7684\u5bf9\u8c61\n                Ticetk2 t=new Ticetk2();  \/\/5\u5f20\u7968\n                \n                \/\/\u521b\u5efa\u4e09\u4e2a\u7ebf\u7a0b\u4ee3\u7406\u7c7b\n                Thread t1=new Thread(t,\"A\u7a97\u53e3\");\n                Thread t2=new Thread(t,\"B\u7a97\u53e3\");\n                Thread t3=new Thread(t,\"C\u7a97\u53e3\");\n                \n                \/\/\u542f\u52a8\u7ebf\u7a0b\n                t1.start(); \/\/for--0--100\n                t2.start();\/\/for--0--100\n                t3.start();\n        }\n}\n<\/code><\/pre>\n\n

2.\u540c\u6b65\u65b9\u6cd5<\/p>\n\n

\npackage com.bjsxt.titcket;\n\npublic class Ticetk3 implements Runnable{\/\/Ticetk\u8fd9\u4e2a\u7c7b\u5177\u5907\u4e86\u4e00\u4e2a\u80fd\u529b\uff0c\u591a\u7ebf\u7a0b\u64cd\u4f5c\u7684\u80fd\u529b\n        private int ticket=5;\/\/5\u5f20\u7968\n        @Override\n        public void run() {\n                \/\/\u7ebf\u7a0b\u4f53\n                for (int i = 0; i <100; i++) {\n                        \/\/\u8c03\u7528\n                        saleTicket();\/\/\u5356\u7968\u7684\u65b9\u6cd5\n                }\n                \n        }\n        \/\/\u540c\u6b65\u65b9\u6cd5  -->\u9501\u7684\u662f\u5f53\u524d\u7c7b\u7684\u5bf9\u8c61this\n        private synchronized void saleTicket(){\n                if (ticket>0) {\n                        try {\n                                Thread.sleep(1000);\n                        } catch (InterruptedException e) {\n                                \/\/ TODO Auto-generated catch block\n                                e.printStackTrace();\n                        }\n                        System.out.println(Thread.currentThread().getName()+\"\u5356\u7b2c\"+ticket+\"\u5f20\u7968\");\n                        ticket--;\n                }\n        }\n        \n}<\/code><\/pre>\n\n

\u6d4b\u8bd5\uff1a<\/p>\n\n

\npackage com.bjsxt.titcket;\n\npublic class TestTicket {\n        public static void main(String[] args) {\n                \/\/\u521b\u5efa\u7ebf\u7a0b\u7c7b\u7684\u5bf9\u8c61\n                \/\/Ticetk2 t=new Ticetk2();  \/\/5\u5f20\u7968\n                Ticetk3 t=new Ticetk3(); \n                \/\/\u521b\u5efa\u4e09\u4e2a\u7ebf\u7a0b\u4ee3\u7406\u7c7b\n                Thread t1=new Thread(t,\"A\u7a97\u53e3\");\n                Thread t2=new Thread(t,\"B\u7a97\u53e3\");\n                Thread t3=new Thread(t,\"C\u7a97\u53e3\");\n                \n                \/\/\u542f\u52a8\u7ebf\u7a0b\n                t1.start(); \/\/for--0--100\n                t2.start();\/\/for--0--100\n                t3.start();\n        }\n}<\/code><\/pre>\n\n

\u201c\u9501\u201d\u7684\u662f\u4ec0\u4e48\uff1f\u5171\u4eab\u8d44\u6e90\u5bf9\u8c61 \u88ab\u9501\u4f4f\u7684\u5bf9\u8c61\uff0c\u79f0\u4e3a\u201c\u540c\u6b65\u76d1\u89c6\u5668\u201d
\n \u540c\u6b65\u4ee3\u7801\u5757 -->\u540c\u6b65\u76d1\u89c6\u5668\uff0c\u53ef\u4ee5\u662f\u5171\u4eab\u8d44\u6e90\u7684\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u662f\u5f53\u524d\u7c7b\u5bf9\u8c61this
\n \u540c\u6b65\u65b9\u6cd5 -->\u540c\u6b65\u76d1\u89c6\u5668\uff0c\uff0c\uff0c\u53ea\u80fd\u662f\u5f53\u524d\u7c7b\u7684\u5bf9\u8c61this<\/p>\n\n


\n\u540c\u6b65\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5b89\u5168\u6027
\n\u8fc7\u591a\u7684\u540c\u6b65\uff0c\u5c31\u4f1a\u4ea7\u751f\u6b7b\u9501\uff0c\u53cc\u65b9\u90fd\u5728\u7b49\u5f85\u5bf9\u65b9\u7684\u8d44\u6e90
\n\u6309\u7167\u201cboss\u201d\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5927\u5c0f\uff0c\u5927\u7684\u5148\u83b7\u53d6\u9501<\/p>\n\n

23.2\u751f\u4ea7\u8005\u4e0e\u6d88\u8d39\u8005\u95ee\u9898
\n(1)\u6570\u636e\u9519\u4e71 (\u65fa\u4ed4\u77ff\u6cc9\u6c34\uff0c\u5a03\u54c8\u54c8\u5c0f\u9992\u5934) \u540c\u6b65<\/p>\n\n

(2)\u91cd\u590d\u751f\u4ea7\u548c\u91cd\u590d\u53d6\u8d70 (\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1)
\n\u7b49\u5f85:wait()
\n\u5524\u9192notify(\uff09\uff0cnotifyA()
\n\u4ee5\u4e0a\u65b9\u6cd5\u5747\u4e3aObject\u7c7b\u4e2d\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u88ab\u5b50\u7c7b\u8c03\u7528\uff0c\u4f46\u4e0d\u80fd\u88ab\u5b50\u7c7b\u91cd\u5199\uff0c\u56e0\u4e3a\u5747\u4e3afinal\u4fee\u9970\u7684\u65b9\u6cd5<\/p>\n\n

\u7ebf\u7a0b\u95f4\u901a\u4fe1\u7684\u65b9\u6cd5\uff0c\u53ea\u80fd\u7528\u5728\u540c\u6b65\u4ee3\u7801\u5757\u6216\u540c\u6b65\u65b9\u6cd5\u4e2d<\/p>\n\n


\nsleep(...)\u4e0ewait()\u7684\u533a\u522b
\n sleep\u4e0ewait\u90fd\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u8fdb\u5165\u963b\u585e\u72b6\u6001
\n(1)sleep\u65b9\u6cd5\u4e3aThread\u7c7b\u4e2d\u7684static\u65b9\u6cd5 \uff0cwait()Object\u7c7b\u4e2d\u7684final\u65b9\u6cd5
\n(2)sleep()\u4e0d\u4f1a\u91ca\u653e\u5bf9\u8c61\u9501,wait()\u4f1a\u91ca\u653e\u5bf9\u8c61\u9501\uff0c\u7b49\u5f85\u6c60\uff0c\u5f53notify()\u6216notifyAll()\u5524\u9192\u8fdb\u5165\u9501\u6c60\uff0c\u5c31\u7eea\u72b6\u6001
\n(3)wait()\u7b49\u5f85\uff0c\u5f53\u88ab\u5524\u9192\u540e\uff0c\u4ecewait()\u4e4b\u540e\u7684\u4ee3\u7801\u5f00\u59cb\u6267\u884c
\nif(){
\n wait();
\n}
\n....... wait()\u4e4b\u540e\u7684\u4ee3\u7801<\/p>\n\n

run()\u65b9\u6cd5\u6709\u6ca1\u6709\u5c40\u9650\u6027\uff1f \u6709 \uff0c\u56e0\u4e3a\u5982\u679c\u6709\u5f02\u5e38\uff0c\u5fc5\u987b\u5904\u7406\uff0c\u4e0d\u5141\u8bb8\u58f0\u660e
\n void\uff0c\u5982\u679c\u5e0c\u671b\u65b9\u6cd5\u8fd0\u884c\u4e4b\u540e\u6709\u7ed3\u679c\u600e\u4e48\u529e\uff1f<\/p>\n\n

\u5728Java\u4e2dJDK1.5\u5f00\u59cb\uff0c\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u65b9\u5f0f\uff0c\u6709\u4e09\u79cd\uff0c\u5b9e\u73b0Callable\u63a5\u53e3
\ncall()\u65b9\u6cd5\u7684\u597d\u5904
\n(1)\u6709\u8fd4\u56de\u503c
\n(2)\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38
\n(3)\u652f\u6301\u6cdb\u578b<\/p>\n\n

\npackage com.bjsxt.callable;\n\nimport java<\/a>.util.concurrent.Callable;\n\npublic class RandomCallable implements Callable<Integer>{\n        @Override\n        public Integer call() throws Exception {\n               \n                return (int)(Math.random()*10);\/\/0-9\n        }\n\n}<\/code><\/pre>\n\n

\u6d4b\u8bd5\uff1a<\/p>\n\n

\npackage com.bjsxt.callable;\n\nimport java<\/a>.util.concurrent.ExecutionException;\nimport java.util.concurrent.FutureTask;\n\npublic class Test {\n        public static void main(String[] args) throws InterruptedException, ExecutionException {\n                \/\/\u521b\u5efa\u4efb\u52a1\n                RandomCallable rc=new RandomCallable();\n                \n                \/\/\u4efb\u52a1\u7ba1\u7406\u5668  -->Runnable\u7684\u5b9e\u73b0\u7c7b\u4e0d\uff1f\u662f\uff0c\u7ebf\u7a0b\u5bf9\u8c61\u662f\uff1f\u662f\n                FutureTask<Integer> ft=new FutureTask<>(rc);\n                \n                \/\/\u521b\u5efa\u4ee3\u7406\u7c7b\u7684\u5bf9\u8c61\n                Thread t=new Thread(ft);  \/\/\u591a\u6001\uff0c\u63a5\u53e3\u4f5c\u65b9\u6cd5\u7684\u5f62\u5f0f\u53c2\u6570\uff0c\u53ef\u4ee5\u4f20\u5165Runnable\u7684\u4efb\u52a1\u5b9e\u73b0\u7c7b\n                \/**\u5224\u65ad\u4efb\u52a1\u662f\u5426\u6267\u884c\u5b8c\u6210*\/\n                System.out.println(\"\u4efb\u52a1\u662f\u5426\u6267\u884c\u5b8c\u6bd5\"+ft.isDone());\n                \/\/\u624d\u4f1a\u6267\u884ccall()\u65b9\u6cd5\u4e2d\u7684\u4ee3\u7801\n                t.start();\n                \/**\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u8fd9\u4e2a\u65f6\u5019 call()\u65b9\u6cd5\u672a\u5fc5\u6267\u884c\u5b8c\uff0c\u6709\u53ef\u80fd\u6267\u884c\u5230\u83b7\u53d6\u7ed3\u679c\u65f6\uff0c\u800c\u7ed3\u679c\u8fd8\u6ca1\u6709\u8ba1\u7b97\u51fa\u6765*\/\n                System.out.println(\"\u83b7\u53d6\u7ed3\u679c:\"+ft.get()); \/\/\u5bfc\u81f4\u540e\u9762\u7684\u4ee3\u7801\u65e0\u6cd5\u6267\u884c\n                \n                System.out.println(\"\u4efb\u52a1\u662f\u5426\u6267\u884c\u5b8c\u6bd5\"+ft.isDone());\n                \n                \n                \n        }\n}<\/code><\/pre>\n\n

\"\"<\/p> <\/div>\n <\/div>","orderid":"0","title":"\u591a\u7ebf\u7a0b\uff1a \u7ebf\u7a0b\u7684\u540c\u6b65   \u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1   \u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u5176\u5b83\u65b9\u5f0f","smalltitle":"","mid":"0","fname":"Kafka","special_id":"0","bak_id":"0","info":"0","hits":"865","pages":"1","comments":"0","posttime":"2019-05-16 02:31:18","list":"1557945078","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u7ebf\u7a0b<\/A> \u540c\u6b65<\/A>  <\/A> \u901a\u4fe1<\/A> \u5b9e\u73b0<\/A> \u5176\u5b83<\/A> \u65b9\u5f0f<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";s:4:\"head\";s:0:\"\";}","target":"0","ip":"47.106.78.186","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":" \u4e3b\u8981\u5185\u5bb9[1]\u7ebf\u7a0b\u540c\u6b65[2]\u751f\u4ea7\u8005\u4e0e\u6d88\u8d39\u8005[3]\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5176\u5b83\u65b9\u5f0f\uff0c\u7ebf\u7a0b\u6c60\uff0c\u7ebf\u7a0b\u7ec423.1\u4e3a\u4ec0\u4e48\u9700\u8981\u7ebf\u7a0b\u7684\u540c\u6b65\uff1f\u591a\u7ebf\u7a0b\u5728\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u5bb9\u6613\u51fa\u73b0\u201c\u6570\u636e\u9519\u4e71\/\u810f\u6570\u636e\u201d\u7684\u60c5\u51b5\u3002\u540c\u6b65\uff1a\u5b8c\u6210\u4e00\u4e2a\u529f\u80fd\uff0c\u9700\u8981N..","lastview":"1713291188","digg_num":"1010","digg_time":"1713272413","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}