let q =DispatchQueue.init(label: "concurrent", attributes: .concurrent) // 指定为并发 // 使用WorkItem提交任务 let work =DispatchWorkItem.init(block:{ print("++1: item run on \(Thread.current)") DispatchQueue.global().async { print("++2: item run on \(Thread.current)") } }) //work.perform() //在当前线程执行任务 // 执行任务 q.async(execute: work)
let work =DispatchWorkItem.init(block:{ print("++1: item run on \(Thread.current)") DispatchQueue.global().async { print("++2: item run on \(Thread.current)") } }) work.perform()
//输出日志: //++1: item run on <NSThread: 0x600001cf41c0>{number = 1, name = main} //++2: item run on <NSThread: 0x600001cfa240>{number = 3, name = (null)}
2.wait()
阻塞当前线程,待当前任务完成后,再继续执行后续代码。
1 2 3 4 5 6 7 8 9 10 11 12 13
let work =DispatchWorkItem.init { sleep(1) print("++working.. \(Thread.current)") } DispatchQueue.global().async(execute: work) print("++before wait on \(Thread.current)") work.wait() print("++after wait on \(Thread.current)")
//输出日志: //++before wait on <NSThread: 0x600001b08440>{number = 1, name = main} //++working.. <NSThread: 0x600001b023c0>{number = 6, name = (null)} //++after wait on <NSThread: 0x600001b08440>{number = 1, name = main}
let work =DispatchWorkItem.init { sleep(1) print("++working.. \(Thread.current)") } DispatchQueue.global().sync(execute: work) print("++before wait on \(Thread.current)") work.wait() print("++after wait on \(Thread.current)")
//输出日志: //++working.. <NSThread: 0x6000019f40c0>{number = 1, name = main} //++before wait on <NSThread: 0x6000019f40c0>{number = 1, name = main} //++after wait on <NSThread: 0x6000019f40c0>{number = 1, name = main}
let q1 =DispatchQueue.init(label: "q1") let q2 =DispatchQueue.init(label: "q2") let work =DispatchWorkItem.init(block: { print("++work on \(Thread.current)") sleep(2) }) // 监听通知 work.notify(queue: q2) { print("notify on \(Thread.current)") } q1.async(execute: work) print("finished on \(Thread.current)")
//输出日志: //++work on <NSThread: 0x600000785f40>{number = 3, name = (null)} //++finished on <NSThread: 0x60000078c180>{number = 1, name = main} //++notify on <NSThread: 0x6000007889c0>{number = 7, name = (null)}
notify也涉及到线程间的通信,收到任务完成通知后,可以在另一队列中执行其他任务。
需要指出的是,notify中的任务会异步地执行,因此有机会开辟新线程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
let q1 =DispatchQueue.init(label: "q1") let work =DispatchWorkItem.init(block: { print("++work on \(Thread.current)") sleep(2) }) // 通知 work.notify(queue: q1) { print("++notify on \(Thread.current)") } q1.sync(execute: work) print("++finished on \(Thread.current)")
//输出日志: //++work on <NSThread: 0x600003ee0200>{number = 1, name = main} //++notify on <NSThread: 0x600003ee8ac0>{number = 3, name = (null)} //++finished on <NSThread: 0x600003ee0200>{number = 1, name = main}
let q =DispatchQueue.init(label: "q", attributes: .concurrent) for index in0..<10 { if index >=5&& index <7 { q.async(flags: .barrier) { // 栅栏 print("barrier \(index), on \(Thread.current)") } }else{ q.async(){ print("\(index), on \(Thread.current)") } } }
//输出日志: //0, on <NSThread: 0x600003e92280>{number = 6, name = (null)} //2, on <NSThread: 0x600003e9e300>{number = 5, name = (null)} //1, on <NSThread: 0x600003e99a40>{number = 7, name = (null)} //4, on <NSThread: 0x600003e99a40>{number = 7, name = (null)} //3, on <NSThread: 0x600003e92280>{number = 6, name = (null)} //barrier 5, on <NSThread: 0x600003e92280>{number = 6, name = (null)} //barrier 6, on <NSThread: 0x600003e92280>{number = 6, name = (null)} //9, on <NSThread: 0x600003e9e300>{number = 5, name = (null)} //8, on <NSThread: 0x600003e92280>{number = 6, name = (null)} //7, on <NSThread: 0x600003e9ea40>{number = 3, name = (null)}
let q =DispatchQueue.init(label: "concurrent", attributes: .concurrent) for index in0..<10 { if index >=5&& index <7 { let work =DispatchWorkItem.init(flags: .barrier) { // 栅栏 print("barrier \(index), on \(Thread.current)") } q.async(execute: work) }else{ q.async { print("\(index), on \(Thread.current)") } } }
//输出的日志: //0, on <NSThread: 0x600001fe8600>{number = 6, name = (null)} //1, on <NSThread: 0x600001fe8680>{number = 5, name = (null)} //2, on <NSThread: 0x600001fe9200>{number = 7, name = (null)} //4, on <NSThread: 0x600001fe8600>{number = 6, name = (null)} //3, on <NSThread: 0x600001fe8640>{number = 8, name = (null)} //barrier 5, on <NSThread: 0x600001fe8640>{number = 8, name = (null)} //barrier 6, on <NSThread: 0x600001fe8640>{number = 8, name = (null)} //8, on <NSThread: 0x600001fe8680>{number = 5, name = (null)} //9, on <NSThread: 0x600001fe8600>{number = 6, name = (null)} //7, on <NSThread: 0x600001fe8640>{number = 8, name = (null)}