Skip to content

Instantly share code, notes, and snippets.

@TakahashiIkki
Last active June 10, 2018 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakahashiIkki/7a324dbafaafa56dc756dd21127961f3 to your computer and use it in GitHub Desktop.
Save TakahashiIkki/7a324dbafaafa56dc756dd21127961f3 to your computer and use it in GitHub Desktop.
swift_begin_day10
// Thread の例
import Foundation
import PlaygroundSupport
// Playground上で非同期処理を有効にする
PlaygroundPage.current.needsIndefiniteExecution = true
class SubThread: Thread {
override func main() {
print("実行されたよー!")
}
}
let thread = SubThread()
thread.start()
// 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