.NET \u4e2d\u7684\u6b7b\u9501<\/strong><\/div>\n\u901a\u5e38\u7684\u6b7b\u9501\u7684\u793a\u4f8b\u90fd\u662f\u4e24\u4e2a\u9501\uff0c\u591a\u4e2a\u8d44\u6e90\u5bfc\u81f4\u7684\u6b7b\u9501\uff0c\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7\u4e00\u4e2a\u8d44\u6e90\u4e5f\u4f1a\u5bfc\u81f4\u6b7b\u9501\uff0c\u5982\u4f55\u4f7f\u7528\u4e00\u4e2a\u9501\u9020\u6210\u6b7b\u9501\u5462\uff1f\u601d\u8003\u4e00\u4e0b\u518d\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n
\r\nprivate static readonly object Lock = new object();\r\n\r\npublic static void Test()\r\n{\r\n lock (Lock)\r\n {\r\n Task.Run(TestMethod1).Wait();\r\n }\r\n}\r\n\r\nprivate static void TestMethod1()\r\n{\r\n lock (Lock)\r\n {\r\n Console.WriteLine(\"xxx\");\r\n }\r\n}\r\n<\/pre>\n\u5728 Test \u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u9996\u5148\u83b7\u53d6\u9501\uff0c\u83b7\u53d6\u9501\u6210\u529f\u4e4b\u540e\u8c03\u7528\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u8c03\u7528 TestMethod1 \u65b9\u6cd5\uff0c\u800c TestMethod1 \u65b9\u6cd5\u4e2d\u4f1a\u518d\u6b21\u5c1d\u8bd5\u83b7\u53d6\u9501\uff0c\u6b64\u65f6\u56e0\u4e3a\u9501\u5df2\u7ecf\u88ab Test \u65b9\u6cd5\u83b7\u53d6\u800c\u4e14\u5e76\u6ca1\u6709\u91ca\u653e\uff0c\u6240\u4ee5\u4f1a\u4e00\u76f4\u83b7\u53d6\u4e0d\u5230\u9501\u4ece\u800c\u9020\u6210\u6b7b\u9501\u3002<\/p>\n
\u5176\u5b9e\u8fd9\u79cd\u60c5\u51b5\u8fd8\u6709\u5f88\u591a\u53d8\u5f62\uff0c\u6bd4\u5982\u8bf4 lock(this)\/lock(\"lockedString\") \u8fd9\u79cd\u90fd\u662f\u6bd4\u8f83\u5371\u9669\u7684\uff0c\u6240\u4ee5\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u6211\u4eec\u4f7f\u7528\u4e0a\u9762\u7684\u793a\u4f8b\u505a\u4e00\u4e2a\u53d8\u5f62\uff0c\u4f7f\u7528 lock(\"lockedString\") \u6765\u6d4b\u8bd5\u4e00\u4e0b\u3002<\/p>\n
\r\npublic static void Test()\r\n{\r\n lock (\"Lock\")\r\n {\r\n Task.Run(TestMethod1).Wait();\r\n }\r\n}\r\n\r\nprivate static void TestMethod1()\r\n{\r\n lock (\"Lock\")\r\n {\r\n Console.WriteLine(\"xxx\");\r\n }\r\n}\r\n<\/pre>\n\u8fd9\u6837\u4e5f\u4f1a\u9020\u6210\u6b7b\u9501\uff0c\u56e0\u4e3a lock \u7684 string \u5b9e\u9645\u4e0a\u662f\u540c\u4e00\u4e2a\u5f15\u7528\uff0c\u5b57\u7b26\u4e32\u6c60\uff08string intern\uff09\uff0c\u6240\u4ee5\u7c7b\u4f3c\u4e8e\u4e0a\u9762\u7684\u793a\u4f8b\uff0c\u76f8\u5f53\u4e8e\u662f\u4e00\u4e2a\u9501\uff0c\u5bf9\u4e8e lock(this) \u4e5f\u662f\u7c7b\u4f3c\u7684\uff0c\u6240\u4ee5\u901a\u5e38 lock \u662f\u4e0d\u63a8\u8350 lock(this)\/lock(\"string\") \u8fd9\u4e9b\u5199\u6cd5\u7684\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u8d44\u6e90\u8981\u4f7f\u7528\u4e0d\u540c\u7684 lock\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u4e0a\u9762\u8fd9\u4e2a\u793a\u4f8b\u7684\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n
More<\/strong><\/div>\n\u5728 SQL Server \u4e2d\u4f1a\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u6b7b\u9501\u68c0\u6d4b\u7684\u8fdb\u7a0b\uff0c\u5982\u679c\u53d1\u751f\u6b7b\u9501\u7684\u60c5\u51b5\uff0c\u4f1a\u6709\u4e00\u4e2a\u4e8b\u52a1\u4f1a\u88ab\u9009\u62e9\u4e3a\u727a\u7272\u54c1\u6765\u89e3\u51b3\u6b7b\u9501\u7684\u95ee\u9898\u3002<\/p>\n
\u5728\u901a\u8fc7 Redis \u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u65f6\u5019\uff0c\u901a\u5e38\u4f1a\u6307\u5b9a\u4e00\u4e2a\u9501\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u8fc7\u671f\u65f6\u95f4\u901a\u5e38\u662f\u4e3a\u4e86\u907f\u514d\u83b7\u53d6\u9501\u6210\u529f\u7684\u7cfb\u7edf\u7a81\u7136\u5b95\u673a\u5bfc\u81f4\u9501\u4e00\u76f4\u5728\u9501\u5b9a\u72b6\u6001\uff0c\u4ece\u800c\u5bfc\u81f4\u5176\u4ed6\u670d\u52a1\u83b7\u53d6\u9501\u7684\u65f6\u5019\u4e00\u76f4\u83b7\u53d6\u5931\u8d25\uff0c\u9664\u6b64\u4e4b\u5916\uff0c\u901a\u5e38\u8fd8\u4f1a\u6307\u5b9a\u4e00\u4e2a\u6700\u5927\u7b49\u5f85\u65f6\u95f4\uff0c\u5982\u679c\u522b\u7684\u670d\u52a1\u83b7\u53d6\u5230\u9501\u4e86\uff0c\u6b63\u5728\u64cd\u4f5c\uff0c\u90a3\u4e48\u4f1a\u7b49\u5f85\u9501\u91ca\u653e\uff0c\u4f46\u662f\u4e3a\u4e86\u907f\u514d\u6b7b\u9501\uff0c\u5982\u679c\u957f\u65f6\u95f4\u83b7\u53d6\u4e0d\u5230\u9501\u7684\u8bdd\u5c31\u4f1a\u653e\u5f03\u83b7\u53d6\u9501\uff0c\u76f4\u63a5\u8fd4\u56de\u83b7\u53d6\u9501\u5931\u8d25\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u901a\u5e38\u7684\u6b7b\u9501\u7684\u793a\u4f8b\u90fd\u662f\u4e24\u4e2a\u9501\uff0c\u591a\u4e2a\u8d44\u6e90\u5bfc\u81f4\u7684\u6b7b\u9501\uff0c\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7\u4e00\u4e2a\u8d44\u6e90\u4e5f\u4f1a\u5bfc\u81f4\u6b7b\u9501\uff0c\u5982\u4f55\u4f7f\u7528\u4e00\u4e2a\u9501\u9020\u6210\u6b7b\u9501\u5462\uff1f\u601d […]<\/p>\n","protected":false},"author":1903,"featured_media":206165,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-206132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=206132"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206132\/revisions"}],"predecessor-version":[{"id":206164,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206132\/revisions\/206164"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/206165"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=206132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=206132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=206132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}