var count = 0
var timer: Timer? = nil
func test() {
let queue = DispatchQueue(label: "aaa")
queue.async {
print("start\(Thread.current)")
queue.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: 1), execute: DispatchWorkItem(block: {
print("1s后執(zhí)行異步任務(wù)\(Thread.current)")
}))
self.perform(#selector(self.perfromTask), with: nil, afterDelay: 2)
self.timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
print("定時(shí)任務(wù)\(Thread.current)")
self.count += 1
if self.count > 10 {
self.timer?.invalidate()
self.timer = nil
}
}
RunLoop.current.add(self.timer!, forMode: .common)
RunLoop.current.run()
}
}
@objc func perfromTask() {
print("perform 延時(shí)任務(wù)\(Thread.current)")
}
/**
start<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
perform 延時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
定時(shí)任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
1s后執(zhí)行異步任務(wù)<NSThread: 0x6000000bc6c0>{number = 7, name = (null)} //(timer任務(wù)會(huì)卡住當(dāng)前線程)
*/
GCD處理任務(wù)和runloop的關(guān)系
?著作權(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)容
- 一、需求 在處理一項(xiàng)功能時(shí)弛秋,需要進(jìn)行a,b,c三個(gè)步驟器躏,其中步驟b需要拿到a步驟的結(jié)果,而a步驟中結(jié)果是在異步線程...
- 前言 文章主要會(huì)RunLoop源碼進(jìn)行剖析,里面會(huì)有對它的理解及注釋,有不足望見解 1,RunLoop是什么? 廣...
- 由于字?jǐn)?shù)限制 接上一遍http://www.reibang.com/p/a5c442cb9a7f繼續(xù)講解下去 7...
- 前言 文章主要會(huì)RunLoop源碼進(jìn)行剖析,里面會(huì)有對它的理解及注釋,有不足望見解 1,RunLoop是什么? 廣...
- 在軟件開發(fā)中使用多線程可以大大地提高用戶體驗(yàn)揽浙,提高效率。Grand Central Dispatch(CGD)則是...