gì tôi thực hiện các lớp sau đây:phần chênh lệch giữa DISPATCH_QUEUE_CONCURRENT và DISPATCH_QUEUE_SERIAL
class GCDStudy {
func asyncSerial(time:Double){
let queue = dispatch_queue_create("DISPATCH_QUEUE_SERIAL",DISPATCH_QUEUE_SERIAL)
dispatch_async(queue){
var i:Double = 0
while(i < 3){
i++
print("asyncSerial -wait:\(time)-\(i)")
}
}
}
func asyncConcurrent(time:Double){
let queue = dispatch_queue_create("DISPATCH_QUEUE_CONCURRENT",DISPATCH_QUEUE_CONCURRENT)
dispatch_async(queue){
var i:Double = 0
while(i < 3){
i++
print("asyncConcurrent -wait:\(time)-\(i)")
}
}
}
}
Và chạy như sau:
Run A:
gCDStudy = GCDStudy()
gCDStudy.asyncSerial(1)
gCDStudy.asyncSerial(2)
Run B
vgCDStudy2 = GCDStudy()
gCDStudy2.asyncConcurrent(1)
gCDStudy2.asyncConcurrent(2)
Và kết quả Runa:
asyncSerial -wait:1.0-1.0
asyncSerial -wait:2.0-1.0
asyncSerial -wait:1.0-2.0
asyncSerial -wait:2.0-2.0
asyncSerial -wait:1.0-3.0
asyncSerial -wait:2.0-3.0
Kết quả của RunB:
asyncSerial -wait:1.0-1.0
asyncSerial -wait:2.0-1.0
asyncSerial -wait:1.0-2.0
asyncSerial -wait:2.0-2.0
asyncSerial -wait:1.0-3.0
asyncSerial -wait:2.0-3.0
Những kết quả đều giống nhau, những gì là sự khác nhau giữa những?
Đôi khi các kết quả này khác nhau.
Cảm ơn
Bạn chỉ cử một khối vào hàng đợi trong mỗi ví dụ. Để xem sự khác biệt, hãy gửi nhiều khối tới mỗi hàng đợi. Nói cách khác, đặt cuộc gọi đến 'dispatch_async' bên trong một vòng lặp. – rmaddy