{"id":313,"date":"2014-11-08T15:27:11","date_gmt":"2014-11-08T07:27:11","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=313"},"modified":"2016-07-14T22:55:43","modified_gmt":"2016-07-14T14:55:43","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%8a%80%e6%9c%afgcd%e7%9a%848%e7%a7%8d%e9%98%9f%e5%88%97%e8%af%b7%e6%b1%82%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/11\/08\/152711313.html","title":{"rendered":"\u591a\u7ebf\u7a0b\u6280\u672fGCD\u76848\u79cd\u961f\u5217\u8bf7\u6c42\u793a\u4f8b"},"content":{"rendered":"<p>1\u3001\u4e32\u884c\u961f\u5217\u540c\u6b65\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">#pragma mark 1\u3001\u4e32\u884c\u961f\u5217\u540c\u6b65\u8bf7\u6c42\n- (void)gcd1\n{\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u4e32\u884c\u961f\u5217dispatch_queue_create(\u6807\u8bc6, \u4e32\u884c\u961f\u5217)\uff0cDISPATCH_QUEUE_SERIAL \u8868\u793a\u4e32\u884c\u961f\u5217\n    dispatch_queue_t queue = dispatch_queue_create(\"gcd1\", DISPATCH_QUEUE_SERIAL);\n    \n    \n    \/\/ \u53d1\u8d77\u4e00\u4e2a\u540c\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_sync(dispatch_queue_t queue, ^{ code })\u662f\u540c\u6b65\u6267\u884c\u51fd\u6570\n    for (int i = 0; i < 10; i++) {\n        \n        dispatch_sync(queue, ^{\n            \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        });\n    }\n    \/\/ \u7ed3\u8bba\uff1a\u4e32\u884c\u4efb\u52a1\u6309\u987a\u5e8f\u6267\u884c\uff0c\u540c\u6b65\u4efb\u52a1\u6309\u987a\u5e8f\u6267\u884c\uff0c\u65e0\u5e76\u53d1\u6570\uff0c\u5f53\u524d\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u56e0\u6b64\u5728\u7ebf\u7a0b1\u6309\u987a\u5e8f\u6267\u884c\n}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<!--more--><br \/>\n2014-11-08 15:12:37.952 Test[3166:607] \u7b2c 0 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.955 Test[3166:607] \u7b2c 1 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.957 Test[3166:607] \u7b2c 2 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.958 Test[3166:607] \u7b2c 3 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.959 Test[3166:607] \u7b2c 4 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.959 Test[3166:607] \u7b2c 5 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.960 Test[3166:607] \u7b2c 6 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.961 Test[3166:607] \u7b2c 7 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.965 Test[3166:607] \u7b2c 8 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<br \/>\n2014-11-08 15:12:37.966 Test[3166:607] \u7b2c 9 \u6b21 - <NSThread: 0x8c02f70>{name = (null), num = 1}<\/p>\n<p>2\u3001\u4e32\u884c\u961f\u5217\u5f02\u6b65\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">#pragma mark 2\u3001\u4e32\u884c\u961f\u5217\u5f02\u6b65\u8bf7\u6c42\n- (void)gcd2\n{\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u4e32\u884c\u961f\u5217dispatch_queue_create(\u6807\u8bc6, \u4e32\u884c\u961f\u5217)\uff0cDISPATCH_QUEUE_SERIAL \u8868\u793a\u4e32\u884c\u961f\u5217\n    dispatch_queue_t queue = dispatch_queue_create(\"gcd2\", DISPATCH_QUEUE_SERIAL);\n    \n    \/\/ \u53d1\u8d77\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_async(dispatch_queue_t queue, ^{ code })\u662f\u5f02\u6b65\u6267\u884c\u51fd\u6570\n    for (int i = 0; i < 10; i++) {\n        \n        dispatch_async(queue, ^{\n            \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        });\n    }\n    \/\/ \u7ed3\u8bba\uff1a\u5f02\u6b65\u4efb\u52a1\u53ef\u591a\u4e2a\u540c\u65f6\u8fdb\u884c\uff0c\u65e0\u5148\u540e\u987a\u5e8f\uff0c\u4e14\u53e6\u5f00\u7ebf\u7a0b\u5e76\u53d1\u8fdb\u884c\uff0c\u4f46\u7531\u4e8e\u662f\u4e32\u884c\u961f\u5217\uff0c\u53ea\u80fd\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u5b8c\u6210\uff0c\u56e0\u6b64\u5728\u7ebf\u7a0b2\u6309\u987a\u5e8f\u6267\u884c\n}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n2014-11-08 15:14:35.006 Test[3207:1303] \u7b2c 0 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.008 Test[3207:1303] \u7b2c 1 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.011 Test[3207:1303] \u7b2c 2 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.012 Test[3207:1303] \u7b2c 3 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.023 Test[3207:1303] \u7b2c 4 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.024 Test[3207:1303] \u7b2c 5 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.025 Test[3207:1303] \u7b2c 6 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.026 Test[3207:1303] \u7b2c 7 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.028 Test[3207:1303] \u7b2c 8 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<br \/>\n2014-11-08 15:14:35.032 Test[3207:1303] \u7b2c 9 \u6b21 - <NSThread: 0x8c55cc0>{name = (null), num = 2}<\/p>\n<p>3\u3001\u5e76\u884c\u961f\u5217\u5f02\u6b65\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">#pragma mark 3\u3001\u5e76\u884c\u961f\u5217\u540c\u6b65\u8bf7\u6c42\n- (void)gcd3\n{\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u5e76\u884c\u961f\u5217dispatch_queue_create(\u6807\u8bc6, \u4e32\u884c\u961f\u5217)\uff0cDISPATCH_QUEUE_CONCURRENT \u8868\u793a\u5e76\u884c\u961f\u5217\n    dispatch_queue_t queue = dispatch_queue_create(\"gcd3\", DISPATCH_QUEUE_CONCURRENT);\n    \n    \/\/ \u53d1\u8d77\u4e00\u4e2a\u540c\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_sync(dispatch_queue_t queue, ^{ code })\u662f\u540c\u6b65\u6267\u884c\u51fd\u6570\n    for (int i = 0; i < 10; i++) {\n        \n        dispatch_sync(queue, ^{\n            \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        });\n    }\n    \/\/ \u7ed3\u8bba\uff1a\u7531\u4e8e\u540c\u6b65\u4efb\u52a1\uff0c\u56e0\u6b64\u53ea\u80fd\u6309\u987a\u5e8f\u6267\u884c\uff0c\u7531\u4e8e\u662f\u5e76\u884c\u961f\u5217\uff0c\u539f\u5219\u4e0a\u53ef\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u4f46\u540c\u6b65\u4efb\u52a1\u8ba9\u5176\u65e0\u6cd5\u53d1\u6325\u4f5c\u7528\n}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n2014-11-08 15:15:47.285 Test[3241:607] \u7b2c 0 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.287 Test[3241:607] \u7b2c 1 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.288 Test[3241:607] \u7b2c 2 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.289 Test[3241:607] \u7b2c 3 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.290 Test[3241:607] \u7b2c 4 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.292 Test[3241:607] \u7b2c 5 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.292 Test[3241:607] \u7b2c 6 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.293 Test[3241:607] \u7b2c 7 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.294 Test[3241:607] \u7b2c 8 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<br \/>\n2014-11-08 15:15:47.295 Test[3241:607] \u7b2c 9 \u6b21 - <NSThread: 0x8c0ebd0>{name = (null), num = 1}<\/p>\n<p>4\u3001\u5e76\u884c\u961f\u5217\u5f02\u6b65\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">#pragma mark 4\u3001\u5e76\u884c\u961f\u5217\u5f02\u6b65\u8bf7\u6c42\n- (void)gcd4\n{\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u5e76\u884c\u961f\u5217dispatch_queue_create(\u6807\u8bc6, \u4e32\u884c\u961f\u5217)\uff0cDISPATCH_QUEUE_CONCURRENT \u8868\u793a\u5e76\u884c\u961f\u5217\n     dispatch_queue_t queue = dispatch_queue_create(\"gcd4\", DISPATCH_QUEUE_CONCURRENT);\n    \n    \/\/ \u53d1\u8d77\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_async(dispatch_queue_t queue, ^{ code })\u662f\u5f02\u6b65\u6267\u884c\u51fd\u6570\n    for (int i = 0; i < 10; i++) {\n        \n        dispatch_async(queue, ^{\n            \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        });\n    }\n    \/\/ \u7ed3\u8bba\uff1a\u7531\u4e8e\u662f\u5f02\u6b65\u961f\u5217\uff0cfor\u5faa\u73af\u4e2d\u6ca1\u7b49\u5355\u6b21\u5faa\u73af\u7ed3\u675f\u76f4\u63a5\u65b0\u5f00\u7ebf\u7a0b\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\uff0c\u56e0\u6b64\u7ebf\u7a0b\u6570\u4ee5\u53ca\u5728\u54ea\u4e2a\u7ebf\u7a0b\u4e2d\u5b8c\u6210\u968f\u673a\uff0c\u7a0b\u5e8f\u5458\u4e0d\u53ef\u63a7\n}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n2014-11-08 15:17:04.004 Test[3273:3b03] \u7b2c 9 \u6b21 - <NSThread: 0x8d30120>{name = (null), num = 11}<br \/>\n2014-11-08 15:17:04.001 Test[3273:3703] \u7b2c 5 \u6b21 - <NSThread: 0x8c06f70>{name = (null), num = 7}<br \/>\n2014-11-08 15:17:04.001 Test[3273:3107] \u7b2c 1 \u6b21 - <NSThread: 0x8d2f2a0>{name = (null), num = 6}<br \/>\n2014-11-08 15:17:04.002 Test[3273:3603] \u7b2c 4 \u6b21 - <NSThread: 0x8d2ea20>{name = (null), num = 5}<br \/>\n2014-11-08 15:17:04.002 Test[3273:3803] \u7b2c 6 \u6b21 - <NSThread: 0x8d2f250>{name = (null), num = 8}<br \/>\n2014-11-08 15:17:04.001 Test[3273:3403] \u7b2c 2 \u6b21 - <NSThread: 0x8d2e6b0>{name = (null), num = 3}<br \/>\n2014-11-08 15:17:04.003 Test[3273:3a03] \u7b2c 8 \u6b21 - <NSThread: 0x8d2fd50>{name = (null), num = 10}<br \/>\n2014-11-08 15:17:04.006 Test[3273:3503] \u7b2c 3 \u6b21 - <NSThread: 0x8d2e880>{name = (null), num = 4}<br \/>\n2014-11-08 15:17:04.001 Test[3273:1303] \u7b2c 0 \u6b21 - <NSThread: 0x8c06610>{name = (null), num = 2}<br \/>\n2014-11-08 15:17:04.002 Test[3273:3903] \u7b2c 7 \u6b21 - <NSThread: 0x8d2f7b0>{name = (null), num = 9}<\/p>\n<p>5\u3001\u5168\u5c40\u961f\u5217\u5f02\u6b65\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">#pragma mark 5\u3001\u5168\u5c40\u961f\u5217\u540c\u6b65\u8bf7\u6c42\n- (void)gcd5\n{\n    \/\/ \u83b7\u53d6\u5168\u5c40\u961f\u5217 dispatch_get_global_queue(\u961f\u5217\u4f18\u5148\u7ea7, \u9884\u7559\u6807\u8bb0)\n    \/\/ \u67094\u79cd\u4f18\u5148\u7ea7\uff0cDISPATCH_QUEUE_PRIORITY_DEFAULT\u4e3a\u9ed8\u8ba4\u4f18\u5148\u7ea7\uff0c\u9884\u7559\u6807\u8bb0\u6c38\u8fdc\u4e3a0\uff0c\u5f53\u524d\u65e0\u5177\u4f53\u542b\u4e49\n    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);\n    \n    \/\/ \u53d1\u8d77\u4e00\u4e2a\u540c\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_sync(dispatch_queue_t queue, ^{ code })\u662f\u540c\u6b65\u6267\u884c\u51fd\u6570\n    for (int i = 0; i < 10; i++) {\n        \n        dispatch_sync(queue, ^{\n            \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        });\n    }\n    \/\/ \u7ed3\u8bba\uff1a\u5168\u5c40\u961f\u5217\u4e0e\u81ea\u5b9a\u4e49\u961f\u5217\u7c7b\u4f3c\uff0c\u4e0d\u540c\u7684\u662f\u5168\u5c40\u961f\u5217\u9ed8\u8ba4\u662f\u5e76\u53d1\u961f\u5217\uff0c\u4f46\u540c\u6b65\u8bf7\u6c42\u4f7f\u5176\u53ea\u80fd\u5355\u7ebf\u7a0b\u6267\u884c\n}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n2014-11-08 15:18:18.991 Test[3310:607] \u7b2c 0 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.993 Test[3310:607] \u7b2c 1 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.994 Test[3310:607] \u7b2c 2 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.994 Test[3310:607] \u7b2c 3 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.994 Test[3310:607] \u7b2c 4 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.995 Test[3310:607] \u7b2c 5 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.995 Test[3310:607] \u7b2c 6 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.996 Test[3310:607] \u7b2c 7 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.996 Test[3310:607] \u7b2c 8 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<br \/>\n2014-11-08 15:18:18.997 Test[3310:607] \u7b2c 9 \u6b21 - <NSThread: 0x8e08d70>{name = (null), num = 1}<\/p>\n<p>6\u3001\u5168\u5c40\u961f\u5217\u5f02\u6b65\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">#pragma mark 6\u3001\u5168\u5c40\u961f\u5217\u5f02\u6b65\u8bf7\u6c42\n- (void)gcd6\n{\n    \/\/ \u83b7\u53d6\u5168\u5c40\u961f\u5217 dispatch_get_global_queue(\u961f\u5217\u4f18\u5148\u7ea7, \u9884\u7559\u6807\u8bb0)\n    \/\/ \u67094\u79cd\u4f18\u5148\u7ea7\uff0cDISPATCH_QUEUE_PRIORITY_DEFAULT\u4e3a\u9ed8\u8ba4\u4f18\u5148\u7ea7\uff0c\u9884\u7559\u6807\u8bb0\u6c38\u8fdc\u4e3a0\uff0c\u5f53\u524d\u65e0\u5177\u4f53\u542b\u4e49\n    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);\n    \n    \/\/ \u53d1\u8d77\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_async(dispatch_queue_t queue, ^{ code })\u662f\u5f02\u6b65\u6267\u884c\u51fd\u6570\n    for (int i = 0; i < 10; i++) {\n        \n        dispatch_async(queue, ^{\n            \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        });\n    }\n    \/\/ \u7ed3\u8bba\uff1a\u5168\u5c40\u961f\u5217\u5f02\u6b65\u8bf7\u6c42\u4e0e\u5e76\u884c\u961f\u5217\u5f02\u6b65\u8bf7\u6c42\u7c7b\u4f3c\uff0c\u8bf4\u660e\u5168\u5c40\u961f\u5217\u662f\u5e76\u884c\u961f\u5217\u3002\n}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n2014-11-08 15:19:30.332 Test[3337:3503] \u7b2c 3 \u6b21 - <NSThread: 0x8e3eab0>{name = (null), num = 5}<br \/>\n2014-11-08 15:19:30.332 Test[3337:1303] \u7b2c 0 \u6b21 - <NSThread: 0x8e3f8d0>{name = (null), num = 2}<br \/>\n2014-11-08 15:19:30.332 Test[3337:3803] \u7b2c 6 \u6b21 - <NSThread: 0x8f19530>{name = (null), num = 8}<br \/>\n2014-11-08 15:19:30.332 Test[3337:3107] \u7b2c 1 \u6b21 - <NSThread: 0x8f195b0>{name = (null), num = 3}<br \/>\n2014-11-08 15:19:30.332 Test[3337:3403] \u7b2c 2 \u6b21 - <NSThread: 0x8c3a140>{name = (null), num = 4}<br \/>\n2014-11-08 15:19:30.332 Test[3337:3703] \u7b2c 5 \u6b21 - <NSThread: 0x8c3b1c0>{name = (null), num = 7}<br \/>\n2014-11-08 15:19:30.332 Test[3337:3603] \u7b2c 4 \u6b21 - <NSThread: 0x8c3a260>{name = (null), num = 6}<br \/>\n2014-11-08 15:19:30.334 Test[3337:3903] \u7b2c 7 \u6b21 - <NSThread: 0x8f19bf0>{name = (null), num = 9}<br \/>\n2014-11-08 15:19:30.334 Test[3337:3c03] \u7b2c 8 \u6b21 - <NSThread: 0x8f1a500>{name = (null), num = 10}<br \/>\n2014-11-08 15:19:30.335 Test[3337:3d03] \u7b2c 9 \u6b21 - <NSThread: 0x8f1a7c0>{name = (null), num = 11}<\/p>\n<p>7\u3001\u4e3b\u961f\u5217\u5f02\u6b65\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">#pragma mark 7\u3001\u4e3b\u961f\u5217\u540c\u6b65\u8bf7\u6c42\n- (void)gcd7\n{\n    \/\/ \u83b7\u53d6\u4e3b\u961f\u5217 dispatch_get_main_queue()\n    dispatch_queue_t queue = dispatch_get_main_queue();\n    \n    NSLog(@\"\u4efb\u52a1\u5f00\u59cb\");\n    \/\/ \u53d1\u8d77\u4e00\u4e2a\u540c\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_sync(dispatch_queue_t queue, ^{ code })\u662f\u540c\u6b65\u6267\u884c\u51fd\u6570\n    for (int i = 0; i < 10; i++) {\n        \n        dispatch_sync(queue, ^{\n            \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        });\n    }\n    NSLog(@\"\u4efb\u52a1\u7ed3\u675f\");\n    \n    \/\/ \u7ed3\u8bba\uff1a\u4e3b\u961f\u5217\u540c\u6b65\u8bf7\u6c42\u4e0d\u4f1a\u8fdb\u5165\u5faa\u73af\u4f53\uff0c\u56e0\u4e3a\u5e94\u7528\u7a0b\u5e8f\u5728\u4e3b\u961f\u5217\u4e2d\u5f00\u542f\u4e86\u6d88\u606f\u5faa\u73af\uff0c\u8be5\u4efb\u52a1\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4e0d\u4f1a\u7ed3\u675f\uff0c\u56e0\u6b64\u5f53\u524d\u4efb\u52a1\u672a\u5b8c\u6210\uff0c\u4e0b\u4e00\u4e2a\u4efb\u52a1\u6c38\u8fdc\u4e0d\u4f1a\u5f00\u59cb\u3002\n}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n2014-11-08 15:20:50.754 Test[3372:607] \u4efb\u52a1\u5f00\u59cb<\/p>\n<p>8\u3001\u4e3b\u961f\u5217\u5f02\u6b65\u8bf7\u6c42<\/p>\n<pre lang=\"objc\" line=\"1\">#pragma mark 8\u3001\u4e3b\u961f\u5217\u5f02\u6b65\u8bf7\u6c42\n- (void)gcd8\n{\n    \/\/ \u83b7\u53d6\u4e3b\u961f\u5217 dispatch_get_main_queue()\n    dispatch_queue_t queue = dispatch_get_main_queue();\n    \n    \/\/ \u53d1\u8d77\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_async(dispatch_queue_t queue, ^{ code })\u662f\u5f02\u6b65\u6267\u884c\u51fd\u6570\n    for (int i = 0; i < 10; i++) {\n        \n        dispatch_async(queue, ^{\n            \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b\n            NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]);\n        });\n    }\n    \/\/ \u7ed3\u8bba\uff1a\u4e3b\u961f\u5217\u4e2d\u53d1\u8d77\u4e00\u4e2a\u5f02\u6b65\u8bf7\u6c42\uff0c\u53ea\u5728\u7ebf\u7a0b1\u4e2d\u6267\u884c\uff0c\u56e0\u4e3a\u4e3b\u961f\u5217\u662f\u4e2a\u4e32\u884c\u961f\u5217\n}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n2014-11-08 15:23:08.199 Test[3425:607] \u7b2c 0 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.203 Test[3425:607] \u7b2c 1 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.204 Test[3425:607] \u7b2c 2 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.205 Test[3425:607] \u7b2c 3 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.205 Test[3425:607] \u7b2c 4 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.206 Test[3425:607] \u7b2c 5 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.207 Test[3425:607] \u7b2c 6 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.210 Test[3425:607] \u7b2c 7 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.210 Test[3425:607] \u7b2c 8 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<br \/>\n2014-11-08 15:23:08.211 Test[3425:607] \u7b2c 9 \u6b21 - <NSThread: 0x8e0ba40>{name = (null), num = 1}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4e32\u884c\u961f\u5217\u540c\u6b65\u8bf7\u6c42 #pragma mark 1\u3001\u4e32\u884c\u961f\u5217\u540c\u6b65\u8bf7\u6c42 &#8211; (void)gcd1 { \/\/ \u521b\u5efa\u4e00\u4e2a\u4e32\u884c\u961f\u5217dispatch_queue_create(\u6807\u8bc6, \u4e32\u884c\u961f\u5217)\uff0cDISPATCH_QUEUE_SERIAL \u8868\u793a\u4e32\u884c\u961f\u5217 dispatch_queue_t queue = dispatch_queue_create(&#8220;gcd1&#8221;, DISPATCH_QUEUE_SERIAL); \/\/ \u53d1\u8d77\u4e00\u4e2a\u540c\u6b65\u4efb\u52a1\u5e76\u6267\u884c10\u6b21\uff1adispatch_sync(dispatch_queue_t queue, ^{ code })\u662f\u540c\u6b65\u6267\u884c\u51fd\u6570 for (int i = 0; i < 10; i++) { dispatch_sync(queue, ^{ \/\/ \u6253\u5370\u5f53\u524d\u7ebf\u7a0b NSLog(@\"\u7b2c %d \u6b21 - %@\", i, [NSThread currentThread]); }); } \/\/ \u7ed3\u8bba\uff1a\u4e32\u884c\u4efb\u52a1\u6309\u987a\u5e8f\u6267\u884c\uff0c\u540c\u6b65\u4efb\u52a1\u6309\u987a\u5e8f\u6267\u884c\uff0c\u65e0\u5e76\u53d1\u6570\uff0c\u5f53\u524d\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u56e0\u6b64\u5728\u7ebf\u7a0b1\u6309\u987a\u5e8f\u6267\u884c } \u8fd0\u884c\u7ed3\u679c\uff1a\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[17],"class_list":["post-313","post","type-post","status-publish","format-standard","hentry","category-project","tag-skill"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}