表示當前線程的Scheduler
,默認使用的線程。那他是如何獲取當前線程的呢: 以subscribe
爲例,看一下bash
CurrentThreadScheduler.instance.schedule(())
經過schedule()
調用queue
使用當前關聯的線程key獲取並返回一個queue
CurrentThreadScheduler.isScheduleRequired
經過isScheduleRequired
設置一個線程調度key
Thread
拓展了兩個存取線程調度key的方法:
schedule()
,返回一個ScheduledItem
:
ScheduledItem
初始化過程會保存state
和action
,最後在invoke()
的時候調用
MainScheduler
是主線程,咱們要執行的一些和UI相關的操做就須要切換到主線程,它繼承於SerialDispatchQueueScheduler
(串行), 初始化的過程保存了一個線程_mainQueue: DispatchQueue
,這個queue
就是主線程self._mainQueue = DispatchQueue.main
多線程
它封裝了GCD
的串行隊列,若是咱們須要執行一些串行任務,能夠切換到這個Scheduler
,初始化經過configuration
-> DispatchQueueConfiguration
進行了一系列的多線程配置: ui
它封裝了GCD
的並行隊列,若是咱們須要執行一些並行任務,能夠切換到這個Scheduler
,初始化經過configuration
-> DispatchQueueConfiguration
進行了一系列的多線程配置: spa
NSOperation
,初始化經過
configuration
->
DispatchQueueConfiguration
進行了一系列的多線程配置:
* SerialDispatchQueueScheduler
複製代碼
Observable.of(1,2,3,4,5,6,7,8,9,10)
.observeOn(SerialDispatchQueueScheduler.init(internalSerialQueueName: "observeOnSerial"))
.subscribe{print("observeOn",$0,Thread.current)}
.disposed(by: self.bag)
複製代碼
* ConcurrentDispatchQueueScheduler
複製代碼
Observable.of(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)
.subscribeOn(ConcurrentDispatchQueueScheduler.init(qos: .background))
.subscribe{print("subscribeOn",$0,Thread.current)}
.disposed(by: self.bag)
複製代碼
* OperationQueueScheduler
複製代碼
let opQueue = OperationQueue.init()
Observable.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)
.subscribeOn(OperationQueueScheduler.init(operationQueue: opQueue))
.subscribe{print("subscribeOn",$0,Thread.current)}
.disposed(by: self.bag)
複製代碼