//
// 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)
}
}
}
}
GCD基礎(chǔ)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門轻掩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來幸乒,“玉大人,你說我怎么就攤上這事唇牧『痹” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵丐重,是天一觀的道長腔召。 經(jīng)常有香客問我,道長扮惦,這世上最難降的妖魔是什么臀蛛? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮崖蜜,結(jié)果婚禮上浊仆,老公的妹妹穿的比我還像新娘。我一直安慰自己豫领,他們只是感情好抡柿,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著等恐,像睡著了一般洲劣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鼠锈,一...
- 文/蒼蘭香墨 我猛地睜開眼横缔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了衫哥?” 一聲冷哼從身側(cè)響起茎刚,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎撤逢,沒想到半個(gè)月后膛锭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡蚊荣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年初狰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片互例。...
- 正文 年R本政府宣布糊秆,位于F島的核電站武福,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏痘番。R本人自食惡果不足惜艘儒,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望夫偶。 院中可真熱鬧,春花似錦觉增、人聲如沸兵拢。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽说铃。三九已至,卻和暖如春嘹履,著一層夾襖步出監(jiān)牢的瞬間腻扇,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓焕刮,卻偏偏與公主長得像舶沿,于是被迫代替她去往敵國和親墙杯。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 這8種學(xué)生永遠(yuǎn)拿不到高分邑闲!早看早受益算行! 下面是一位資深班主任總結(jié)了8種成績提不上去的原因,分別對應(yīng)8類孩子监憎,如果你...
- 這8種學(xué)生永遠(yuǎn)拿不到高分类少!早看早受益! 下面是一位資深班主任總結(jié)了8種成績提不上去的原因渔扎,分別對應(yīng)8類孩子硫狞,如果你...
- 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友晃痴。感恩相遇残吩!感恩不離不棄。 中午開了第一次的黨會(huì)倘核,身份的轉(zhuǎn)變要...
- 表情是什么,我認(rèn)為表情就是表現(xiàn)出來的情緒紧唱。表情可以傳達(dá)很多信息活尊。高興了當(dāng)然就笑了,難過就哭了漏益。兩者是相互影響密不可...