Last active
June 10, 2018 13:58
-
-
Save TakahashiIkki/7a324dbafaafa56dc756dd21127961f3 to your computer and use it in GitHub Desktop.
swift_begin_day10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thread の例 | |
import Foundation | |
import PlaygroundSupport | |
// Playground上で非同期処理を有効にする | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
class SubThread: Thread { | |
override func main() { | |
print("実行されたよー!") | |
} | |
} | |
let thread = SubThread() | |
thread.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Operation・OperationQueue の例 | |
import Foundation | |
import PlaygroundSupport | |
// Playground上で非同期処理を有効にする | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
class SubOperation: Operation { | |
let opeName: String | |
init(opeName: String) { | |
self.opeName = opeName | |
} | |
override func main() { | |
// 1秒待つ | |
Thread.sleep(forTimeInterval: 1) | |
print("名前 \(opeName) 非同期処理が実行されたで") | |
} | |
} | |
// キュー: OperationQueueの生成 | |
let queue = OperationQueue() | |
queue.name = "com.test.myOpeationQueue" | |
queue.maxConcurrentOperationCount = 2 //特定のキューが同時に実行できる数. | |
queue.qualityOfService = .userInitiated // QoS型と同じ内容. | |
// タスク追加の例 | |
var operations = [SubOperation]() | |
for i in 0..<10 { | |
operations.append(SubOperation(opeName: "operation.\(i)")) | |
} | |
queue.addOperations(operations, waitUntilFinished: false) | |
print("Operations are added") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment