{"id":40335,"date":"2024-11-26T17:30:24","date_gmt":"2024-11-26T09:30:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40335\/"},"modified":"2024-11-26T17:30:24","modified_gmt":"2024-11-26T09:30:24","slug":"%e7%ba%bf%e7%a8%8b%e4%bc%98%e5%85%88%e7%ba%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40335\/","title":{"rendered":"\u7ebf\u7a0b\u4f18\u5148\u7ea7"},"content":{"rendered":"<p><strong>\u4e3b\u8981\u6982\u5ff5\uff1a<\/strong><br \/> java \u4e2d\u7684\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2a\u5173\u8054\u7684\u4f18\u5148\u7ea7\uff0c\u8be5\u4f18\u5148\u7ea7\u4f1a\u5f71\u54cd\u8be5\u7ebf\u7a0b\u76f8\u5bf9\u4e8e\u5176\u4ed6\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\u63a5\u6536\u7684 cpu \u65f6\u95f4\u91cf\u3002\u4f7f\u7528 setpriority(int level) \u548c getpriority() \u65b9\u6cd5\u914d\u7f6e\u4f18\u5148\u7ea7\u3002\u7ea7\u522b\u8303\u56f4\u4ece 1 (min_priority) \u5230 10 (max_priority)\uff0c\u9ed8\u8ba4\u4e3a 5 (norm_priority)\u3002<\/p>\n<p>\u867d\u7136\u4f18\u5148\u7ea7\u4f1a\u5f71\u54cd\u8c03\u5ea6\uff0c\u4f46\u8d44\u6e90\u9501\u548c\u5b9e\u73b0\u7b49\u5176\u4ed6\u56e0\u7d20\u4e5f\u4f1a\u5f71\u54cd\u6267\u884c\u3002<\/p>\n<p><strong>\u4ee3\u7801\u89e3\u91ca<\/strong><br \/> \u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u914d\u7f6e\u548c\u89c2\u5bdf\u4f18\u5148\u7ea7\u7684\u5f71\u54cd\uff1a<\/p>\n<pre>\/\/ demonstra as prioridades das threads.\nclass priority implements runnable {\n    int count;\n    thread thrd;\n    static boolean stop = false;\n    static string currentname;\n\n    \/\/ construtor para criar uma nova thread\n    priority(string name) {\n        thrd = new thread(this, name);\n        count = 0;\n        currentname = name;\n    }\n\n    \/\/ define o comportamento da thread\n    public void run() {\n        system.out.println(thrd.getname() + \" starting.\");\n        do {\n            count++;\n            if (!currentname.equals(thrd.getname())) {\n                currentname = thrd.getname();\n                system.out.println(\"in \" + currentname);\n            }\n        } while (!stop &amp;&amp; count &lt; 10000000);\n        stop = true; \/\/ interrompe outras threads ao alcan\u00e7ar 10.000.000\n        system.out.println(\"\n\" + thrd.getname() + \" terminating.\");\n    }\n}\n\nclass prioritydemo {\n    public static void main(string args[]) {\n        \/\/ cria duas threads com diferentes prioridades\n        priority mt1 = new priority(\"high priority\");\n        priority mt2 = new priority(\"low priority\");\n\n        \/\/ configura as prioridades\n        mt1.thrd.setpriority(thread.norm_priority + 2); \/\/ alta prioridade\n        mt2.thrd.setpriority(thread.norm_priority - 2); \/\/ baixa prioridade\n\n        \/\/ inicia as threads\n        mt1.thrd.start();\n        mt2.thrd.start();\n\n        try {\n            mt1.thrd.join();\n            mt2.thrd.join();\n        } catch (interruptedexception exc) {\n            system.out.println(\"main thread interrupted.\");\n        }\n\n        \/\/ exibe os resultados\n        system.out.println(\"\nhigh priority thread counted to \" + mt1.count);\n        system.out.println(\"low priority thread counted to \" + mt2.count);\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7a0b\u5e8f\u4e2d\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p><strong>\u5177\u6709\u4e0d\u540c\u4f18\u5148\u7ea7\u7684\u7ebf\u7a0b\uff1a<\/strong><br \/> \u7ebf\u7a0b mt1 \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e mt2\u3002<br \/> \u8fd9\u610f\u5473\u7740mt1\u6709\u66f4\u5927\u7684\u673a\u4f1a\u8bbf\u95eecpu\uff0c\u4f46\u4e0d\u662f\u7edd\u5bf9\u4fdd\u8bc1\u3002<\/p>\n<p><strong>\u8ba1\u6570\u5faa\u73af\u548c\u7ebf\u7a0b\u5207\u6362\uff1a<\/strong><br \/> \u6bcf\u4e2a\u7ebf\u7a0b\u9012\u589e\u4e00\u4e2a\u8ba1\u6570\u53d8\u91cf\uff0c\u76f4\u5230\u8fbe\u5230 10,000,000\u3002<br \/> \u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u5148\u5b8c\u6210\uff0c\u9759\u6001\u53d8\u91cf stop \u4f1a\u505c\u6b62\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p><strong>\u4ea4\u66ff\u89c2\u5bdf\uff1a<\/strong><br \/> \u5f53\u6267\u884c\u5728\u7ebf\u7a0b\u4e4b\u95f4\u5207\u6362\u65f6\u663e\u793a\u7ebf\u7a0b\u540d\u79f0\uff08currentname\uff09\u3002<\/p>\n<p><strong>\u4f18\u5148\u7ea7\u548c\u7ed3\u679c\uff1a<\/strong><br \/> \u5728\u5178\u578b\u793a\u4f8b\u4e2d\uff0c\u9ad8\u4f18\u5148\u7ea7\u7ebf\u7a0b\u8fd0\u884c\u901f\u5ea6\u66f4\u5feb\uff0c\u8fbe\u5230 10,000,000 \u7684\u76ee\u6807\uff0c\u800c\u5176\u4ed6\u7ebf\u7a0b\u5219\u88ab\u4e2d\u65ad\u3002<\/p>\n<p>\u793a\u4f8b\u8f93\u51fa<\/p>\n<pre>High Priority starting.\nIn High Priority\nLow Priority starting.\nIn Low Priority\nIn High Priority\nHigh Priority terminating.\nLow Priority terminating.\n\nHigh priority thread counted to 10000000\nLow priority thread counted to 8183\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u89c2\u5bdf\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u8f93\u51fa\u53ef\u80fd\u4f1a\u56e0\u64cd\u4f5c\u7cfb\u7edf\u3001\u7cfb\u7edf\u8d1f\u8f7d\u548c\u786c\u4ef6\u800c\u5f02\u3002<\/li>\n<li>\u5728\u591a\u6838\u7cfb\u7edf\u4e0a\uff0c\u4f18\u5148\u7ea7\u7684\u5f71\u54cd\u53ef\u80fd\u4e0d\u592a\u660e\u663e\uff0c\u56e0\u4e3a\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173232569039851.jpg\" class=\"aligncenter\" title=\"\u7ebf\u7a0b\u4f18\u5148\u7ea7\u63d2\u56fe\" alt=\"\u7ebf\u7a0b\u4f18\u5148\u7ea7\u63d2\u56fe\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7ebf\u7a0b\u4f18\u5148\u7ea7\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u8981\u6982\u5ff5\uff1a java \u4e2d\u7684\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2a\u5173\u8054\u7684\u4f18\u5148\u7ea7\uff0c\u8be5\u4f18\u5148\u7ea7\u4f1a\u5f71\u54cd\u8be5\u7ebf\u7a0b\u76f8\u5bf9\u4e8e\u5176\u4ed6\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\u63a5\u6536\u7684 cpu \u65f6\u95f4\u91cf\u3002\u4f7f\u7528 setpriority(int level) \u548c getpriority() \u65b9\u6cd5\u914d\u7f6e\u4f18\u5148\u7ea7\u3002\u7ea7\u522b\u8303\u56f4\u4ece 1 (min_priority) \u5230 10 (max_priority)\uff0c\u9ed8\u8ba4\u4e3a 5 (norm_priority)\u3002 \u867d\u7136\u4f18\u5148\u7ea7\u4f1a\u5f71\u54cd\u8c03\u5ea6\uff0c\u4f46\u8d44\u6e90\u9501\u548c\u5b9e\u73b0\u7b49\u5176\u4ed6\u56e0\u7d20\u4e5f\u4f1a\u5f71\u54cd\u6267\u884c\u3002 \u4ee3\u7801\u89e3\u91ca \u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u914d\u7f6e\u548c\u89c2\u5bdf\u4f18\u5148\u7ea7\u7684\u5f71\u54cd\uff1a \/\/ demonstra as prioridades das threads. class priority implements runnable { int count; thread thrd; static boolean stop = false; static string currentname; \/\/ construtor para criar uma nova thread priority(string name) { thrd = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-40335","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=40335"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40335\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}