{"id":26120,"date":"2011-09-19T18:42:21","date_gmt":"2011-09-19T10:42:21","guid":{"rendered":"http:\/\/www.yankay.com\/?p=26120"},"modified":"2011-09-19T18:42:21","modified_gmt":"2011-09-19T10:42:21","slug":"java%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e7%a8%8b%e5%ba%8f%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"http:\/\/yankay.com\/java%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8%e7%a8%8b%e5%ba%8f%e6%8a%80%e5%b7%a7\/","title":{"rendered":"Java\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u6280\u5de7"},"content":{"rendered":"
\u524d\u4e9b\u5929\u4f7f\u7528Java\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u7ebf\u7a0b\u4f1a\u5835\u585e\u5728waitfor()\u65b9\u6cd5\u3002
\n\u8c03\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n
\nProcess process = Runtime.getRuntime().exec(cmd);\nprocess.waitfor();\n<\/pre>\n\u5982\u679c\u76f4\u63a5\u5728Shell\u4e2d\u8c03\u7528\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u4f1a\u5f88\u5feb\u7ed3\u675f\uff0c\u4e0d\u4f1a\u50f5\u6b7b\u3002<\/p>\n
\u4e3a\u4ec0\u4e48\u4f1a\u5835\u585e\u5462\uff0c\u539f\u56e0\u662f\u5f53\u8c03\u7528exec(cmd)\u540e\uff0cJVM\u4f1a\u542f\u52a8\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u8be5\u8fdb\u7a0b\u4f1a\u4e0eJVM\u8fdb\u7a0b\u5efa\u7acb3\u4e2a\u7ba1\u9053\u8fde\u63a5\uff0c\u6807\u51c6\u8f93\u5165\uff0c\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u6d41\u3002\u5047\u8bbe\u8be5\u7a0b\u5e8f\u4e0d\u65ad\u5728\u5411\u6807\u51c6\u8f93\u51fa\u6d41\u548c\u6807\u51c6\u9519\u8bef\u6d41\u5199\u6570\u636e\uff0c\u800cJVM\u4e0d\u8bfb\u53d6\uff0c\u6570\u636e\u4f1a\u6682\u65f6\u7f13\u51b2\u5728Linux\u7684\u7f13\u51b2\u533a\uff0c\u7f13\u51b2\u533a\u6ee1\u540e\u8be5\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u7ee7\u7eed\u5199\u6570\u636e\uff0c\u4f1a\u50f5\u6b7b\uff0c\u6240\u4ee5Java\u7a0b\u5e8f\u5c31\u4f1a\u50f5\u6b7b\u5728waitfor()\uff0c\u6c38\u8fdc\u65e0\u6cd5\u7ed3\u675f\u3002<\/p>\n
\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u589e\u52a0\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u8d1f\u8d23\u8bfb\u6807\u51c6\u8f93\u51fa\u6d41\uff0c\u53e6\u4e00\u4e2a\u8d1f\u8d23\u8bfb\u6807\u51c6\u9519\u8bef\u6d41\uff0c\u8fd9\u6837\u5b50\u6570\u636e\u5c31\u4e0d\u4f1a\u79ef\u538b\u5728\u7f13\u51b2\u533a\uff0c\u7a0b\u5e8f\u5c31\u80fd\u591f\u987a\u5229\u8fd0\u884c\u3002<\/p>\n
\u67e5\u770b\u6e90\u4ee3\u7801\u540e\uff0c\u8fd8\u53d1\u73b0\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\u3002\u4f46\u7a0b\u5e8f\u6267\u884c\u5230exec\u7684\u65f6\u5019\uff0cJVM\u4f1a\u4f7f\u7528\u7ba1\u9053\uff0c\u5360\u67093\u4e2a\u6587\u4ef6\u53e5\u67c4\uff0c\u4f46\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\u540e\uff0c\u8fd9\u4e09\u4e2a\u53e5\u67c4\u5e76\u4e0d\u4f1a\u81ea\u52a8\u5173\u95ed\uff0c\u8fd9\u6837\u6700\u7ec8\u4f1a\u5bfc\u81f4java.io.IOException: Too many open files\u3002\u6240\u4ee5\u5c31\u7b97\u5916\u90e8\u7a0b\u5e8f\u7684\u6ca1\u6709\u8f93\u51fa\uff0c\u4e5f\u5fc5\u987b\u5173\u95ed\u53e5\u67c4\uff1a<\/p>\n
\nProcess process=null;\ntry{\n process = Runtime.getRuntime().exec(cmd);\n process.waitfor();\n}cache{\n process.getOutputStream().close();\n process.getInputStream().close();\n process.getErrorStream().close();\n}\n<\/pre>\n\u6211\u4eec\u53d1\u89c9\u5f53\u8c03\u7528close()\u65b9\u6cd5\u540e,JVM\u5e76\u4e0d\u4f1a\u7acb\u5373\u56de\u6536\u53e5\u67c4\uff0c\u5177\u4f53\u7684\u56de\u6536\u65f6\u95f4\u4e0d\u786e\u5b9a\u3002\u53e6\u5916\u5982\u679c\u4e0d\u8c03\u7528close(),\u53e5\u67c4\u4e5f\u4f1a\u88ab\u56de\u6536\uff0c\u4e5f\u53ef\u80fd\u53d1\u751f\u201cToo many open files\u201d\u7684\u9519\u8bef\u3002\u6839\u636e\u8fd9\u7bc7\u6587\u7ae0<\/a>\uff0c\u4e0d\u540c\u7684\u5783\u573e\u6536\u96c6\u5668\u4f1a\u9009\u62e9\u4e0d\u540c\u7684\u56de\u6536\u7b56\u7565\u3002\u6240\u4ee5\u6700\u597d\u8fd8\u662f\u8981\u5173\u95ed\u3002<\/p>\n\u603b\u7ed3<\/strong>
\n1.\u5982\u679c\u5916\u90e8\u7a0b\u5e8f\u6709\u5927\u91cf\u8f93\u51fa,\u9700\u8981\u542f\u52a8\u989d\u5916\u7684\u7ebf\u7a0b\u6765\u8bfb\u53d6\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u6d41
\n2.\u5fc5\u987b\u5173\u95ed\u4e09\u4e2a\u53e5\u67c4<\/p>\n\u53e6\u5916\u7f16\u5199\u4e86\u4e00\u4e2a\u5de5\u5177\u7c7b\u6765\u65b9\u4fbf\u4f7f\u7528\uff0c\u672c\u6765\u4e24\u884c\u4ee3\u7801\u786e\u8981\u5199\u6210\u8fd9\u4e48\u957f\uff0c\u6709\u70b9\u5c0f\u6298\u817e\u4e86\u3002\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u5728\u8fd9\u91cc<\/a>\u4e0b\u8f7d\uff1a<\/p>\n","protected":false},"excerpt":{"rendered":"\u524d\u4e9b\u5929\u4f7f\u7528Java\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u7ebf\u7a0b\u4f1a\u5835\u585e\u5728waitfor()\u65b9\u6cd5\u3002<\/p>\n