{"id":304,"date":"2022-06-30T15:41:24","date_gmt":"2022-06-30T07:41:24","guid":{"rendered":"https:\/\/www.fengjijiao.cn\/?p=304"},"modified":"2022-06-30T15:41:24","modified_gmt":"2022-06-30T07:41:24","slug":"c%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.fengjijiao.cn\/?p=304","title":{"rendered":"C#\u591a\u7ebf\u7a0b"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System;\r\nusing System.Threading;\r\n\r\nnamespace ConsoleApp1\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string&#91;] args)\r\n        {\r\n            \/\/C#\u591a\u7ebf\u7a0b\r\n            \/*\r\n             * \u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\r\n             * \u7ebf\u7a0b\u751f\u547d\u5468\u671f\u5f00\u59cb\u4e8eSystem.Threading.Thread\u7c7b\u7684\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\uff0c\u7ed3\u675f\u4e8e\u7ebf\u7a0b\u88ab\u7ec8\u6b62\u6216\u5b8c\u6210\u6267\u884c\u65f6\u3002\r\n             * \u5404\u79cd\u72b6\u6001\uff1a\r\n             * \u672a\u542f\u52a8\u72b6\u6001\uff1a\u5f53\u7ebf\u7a0b\u5b9e\u4f8b\u88ab\u521b\u5efa\u4f46Start\u65b9\u6cd5\u672a\u88ab\u8c03\u7528\u65f6\u7684\u72b6\u6001\r\n             * \u5c31\u7eea\u72b6\u6001\uff1a\u5f53\u7ebf\u7a0b\u51c6\u5907\u597d\u8fd0\u884c\u5e76\u7b49\u5f85CPU\u5468\u671f\u65f6\u7684\u72b6\u6001\r\n             * \u4e0d\u53ef\u8fd0\u884c\u72b6\u6001\uff1a\u5305\u542b\u4e0b\u9762\u51e0\u79cd\u60c5\u51b5\r\n             * \u5df2\u7ecf\u8c03\u7528Sleep\u65b9\u6cd5\r\n             * \u5df2\u7ecf\u8c03\u7528Wait\u65b9\u6cd5\r\n             * \u901a\u8fc7I\/O\u64cd\u4f5c\u963b\u585e\r\n             * \u6b7b\u4ea1\u72b6\u6001\uff1a\u5f53\u7ebf\u7a0b\u5df2\u5b8c\u6210\u6307\u5411\u6216\u5df2\u4e2d\u6b62\u65f6\u7684\u72b6\u6001\r\n             *\/\r\n            \/\/\u4e3b\u7ebf\u7a0b\r\n            \/*\r\n             * System.Threading.Thread\u7c7b\u7528\u4e8e\u7ebf\u7a0b\u7684\u5de5\u4f5c\u3002\u5b83\u5141\u8bb8\u521b\u5efa\u5e76\u8bbf\u95ee\u591a\u7ebf\u7a0b\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5355\u4e2a\u7ebf\u7a0b\u3002\u8fdb\u7a0b\u4e2d\u7b2c\u4e00\u4e2a\u88ab\u6267\u884c\u7684\u7ebf\u7a0b\u79f0\u4e3a\u4e3b\u7ebf\u7a0b\u3002\r\n             * \u5f53C#\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u81ea\u52a8\u521b\u5efa\u3002\u4f7f\u7528Thread\u7c7b\u521b\u5efa\u7684\u7ebf\u7a0b\u88ab\u4e3b\u7ebf\u7a0b\u7684\u5b50\u7ebf\u7a0b\u8c03\u7528\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Thread\u7c7b\u7684CurrrentThread\u5c5e\u6027\u8bbf\u95ee\u7ebf\u7a0b\u3002\r\n             *\/\r\n            Thread th1 = Thread.CurrentThread;\r\n            th1.Name = \"MainThread\";\r\n            Console.WriteLine(\"This is {0}\", th1.Name);\r\n            \/*\r\n             * Thread\u7c7b\u5e38\u7528\u5c5e\u6027\r\n             * CurrentContext\u83b7\u53d6\u7ebf\u7a0b\u6b63\u5728\u5176\u4e2d\u6267\u884c\u7684\u5f53\u524d\u4e0a\u4e0b\u6587\r\n             * CurrentCulture\u83b7\u53d6\u6216\u8bbe\u7f6e\u5f53\u524d\u7ebf\u7a0b\u7684\u533a\u57df\u6027\r\n             * CurrentPrincipal\u83b7\u53d6\u6216\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u5f53\u524d\u8d1f\u8d23\u4eba\uff08\u5bf9\u57fa\u4e8e\u89d2\u8272\u7684\u5b89\u5168\u6027\u800c\u8a00\uff09\r\n             * CurrentThread\u83b7\u53d6\u5f53\u524d\u6b63\u5728\u5141\u8bb8\u7684\u7ebf\u7a0b\r\n             * CurrentUICulture\u83b7\u53d6\u6216\u8bbe\u7f6e\u8d44\u6e90\u7ba1\u7406\u5668\u4f7f\u7528\u7684\u5f53\u524d\u533a\u57df\u6027\u4ee5\u4fbf\u5728\u8fd0\u884c\u65f6\u67e5\u627e\u533a\u57df\u6027\u7279\u5b9a\u7684\u8d44\u6e90\r\n             * ExecutionContext\u83b7\u53d6\u4e00\u4e2aExecutionContext\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u6709\u5173\u5f53\u524d\u7a0b\u5e8f\u7684\u5404\u79cd\u4e0a\u4e0b\u6587\u7684\u4fe1\u606f\r\n             * IsAlive\u83b7\u53d6\u4e00\u4e2a\u503c\uff0c\u8be5\u503c\u6307\u793a\u5f53\u524d\u7ebf\u7a0b\u7684\u6267\u884c\u72b6\u6001\r\n             * IsBackground\u83b7\u53d6\u6216\u8bbe\u7f6e\u4e00\u4e2a\u503c\uff0c\u8be5\u503c\u6307\u793a\u67d0\u4e2a\u7ebf\u7a0b\u662f\u5426\u4e3a\u540e\u53f0\u7ebf\u7a0b\r\n             * IsThreadPoolThread\u83b7\u53d6\u4e00\u4e2a\u503c\uff0c\u8be5\u503c\u6307\u793a\u7ebf\u7a0b\u662f\u5426\u5c5e\u4e8e\u6258\u7ba1\u7ebf\u7a0b\u6c60\r\n             * ManagedThreadId\u83b7\u53d6\u5f53\u524d\u6258\u7ba1\u7ebf\u7a0b\u7684\u552f\u4e00\u6807\u8bc6\u7b26\r\n             * Name\u83b7\u53d6\u6216\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u540d\u79f0\r\n             * Priority\u83b7\u53d6\u6216\u8bbe\u7f6e\u4e00\u4e2a\u503c\uff0c\u8be5\u503c\u6307\u793a\u7ebf\u7a0b\u7684\u8c03\u5ea6\u4f18\u5148\u7ea7\r\n             * ThreadState\u83b7\u53d6\u4e00\u4e2a\u503c\uff0c\u8be5\u503c\u5305\u542b\u5f53\u524d\u7ebf\u7a0b\u7684\u72b6\u6001\r\n             * \r\n             * Thread\u7c7b\u5e38\u7528\u65b9\u6cd5\r\n             * public void Abort()\u5728\u8c03\u7528\u6b64\u65b9\u6cd5\u7684\u7ebf\u7a0b\u4e0a\u5f15\u53d1ThreadAbortException\uff0c\u4ee5\u5f00\u59cb\u7ec8\u6b62\u6b64\u7ebf\u7a0b\u7684\u8fc7\u7a0b\u3002\u8c03\u7528\u6b64\u65b9\u6cd5\u901a\u5e38\u4f1a\u7ec8\u6b62\u7ebf\u7a0b\u3002\r\n             * public static LocalDataStoreSlot AllocateDataSlot()\u5728\u6240\u6709\u7ebf\u7a0b\u4e0a\u5206\u914d\u672a\u547d\u540d\u7684\u6570\u636e\u69fd\u3002\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff0c\u8bf7\u6539\u7528\u4ee5ThreadStaticAttribute\u5c5e\u6027\u6807\u8bb0\u7684\u5b57\u6bb5\u3002\r\n             * public static LocalDataStoreSlot AllocateNamedDataSlot(string name)\u5728\u6240\u6709\u7ebf\u7a0b\u4e0a\u5206\u914d\u5df2\u547d\u540d\u7684\u6570\u636e\u69fd\u3002\u4e3a\u4e86\u66f4\u597d\u7684\u6027\u80fd\uff0c\u8bf7\u6539\u7528\u4ee5ThreadStaticAttribute\u5c5e\u6027\u6807\u8bb0\u7684\u5b57\u6bb5\u3002\r\n             * public static void BeginCriticalRegion()\u901a\u77e5\u4e3b\u673a\u6267\u884c\u5c06\u8981\u8fdb\u5165\u4e00\u4e2a\u4ee3\u7801\u533a\u57df\uff0c\u5728\u8be5\u4ee3\u7801\u533a\u57df\u5185\u7ebf\u7a0b\u7ec8\u6b62\u6216\u672a\u7ecf\u5904\u7406\u7684\u5f02\u5e38\u7684\u5f71\u54cd\u53ef\u80fd\u4f1a\u5371\u5bb3\u5e94\u7528\u7a0b\u5e8f\u57df\u4e2d\u7684\u5176\u4ed6\u4efb\u52a1\u3002\r\n             * public static void BeginThreadAffinity()\u901a\u77e5\u4e3b\u673a\u6258\u7ba1\u4ee3\u7801\u5c06\u8981\u6267\u884c\u4f9d\u8d56\u4e8e\u5f53\u524d\u7269\u7406\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u7684\u6807\u8bc6\u7684\u6307\u4ee4\r\n             * public static void EndCritialRegion()\u901a\u77e5\u4e3b\u673a\u6267\u884c\u5c06\u8981\u8fdb\u5165\u4e00\u4e2a\u4ee3\u7801\u533a\u57df\uff0c\u5728\u8be5\u4ee3\u7801\u533a\u57df\u5185\u7ebf\u7a0b\u7ec8\u6b62\u6216\u672a\u7ecf\u5904\u7406\u7684\u5f02\u5e38\u4ec5\u5f71\u54cd\u5f53\u524d\u4efb\u52a1\r\n             * public static void FreeNamedDataSlot(string name)\u4e3a\u8fdb\u7a0b\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b\u6d88\u9664\u540d\u79f0\u4e0e\u69fd\u4e4b\u95f4\u7684\u5173\u8054\u3002\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff0c\u8bf7\u6539\u7528\u4ee5ThreadStaticAttribute\u5c5e\u6027\u6807\u8bb0\u7684\u5b57\u6bb5\u3002\r\n             * public static Object GetData(LocalDataStoreSlot slot)\u5728\u5f53\u524d\u7ebf\u7a0b\u7684\u5f53\u524d\u57df\u4e2d\u4ece\u5f53\u524d\u7ebf\u7a0b\u4e0a\u6307\u5b9a\u7684\u69fd\u4e2d\u68c0\u7d22\u503c\u3002\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff0c\u8bf7\u6539\u7528\u4ee5ThreadStaticAttribute\u5c5e\u6027\u6807\u8bb0\u7684\u5b57\u6bb5\r\n             * public static AppDomain GetDomain()\u8fd4\u56de\u5f53\u524d\u7ebf\u7a0b\u6b63\u5728\u5176\u4e2d\u8fd0\u884c\u7684\u5f53\u524d\u57df\r\n             * public static AppDomain GetDomainID()\u8fd4\u56de\u552f\u4e00\u7684\u5e94\u7528\u7a0b\u5e8f\u57df\u6807\u8bc6\u7b26\r\n             * public static LocalDataStoreSlot GetNamedDataSlot(string name)\u67e5\u627e\u5df2\u547d\u540d\u7684\u6570\u636e\u69fd\u3002\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff0c\u8bf7\u6539\u7528\u4ee5ThreadStaticAttribute\u5c5e\u6027\u6807\u8bb0\u7684\u5b57\u6bb5\r\n             * public void Interrupt()\u4e2d\u65ad\u5904\u4e8eWaitSleepJoin\u7ebf\u7a0b\u72b6\u6001\u7684\u7ebf\u7a0b\r\n             * public void Join()\u5728\u7ee7\u7eed\u6267\u884c\u6807\u51c6\u7684COM\u548cSendMessage\u6d88\u606f\u6cf5\u5904\u7406\u671f\u95f4\uff0c\u963b\u585e\u8c03\u7528\u7ebf\u7a0b\uff0c\u76f4\u5230\u67d0\u4e2a\u7ebf\u7a0b\u7ec8\u6b62\u4e3a\u6b62\u3002\u6b64\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u91cd\u8f7d\u5f62\u5f0f\u3002\r\n             * public static void MemoryBarrier()\u6309\u5982\u4e0b\u65b9\u5f0f\u540c\u6b65\u5185\u5b58\u5b58\u53d6\uff1a\u6267\u884c\u5f53\u524d\u7ebf\u7a0b\u7684\u5904\u7406\u5668\u5728\u5bf9\u6307\u4ee4\u91cd\u65b0\u6392\u5e8f\u65f6\uff0c\u4e0d\u80fd\u91c7\u7528\u5148\u6267\u884cMemoryBarrier\u8c03\u7528\u4e4b\u540e\u7684\u5185\u5b58\u5b58\u53d6\uff0c\u518d\u6267\u884cMemoryBarrier\u8c03\u7528\u4e4b\u524d\u7684\u5185\u5b58\u5b58\u53d6\u7684\u65b9\u5f0f\u3002\r\n             * public static void ResetAbort()\u53d6\u6d88\u4e3a\u5f53\u524d\u7ebf\u7a0b\u8bf7\u6c42\u7684Abort\r\n             * public static void SetData(LocalDataStoreSlot slot, Object data)\u5728\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u7ebf\u7a0b\u4e0a\u4e3a\u6b64\u7ebf\u7a0b\u7684\u5f53\u524d\u57df\u5728\u6307\u5b9a\u69fd\u4e2d\u8bbe\u7f6e\u6570\u636e\u3002\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\uff0c\u8bf7\u6539\u7528\u4ee5ThreadStaticAttribute\u5c5e\u6027\u6807\u6ce8\u7684\u5b57\u6bb5\u3002\r\n             * public void Start()\u5f00\u59cb\u4e00\u4e2a\u7ebf\u7a0b\r\n             * public static void Sleep(int millisecondsTimeout)\u8ba9\u7ebf\u7a0b\u6682\u505c\u4e00\u6bb5\u4e8b\u4ef6\r\n             * public static void SpinWait(int iterations)\u5bfc\u81f4\u7ebf\u7a0b\u7b49\u5f85\u7531iterations\u53c2\u6570\u5b9a\u4e49\u7684\u65f6\u95f4\u91cf\r\n             * public static byte VolatileRead(ref byte address)\r\n             * public static double VolatileRead(ref double address)\r\n             * public static int VolatileRead(ref int address)\r\n             * public static Object VolatileRead(ref Object address)\r\n             * \u8bfb\u53d6\u5b57\u6bb5\u503c\u3002\u65e0\u8bba\u5904\u7406\u5668\u7684\u6570\u76ee\u6216\u5904\u7406\u5668\u7f13\u5b58\u7684\u72b6\u6001\u5982\u4f55\uff0c\u8be5\u503c\u90fd\u662f\u7531\u8ba1\u7b97\u673a\u7684\u4efb\u4f55\u5904\u7406\u5668\u5199\u5165\u7684\u6700\u65b0\u503c\u3002\u6b64\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u91cd\u8f7d\u5f62\u5f0f\u3002\u8fd9\u91cc\u53ea\u7ed9\u51fa\u4e86\u4e00\u4e9b\u5f62\u5f0f\r\n             * public static void VolatileWrite(ref byte address, byte value)\r\n             * public static void VolatileWrite(ref double address, double value)\r\n             * public static void VolatileWrite(ref int address, int value)\r\n             * public static void VolatileWrite(ref Object address, Object value)\r\n             * \u7acb\u5373\u5411\u5b57\u6bb5\u5199\u5165\u4e00\u4e2a\u503c\uff0c\u4ee5\u4f7f\u8be5\u503c\u5bf9\u8ba1\u7b97\u673a\u4e2d\u7684\u6240\u6709\u5904\u7406\u5668\u90fd\u53ef\u89c1\u3002\u6b64\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u91cd\u8f7d\u5f62\u5f0f\u3002\u8fd9\u91cc\u53ea\u5217\u51fa\u4e00\u4e9b\r\n             * public static bool Yield()\u5bfc\u81f4\u8c03\u7528\u7ebf\u7a0b\u6267\u884c\u51c6\u5907\u597d\u5728\u5f53\u524d\u5904\u7406\u5668\u4e0a\u8fd0\u884c\u7684\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u3002\u7531\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u8981\u6267\u884c\u7684\u7ebf\u7a0b\r\n             *\/\r\n\r\n            \/\/\u521b\u5efa\u7ebf\u7a0b\r\n            ThreadStart childref = new ThreadStart(CallToChildThread);\r\n            Console.WriteLine(\"In Main: Creating the Child thread\");\r\n            Thread childThread = new Thread(childref);\r\n            childThread.Start();\r\n\r\n            \/\/\u7ba1\u7406\u7ebf\u7a0b\r\n            \/*\r\n             * Thread\u7c7b\u63d0\u4f9b\u4e86\u5404\u79cd\u7ba1\u7406\u7ebf\u7a0b\u7684\u65b9\u6cd5\r\n             * \u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86sleep()\u65b9\u6cd5\u7684\u4f7f\u7528\uff0c\u7528\u4e8e\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u65f6\u95f4\u6682\u505c\u7ebf\u7a0b\r\n             *\/\r\n            ThreadStart childref2 = new ThreadStart(CallToChildThread2);\r\n            Console.WriteLine(\"In Main: Creating the Child thread\");\r\n            Thread childThread2 = new Thread(childref2);\r\n            childThread2.Start();\r\n\r\n            \/\/\u9500\u6bc1\u7ebf\u7a0b\r\n            \/*\r\n             * Abort()\u65b9\u6cd5\u7528\u4e8e\u9500\u6bc1\u7ebf\u7a0b\r\n             * \u901a\u8fc7\u629b\u51fathreadabortException\u5728\u8fd0\u884c\u65f6\u4e2d\u6b62\u7ebf\u7a0b\u3002\u8fd9\u4e2a\u5f02\u5e38\u4e0d\u80fd\u88ab\u6355\u83b7\uff0c\u5982\u679c\u6709finally\u5757\uff0c\u63a7\u5236\u4f1a\u88ab\u9001\u81f3finally\u5757\u3002\r\n             *\/\r\n            ThreadStart childref3 = new ThreadStart(CallToChildThread3);\r\n            Console.WriteLine(\"In Main: Creating the Child thread\");\r\n            Thread childThread3 = new Thread(childref3);\r\n            childThread3.Start();\r\n            \/\/\u505c\u6b62\u4e3b\u7ebf\u7a0b\u4e00\u6bb5\u65f6\u95f4\r\n            Thread.Sleep(2000);\r\n            \/\/\u73b0\u5728\u4e2d\u6b62\u5b50\u7ebf\u7a0b\r\n            Console.WriteLine(\"In Main: Aborting the Child thread\");\r\n            childThread3.Abort();\r\n\r\n            Console.ReadKey();\r\n        }\r\n\r\n        public static void CallToChildThread3()\r\n        {\r\n            try\r\n            {\r\n                Console.WriteLine(\"child thread starts\");\r\n                \/\/\u8ba1\u6570\u523010\r\n                for(int counter = 0;counter &lt;= 10;counter++)\r\n                {\r\n                    Thread.Sleep(500);\r\n                    Console.WriteLine(counter);\r\n                }\r\n                Console.WriteLine(\"child thread completed\");\r\n            }catch(ThreadAbortException e)\r\n            {\r\n                Console.WriteLine(\"Thread Abort Exception\");\r\n            }finally\r\n            {\r\n                Console.WriteLine(\"could not catch the thread exception\");\r\n            }\r\n        }\r\n        public static void CallToChildThread2()\r\n        {\r\n            Console.WriteLine(\"Child thread starts\");\r\n            \/\/\u7ebf\u7a0b\u6682\u505c5000ms\r\n            int sleepfor = 5000;\r\n            Console.WriteLine(\"Child Thread Paused for {0} seconds\", sleepfor \/ 1000);\r\n            Thread.Sleep(sleepfor);\r\n            Console.WriteLine(\"Child thread resumes\");\r\n        }\r\n        public static void CallToChildThread()\r\n        {\r\n            Console.WriteLine(\"Child thread starts\");\r\n        }\r\n    }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[24],"_links":{"self":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/304"}],"collection":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":1,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}