{"id":8,"date":"2024-05-27T23:13:53","date_gmt":"2024-05-27T15:13:53","guid":{"rendered":"http:\/\/www.yixi.ltd\/?p=8"},"modified":"2024-05-28T19:27:52","modified_gmt":"2024-05-28T11:27:52","slug":"c%e5%ae%9e%e7%8e%b0%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%9a%84%e5%87%a0%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.bengbu.host\/?p=8","title":{"rendered":"C#\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u51e0\u79cd\u65b9\u5f0f"},"content":{"rendered":"\n<p>\u4f7f\u7528 Thread \u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public static void ThreadMethod()\n    {\n        var newThread = new Thread(WorkerMethod);\n        newThread.Start();\n        for (int i = 0; i &lt; 8; i++)\n        {\n            Console.WriteLine($\"ThreadMethod \u4e3b\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n            Thread.Sleep(100);\n        }\n    }\n    private static void WorkerMethod()\n    {\n        for (int i = 0; i &lt; 8; i++)\n        {\n            Console.WriteLine($\"WorkerMethod \u8f85\u52a9\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n            Thread.Sleep(100);\n        }\n    }<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528 ThreadPool \u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        public static void ThreadPoolMethod()\n        {\n            ThreadPool.QueueUserWorkItem(o => WorkerMethod());\n            for (int i = 0; i &lt; 8; i++)\n            {\n                Console.WriteLine($\"ThreadPoolMethod \u4e3b\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n                Thread.Sleep(100);\n            }\n        }\n        private static void WorkerMethod()\n        {\n            for (int i = 0; i &lt; 8; i++)\n            {\n                Console.WriteLine($\"WorkerMethod \u8f85\u52a9\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n                Thread.Sleep(100);\n            }\n        }<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528 Task \u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        public static void TaskMethod()\n        {\n            Task.Run(() => WorkerMethod());\n            for (int i = 0; i &lt; 8; i++)\n            {\n                Console.WriteLine($\"TaskMethod \u4e3b\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n                Task.Delay(100).Wait();\n            }\n        }\n        private static void WorkerMethod()\n        {\n            for (int i = 0; i &lt; 8; i++)\n            {\n                Console.WriteLine($\"WorkerMethod \u8f85\u52a9\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n                Thread.Sleep(100);\n            }\n        }<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528 Parallel \u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        public static void ParallelMethod()\n        {\n            Parallel.Invoke(WorkerMethod, WorkerMethodOther1, WorkerMethodOther2);\n        }\n        private static void WorkerMethod()\n        {\n            for (int i = 0; i &lt; 8; i++)\n            {\n                Console.WriteLine($\"WorkerMethod \u8f85\u52a9\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n                Thread.Sleep(100);\n            }\n        }\n        private static void WorkerMethodOther1()\n        {\n            for (int i = 0; i &lt; 8; i++)\n            {\n                Console.WriteLine($\"WorkerMethodOther1 \u8f85\u52a9\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n                Thread.Sleep(100);\n            }\n        }\n        private static void WorkerMethodOther2()\n        {\n            for (int i = 0; i &lt; 8; i++)\n            {\n                Console.WriteLine($\"WorkerMethodOther2 \u8f85\u52a9\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c\uff1a{i}\");\n                Thread.Sleep(100);\n            }\n        }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Thread \u7c7b \u4f7f\u7528 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-8","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/posts\/8","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":1,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":9,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions\/9"}],"wp:attachment":[{"href":"https:\/\/www.bengbu.host\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bengbu.host\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}