GCD基礎(chǔ)

//
//  QueueTestController.swift
//  GCDDemo
//
//  Created by shangkun on 2018/4/29.
//  Copyright ? 2018 J1. All rights reserved.
//

import UIKit

@objc class QueueTestController: UITableViewController {
    
    //    除了主隊(duì)列以外的所有異步執(zhí)行都會(huì)新建線程, 并發(fā)執(zhí)行。
    //    在主隊(duì)列下的任務(wù)不管是異步任務(wù)還是同步任務(wù)都不會(huì)開辟線程囤踩,任務(wù)只會(huì)在主線程順序執(zhí)行袜硫。
    //    serial將會(huì)執(zhí)行完一個(gè)任務(wù)才會(huì)開始下一個(gè)眯亦,
    //    concurrent觸發(fā)完一個(gè)就立即進(jìn)入下一個(gè),而不管它是否已完成宠能。
    
    lazy var scenes = [
        [
            "title":"主隊(duì)列,同步 -> 1 死鎖",
            "action":"mainQueueSync"
        ],
        [
            "title":"主隊(duì)列,異步 -> 132 沒有開啟新線程",
            "action":"mainQueueAsync"
        ], [
            "title":"串行隊(duì)列牢贸,同步 -> 123 順序執(zhí)行,沒有開啟新線程",
            "action":"serialQueueSync"
        ],
           [
            "title":"串行隊(duì)列镐捧,異步 -> 132 并發(fā)執(zhí)行潜索,開啟新線程",
            "action":"serialQueueAsync"
        ],  [
            "title":"并行隊(duì)列,同步 -> 123 順序執(zhí)行懂酱,沒有開啟新線程",
            "action":"concurrentQueueSync"
        ],
            [
                "title":"并行隊(duì)列竹习,異步 -> 132 并發(fā)執(zhí)行,開啟新線程",
                "action":"concurrentQueueAsync"
        ],
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "queue"
        tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "reuseIdentifier")
    }
    
    @objc func mainQueueSync() {
        
        print("1 \(Thread.current)")
        let queue = DispatchQueue.main
        queue.sync {
            print("2  \(Thread.current)")
        }
        print("3 \(Thread.current)")
        
        /*  發(fā)生死鎖列牺,程序崩潰
         主隊(duì)列的同步線程整陌,按照FIFO的原則(先入先出),2排在3后面會(huì)等3執(zhí)行完瞎领,但因?yàn)橥骄€程泌辫,3又要等2執(zhí)行完,相互等待成為死鎖九默。
         
         1 <NSThread: 0x282a43080>{number = 1, name = main}
         */
    }
    
    @objc func mainQueueAsync() {
        
        print("1 \(Thread.current)")
        let queue = DispatchQueue.main
        queue.async {
            for j in 0...10 {
                print("2 j=\(j) \(Thread.current)")
            }
        }
        print("3 \(Thread.current)")
        
        /*
         主線程順序執(zhí)行震放,異步實(shí)現(xiàn)最后執(zhí)行,也在主線程中
         沒有開啟新線程
         
         1 <NSThread: 0x281113080>{number = 1, name = main}
         3 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=0 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=1 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=2 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=3 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=4 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=5 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=6 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=7 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=8 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=9 <NSThread: 0x281113080>{number = 1, name = main}
         2 j=10 <NSThread: 0x281113080>{number = 1, name = main}
         */
    }
    
    @objc func serialQueueSync() {
        
        print("1 \(Thread.current)")
        let queue = DispatchQueue.init(label: "serialQueueSync")
        queue.sync {
            for i in 0...5 {
                print("2 i=\(i) \(Thread.current)")
            }
        }
        queue.sync {
            for j in 0...5 {
                print("2 j=\(j) \(Thread.current)")
            }
        }
        print("3 \(Thread.current)")
        
        /*
         順序執(zhí)行驼修,在主線程中
         沒有開啟新線程
         
         1 <NSThread: 0x282b97080>{number = 1, name = main}
         2 i=0 <NSThread: 0x282b97080>{number = 1, name = main}
         2 i=1 <NSThread: 0x282b97080>{number = 1, name = main}
         2 i=2 <NSThread: 0x282b97080>{number = 1, name = main}
         2 i=3 <NSThread: 0x282b97080>{number = 1, name = main}
         2 i=4 <NSThread: 0x282b97080>{number = 1, name = main}
         2 i=5 <NSThread: 0x282b97080>{number = 1, name = main}
         2 j=0 <NSThread: 0x282b97080>{number = 1, name = main}
         2 j=1 <NSThread: 0x282b97080>{number = 1, name = main}
         2 j=2 <NSThread: 0x282b97080>{number = 1, name = main}
         2 j=3 <NSThread: 0x282b97080>{number = 1, name = main}
         2 j=4 <NSThread: 0x282b97080>{number = 1, name = main}
         2 j=5 <NSThread: 0x282b97080>{number = 1, name = main}
         3 <NSThread: 0x282b97080>{number = 1, name = main}
         
         */
    }
    
    @objc func serialQueueAsync() {
        
        print("1 \(Thread.current)")
        let queue = DispatchQueue.init(label: "serialQueueAsync")
        queue.async {
            for i in 0...5 {
                print("2 i=\(i) \(Thread.current)")
            }
        }
        queue.async {
            for j in 0...5 {
                print("2 j=\(j) \(Thread.current)")
            }
        }
        print("3 \(Thread.current)")
        
        /*
         并發(fā)執(zhí)行殿遂。 不在主線程
         開啟新線程
         
         1 <NSThread: 0x280466b00>{number = 1, name = main}
         3 <NSThread: 0x280466b00>{number = 1, name = main}
         2 i=0 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 i=1 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 i=2 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 i=3 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 i=4 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 i=5 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 j=0 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 j=1 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 j=2 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 j=3 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 j=4 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         2 j=5 <NSThread: 0x28040e3c0>{number = 3, name = (null)}
         
         */
    }
    
    @objc func concurrentQueueSync() {
        
        print("1 \(Thread.current)")
        let queue = DispatchQueue.init(label: "concurrentQueueSync", attributes: .concurrent)
        queue.sync {
            for i in 0...5 {
                print("2 i=\(i) \(Thread.current)")
            }
        }
        queue.sync {
            for j in 0...5 {
                print("2 j=\(j) \(Thread.current)")
            }
        }
        print("3 \(Thread.current)")
        
        /*
         順序執(zhí)行诈铛,在主線程中
         沒有開啟新線程
         
         1 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 i=0 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 i=1 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 i=2 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 i=3 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 i=4 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 i=5 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 j=0 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 j=1 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 j=2 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 j=3 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 j=4 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 j=5 <NSThread: 0x2800b2940>{number = 1, name = main}
         3 <NSThread: 0x2800b2940>{number = 1, name = main}
         
         */
    }
    
    @objc func concurrentQueueAsync() {
        
        print("1 \(Thread.current)")
        let queue = DispatchQueue.init(label: "concurrentQueueAsync")
        queue.async {
            for i in 0...5 {
                print("2 i=\(i) \(Thread.current)")
            }
        }
        queue.async {
            for j in 0...5 {
                print("2 j=\(j) \(Thread.current)")
            }
        }
        print("3 \(Thread.current)")
        
        /*
         并發(fā)執(zhí)行。 不在主線程
         開啟新線程
         
         1 <NSThread: 0x2800b2940>{number = 1, name = main}
         3 <NSThread: 0x2800b2940>{number = 1, name = main}
         2 i=0 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 i=1 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 i=2 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 i=3 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 i=4 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 i=5 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 j=0 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 j=1 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 j=2 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 j=3 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 j=4 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         2 j=5 <NSThread: 0x2800ece00>{number = 6, name = (null)}
         
         */
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return scenes.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        cell.textLabel?.text = scenes[indexPath.item]["title"]
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if let function = scenes[indexPath.item]["action"] {
            let selector = Selector(function)
            if self.responds(to: selector) {
                perform(selector)
            }
        }
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末墨礁,一起剝皮案震驚了整個(gè)濱河市幢竹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌饵溅,老刑警劉巖妨退,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蜕企,居然都是意外死亡咬荷,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門轻掩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來幸乒,“玉大人,你說我怎么就攤上這事唇牧『痹” “怎么了?”我有些...
    開封第一講書人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵丐重,是天一觀的道長腔召。 經(jīng)常有香客問我,道長扮惦,這世上最難降的妖魔是什么臀蛛? 我笑而不...
    開封第一講書人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮崖蜜,結(jié)果婚禮上浊仆,老公的妹妹穿的比我還像新娘。我一直安慰自己豫领,他們只是感情好抡柿,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著等恐,像睡著了一般洲劣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鼠锈,一...
    開封第一講書人閱讀 50,050評(píng)論 1 291
  • 那天闪檬,我揣著相機(jī)與錄音,去河邊找鬼购笆。 笑死粗悯,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的同欠。 我是一名探鬼主播样傍,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼横缔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了衫哥?” 一聲冷哼從身側(cè)響起茎刚,我...
    開封第一講書人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎撤逢,沒想到半個(gè)月后膛锭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蚊荣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年初狰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片互例。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奢入,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出媳叨,到底是詐尸還是另有隱情腥光,我是刑警寧澤,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布糊秆,位于F島的核電站武福,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏痘番。R本人自食惡果不足惜艘儒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望夫偶。 院中可真熱鬧,春花似錦觉增、人聲如沸兵拢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽说铃。三九已至,卻和暖如春嘹履,著一層夾襖步出監(jiān)牢的瞬間腻扇,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來泰國打工砾嫉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留幼苛,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓焕刮,卻偏偏與公主長得像舶沿,于是被迫代替她去往敵國和親墙杯。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

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