DISPATCH_QUEUE_CONCURRENT和DISPATCH_QUEUE_SERIAL有什么區別

[英]What's the difference between DISPATCH_QUEUE_CONCURRENT and DISPATCH_QUEUE_SERIAL


I implement the following class:

我執行以下類:

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)")
            }
        }
    }
}

And run as the following:

並按以下方式運行:

Run A:

運行:

gCDStudy = GCDStudy()
gCDStudy.asyncSerial(1)
gCDStudy.asyncSerial(2)

Run B

運行B

vgCDStudy2 = GCDStudy()
gCDStudy2.asyncConcurrent(1)
gCDStudy2.asyncConcurrent(2)

And the result RunA:

結果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

The result of RunB:

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

These results are the same, what is the different between these?

這些結果是一樣的,它們之間有什么區別?

Sometimes these results are different.

有時這些結果是不同的。

Thanks

謝謝

1 个解决方案

#1


11  

Serial queues execute one task at a time in the order in which they are added to the queue whereas concurrent queues execute one or more tasks concurrently. An important thing to note is that, even for concurrent queues, tasks are started in the order in which they were added to the queue. Because of that, in your case, you'll probably see no change in the output as print will be executed pretty fast by each thread in the concurrent queue so that it'll seem as if they were executed sequentially.

串行隊列按順序執行一項任務,其中將它們添加到隊列中,而並發隊列同時執行一個或多個任務。需要注意的重要一點是,即使對於並發隊列,任務的啟動順序也是按照它們被添加到隊列中的順序進行的。因此,在您的例子中,您可能不會看到輸出中的任何更改,因為打印將被並發隊列中的每個線程執行得非常快,因此看起來它們似乎是按順序執行的。

If you expect to see a difference, maybe you might try updating your code as follows:

如果您希望看到差異,也許您可以嘗試更新您的代碼如下:

class GCDStudy {

  func asyncSerial(time: Double) {
    let queue = dispatch_queue_create("DISPATCH_QUEUE_SERIAL", DISPATCH_QUEUE_SERIAL)

    for i in 0..<3 {
      dispatch_async(queue) {
        print("asyncSerial -wait:\(time)-\(i)")
      }
    }
  }

  func asyncConcurrent(time: Double) {
    let queue = dispatch_queue_create("DISPATCH_QUEUE_CONCURRENT", DISPATCH_QUEUE_CONCURRENT)

    for i in 0..<3 {
      dispatch_async(queue) {
        print("asyncConcurrent -wait:\(time)-\(i)")
      }
    }
  }
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/11/10/f7bc553da78100e820d76a51407aef8a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com