swift中DispatchGroup之管理异步任务

swift中DispatchGroup,可以分组管理异步任务;

1.分组管理多个异步任务

   func dispatchGroupConcurrentTest() {
        print("class:\(type(of: self)),line:\(#line)")
        
        let WORKMAXNUM = 10
        
        let dispatchGroup = Dispatch.DispatchGroup()
        
        let queue1 = DispatchQueue(label: "1")
        let queue2 = DispatchQueue(label: "2")
        let queue3 = DispatchQueue(label: "3")
        
        queue1.async(group: dispatchGroup, execute:DispatchWorkItem {
            for i in 0..

执行结果

class:ViewController,line:19
0 execute
1 execute
2 execute
3 execute
4 execute
5 execute
6 execute
7 execute
8 execute
9 execute
10 execute
20 execute
11 execute
12 execute
13 execute
14 execute
15 execute
16 execute
21 execute
17 execute
22 execute
23 execute
24 execute
25 execute
26 execute
27 execute
18 execute
28 execute
29 execute
19 execute
main queue execute!

2.分组管理多个串行的异步任务

    func dispatchGroupSerialTest() {
        
        print("class:\(type(of: self)),line:\(#line)")
        let WORKMAXNUM = 10
        
        let dispatchGroup = Dispatch.DispatchGroup()
        
        let queue1 = DispatchQueue(label: "1")
        let queue2 = DispatchQueue(label: "2")
        let queue3 = DispatchQueue(label: "3")
        
        dispatchGroup.enter()
        queue1.async(group: dispatchGroup, execute:DispatchWorkItem {
            for i in 0..

执行结果

class:ViewController,line:57
before wait
0 execute
1 execute
2 execute
3 execute
4 execute
5 execute
6 execute
7 execute
8 execute
9 execute
end wait
before wait
10 execute
11 execute
12 execute
13 execute
14 execute
15 execute
16 execute
17 execute
18 execute
19 execute
end wait
20 execute
21 execute
22 execute
23 execute
24 execute
25 execute
26 execute
27 execute
28 execute
29 execute
main queue execute!

3.小结

  • 1.注意上述两种不同的使用场景。
  • 2.注意DispatchGroupenterleave配对使用,以及与wait的配合使用。

你可能感兴趣的:(swift中DispatchGroup之管理异步任务)