前幾天,同事多線程訪問sqlite的時候遇到一個問題癌佩,我也跟著看了一下木缝,這幾天又剛好看了一些相關(guān)的博文,發(fā)現(xiàn)一個問題围辙,所以在此記錄一下我碟。
先展示代碼如下:
dispatch_queue_t q = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
for (NSUInteger i = 0; i < 10; i++) {
}
以下是日志打印
2019-08-30 16:34:39.999006+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 0
2019-08-30 16:34:39.999211+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 1
2019-08-30 16:34:39.999977+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 2
2019-08-30 16:34:40.000368+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 3
2019-08-30 16:34:40.001670+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 4
2019-08-30 16:34:40.001950+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 5
2019-08-30 16:34:40.002119+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 6
2019-08-30 16:34:40.002254+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 7
2019-08-30 16:34:40.002762+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 8
2019-08-30 16:34:40.002905+0800 odinDomain[57322:1491164] current thread = <NSThread: 0x600000464a80>{number = 5, name = (null)}, index = 9
代碼是在主線程執(zhí)行的,從日志可以看出姚建,串行隊列異步執(zhí)行任務(wù)矫俺,會新建一個線程,然后串行隊列的任務(wù)會在新建的線程里面掸冤,按照加入的順序執(zhí)行厘托,這與我們預(yù)期的效果是一樣的。
現(xiàn)在將代碼改動 如下
dispatch_queue_t q = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
for (NSUInteger i = 0; i < 10; i++) {
NSLog(@"start request index = %@", @(i));
[ODMHttp post:@"http://192.168.124.11:5000/odindomain" parameters:@{@"index": @(i)} success:^(id _Nullable responseObject) {
NSLog(@"callback thread = %@, index = %@", NSThread.currentThread, @(i));
dispatch_async(q, ^{
NSLog(@"async thread = %@, index = %@", NSThread.currentThread, @(i));
});
} failure:^(NSError * _Nullable error) {}];
}
打印的日志如下:
17:14:37.7034 callback thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 1
17:14:37.7046 async thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 1
17:14:37.7108 callback thread = <NSThread: 0x60000027cf00>{number = 5, name = (null)}, index = 2
17:14:37.7116 callback thread = <NSThread: 0x60000027cf00>{number = 5, name = (null)}, index = 0
17:14:37.7117 async thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 2
17:14:37.7127 callback thread = <NSThread: 0x60000027cf00>{number = 5, name = (null)}, index = 3
17:14:37.7128 async thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 0
17:14:37.7138 async thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 3
17:14:37.7457 callback thread = <NSThread: 0x604000460380>{number = 6, name = (null)}, index = 4
17:14:37.7461 async thread = <NSThread: 0x604000460380>{number = 6, name = (null)}, index = 4
17:14:37.7672 callback thread = <NSThread: 0x60000027cf00>{number = 5, name = (null)}, index = 5
17:14:37.7686 callback thread = <NSThread: 0x60000027cf00>{number = 5, name = (null)}, index = 6
17:14:37.7694 async thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 5
17:14:37.7745 async thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 6
17:14:37.7839 callback thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 7
17:14:37.7853 async thread = <NSThread: 0x60000027c300>{number = 7, name = (null)}, index = 7
17:14:37.7928 callback thread = <NSThread: 0x600000279880>{number = 4, name = (null)}, index = 8
17:14:37.7945 async thread = <NSThread: 0x604000460380>{number = 6, name = (null)}, index = 8
17:14:37.7960 callback thread = <NSThread: 0x60000027d8c0>{number = 8, name = (null)}, index = 9
17:14:37.7968 async thread = <NSThread: 0x60000027d8c0>{number = 8, name = (null)}, index = 9
由此可以發(fā)現(xiàn)問題稿湿,將任務(wù)添加到串行隊列之后铅匹,串行隊列在執(zhí)行任務(wù)的時候,并不是在一個線程饺藤,從而引發(fā)另一個問題包斑,這些任務(wù)的執(zhí)行有兩種情況考杉,并發(fā)和串行。
因為是任務(wù)在不同的線程執(zhí)行的舰始,所以懷疑可能是并發(fā)的執(zhí)行崇棠;
又因為是在串行隊列中,所以也可能是串行執(zhí)行丸卷,只不過是自不同線程的里面的執(zhí)行的枕稀。
于是將代碼改為如下所示:
@interface ViewController ()
@property (nonatomic, assign) NSInteger counts;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.counts = 1000;
}
- (void)testRequestQueue {
dispatch_queue_t q = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
for (NSUInteger i = 0; i < 100; i++) {
NSLog(@"start request index = %@", @(i));
[ODMHttp post:@"http://192.168.124.11:5000/odindomain" parameters:@{@"index": @(i)} success:^(id _Nullable responseObject) {
NSLog(@"callback thread = %@, index = %@", NSThread.currentThread, @(i));
dispatch_async(q, ^{
self.counts -= 1;
NSLog(@"async thread = %@, index = %@ counts = %@", NSThread.currentThread, @(i), @(self.counts));
});
} failure:^(NSError * _Nullable error) {
}];
}
}
如果是多線程并發(fā)執(zhí)行任務(wù)的話,那么counts打印的順序應(yīng)該會是亂序谜嫉,經(jīng)過我多次測試萎坷,counts的打印都是順序的。
再者如果在打印count之前再添加一句代碼
[NSThread sleepForTimeInterval:3];
那么會發(fā)現(xiàn)結(jié)果很有意思沐兰,所有的任務(wù)的執(zhí)行又放在同一個線程里面哆档,并且是串行執(zhí)行的。
由以上的測試的結(jié)果住闯,可以得出一個粗略的結(jié)論瓜浸,串行隊列可以保證隊列里的任務(wù)是串行執(zhí)行的,但是不能保證這些任務(wù)是在同一個線程里面執(zhí)行 比原。這里面涉及到iOS對線程復(fù)用的知識插佛,還未深究。