2019-08-30

前幾天,同事多線程訪問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ù)用的知識插佛,還未深究。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末量窘,一起剝皮案震驚了整個濱河市雇寇,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蚌铜,老刑警劉巖锨侯,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異冬殃,居然都是意外死亡囚痴,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進店門造壮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來渡讼,“玉大人,你說我怎么就攤上這事耳璧。” “怎么了展箱?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵旨枯,是天一觀的道長。 經(jīng)常有香客問我混驰,道長攀隔,這世上最難降的妖魔是什么皂贩? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮昆汹,結(jié)果婚禮上明刷,老公的妹妹穿的比我還像新娘。我一直安慰自己满粗,他們只是感情好辈末,可當(dāng)我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著映皆,像睡著了一般挤聘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上捅彻,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天组去,我揣著相機與錄音,去河邊找鬼步淹。 笑死从隆,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的缭裆。 我是一名探鬼主播广料,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼幼驶!你這毒婦竟也來了艾杏?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤盅藻,失蹤者是張志新(化名)和其女友劉穎购桑,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體氏淑,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡勃蜘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了假残。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缭贡。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖辉懒,靈堂內(nèi)的尸體忽然破棺而出阳惹,到底是詐尸還是另有隱情,我是刑警寧澤眶俩,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布莹汤,位于F島的核電站,受9級特大地震影響颠印,放射性物質(zhì)發(fā)生泄漏纲岭。R本人自食惡果不足惜抹竹,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望止潮。 院中可真熱鬧窃判,春花似錦、人聲如沸喇闸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽仅偎。三九已至跨蟹,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間橘沥,已是汗流浹背窗轩。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留座咆,地道東北人痢艺。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像介陶,于是被迫代替她去往敵國和親堤舒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,066評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 本文用來介紹 iOS 多線程中 GCD 的相關(guān)知識以及使用方法哺呜。這大概是史上最詳細舌缤、清晰的關(guān)于 GCD 的詳細講...
    花花世界的孤獨行者閱讀 502評論 0 1
  • GCD簡介 Grand Central Dispatch (GCD)是Apple開發(fā)的一個多核編程的較新的解決方法...
    哦累哇滾筒洗衣機閱讀 1,198評論 1 2
  • GCD有三種queue main queue: 主線程隊列。是一個串行隊列某残。一般用來更新UI国撵。 global qu...
    FreeBreath閱讀 615評論 0 0
  • GCD 隊列組:dispatch_group 有時候我們會有這樣的需求:分別異步執(zhí)行多個耗時任務(wù),當(dāng)多個耗...
    gpylove閱讀 481評論 0 0
  • 一直想發(fā)表自己得第一篇文章玻墅,無奈自己是一個中度懶癌患者介牙。就自己認為在前端開發(fā)中遇到的一些小問題和自己旁聽到得一些知...
    Joe_God閱讀 268評論 0 4