स्विफ्ट 5.1 के साथ, ग्रैंड सेंट्रल डिस्पैच आपकी समस्या को हल करने के कई तरीके प्रदान करता है। अपनी आवश्यकताओं के अनुसार, आप निम्नलिखित खेल के मैदान के स्निपेट्स में दिखाए गए सात पैटर्न में से एक चुन सकते हैं ।
Apple डेवलपर कॉन्सिक्वैरिटी प्रोग्रामिंग गाइड के बारे में बताता हैDispatchGroup :
डिस्पैच समूह एक थ्रेड को ब्लॉक करने का एक तरीका है जब तक कि एक या अधिक कार्य निष्पादित नहीं होते हैं। आप इस व्यवहार का उपयोग उन स्थानों पर कर सकते हैं जहाँ आप तब तक प्रगति नहीं कर सकते जब तक कि सभी निर्दिष्ट कार्य पूरे नहीं हो जाते। उदाहरण के लिए, कुछ डेटा की गणना करने के लिए कई कार्यों को भेजने के बाद, आप उन कार्यों पर प्रतीक्षा करने के लिए एक समूह का उपयोग कर सकते हैं और तब परिणाम की प्रक्रिया कर सकते हैं जब वे किए जाते हैं।
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue(label: "com.company.app.queue", attributes: .concurrent)
let group = DispatchGroup()
queue.async(group: group) {
print("#1 started")
Thread.sleep(forTimeInterval: 5)
print("#1 finished")
}
queue.async(group: group) {
print("#2 started")
Thread.sleep(forTimeInterval: 2)
print("#2 finished")
}
group.notify(queue: queue) {
print("#3 finished")
}
/*
prints:
#1 started
#2 started
#2 finished
#1 finished
#3 finished
*/
# 2। का उपयोग करते हुए DispatchGroup, DispatchGroup's wait(), DispatchGroup' s enter()और DispatchGroup'sleave()
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue(label: "com.company.app.queue", attributes: .concurrent)
let group = DispatchGroup()
group.enter()
queue.async {
print("#1 started")
Thread.sleep(forTimeInterval: 5)
print("#1 finished")
group.leave()
}
group.enter()
queue.async {
print("#2 started")
Thread.sleep(forTimeInterval: 2)
print("#2 finished")
group.leave()
}
queue.async {
group.wait()
print("#3 finished")
}
/*
prints:
#1 started
#2 started
#2 finished
#1 finished
#3 finished
*/
ध्यान दें कि आप भी मिश्रण कर सकते हैं DispatchGroup wait()के साथ DispatchQueue async(group:qos:flags:execute:)या मिश्रण DispatchGroup enter()और DispatchGroup leave()साथ DispatchGroup notify(qos:flags:queue:execute:)।
स्विफ्ट 4 के लिए ग्रैंड सेंट्रल डिस्पैच ट्यूटोरियल: रेवेंडरलिच डॉट कॉम का भाग 1/2 लेख बाधाओं के लिए एक परिभाषा देता है :
डिस्पैच बाधाएं समवर्ती कतारों के साथ काम करते समय एक धारावाहिक शैली की अड़चन के रूप में कार्य करने वाले कार्यों का एक समूह है। जब आप DispatchWorkItemएक प्रेषण कतार में जमा करते हैं, तो आप यह इंगित करने के लिए झंडे सेट कर सकते हैं कि यह उस विशेष समय के लिए निर्दिष्ट कतार पर निष्पादित एकमात्र आइटम होना चाहिए। इसका मतलब यह है कि DispatchWorkItemवसीयत बैरियर से पहले कतार में जमा की गई सभी वस्तुओं को वसीयत को पूरा करने से पहले पूरा करना होगा।
उपयोग:
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue(label: "com.company.app.queue", attributes: .concurrent)
queue.async {
print("#1 started")
Thread.sleep(forTimeInterval: 5)
print("#1 finished")
}
queue.async {
print("#2 started")
Thread.sleep(forTimeInterval: 2)
print("#2 finished")
}
queue.async(flags: .barrier) {
print("#3 finished")
}
/*
prints:
#1 started
#2 started
#2 finished
#1 finished
#3 finished
*/
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue(label: "com.company.app.queue", attributes: .concurrent)
queue.async {
print("#1 started")
Thread.sleep(forTimeInterval: 5)
print("#1 finished")
}
queue.async {
print("#2 started")
Thread.sleep(forTimeInterval: 2)
print("#2 finished")
}
let dispatchWorkItem = DispatchWorkItem(qos: .default, flags: .barrier) {
print("#3 finished")
}
queue.async(execute: dispatchWorkItem)
/*
prints:
#1 started
#2 started
#2 finished
#1 finished
#3 finished
*/
# 5। का उपयोग कर DispatchSemaphore, DispatchSemaphore'एस wait()और DispatchSemaphore' एसsignal()
सोरूस खान्लू ने जीसीडी हैंडबुक ब्लॉग पोस्ट में निम्नलिखित पंक्तियां लिखी हैं :
एक सेमाफोर का उपयोग करके, हम एक थ्रेड को समय की एक मनमानी राशि के लिए ब्लॉक कर सकते हैं, जब तक कि किसी अन्य धागे से संकेत नहीं भेजा जाता है। बाकी GCD की तरह सेमाफोर थ्रेड-सेफ हैं, और इन्हें कहीं से भी ट्रिगर किया जा सकता है। जब एक एसिंक्रोनस एपीआई है जिसे आपको सिंक्रोनस बनाने की आवश्यकता होती है, तो सेमाफोरस का उपयोग किया जा सकता है, लेकिन आप इसे संशोधित नहीं कर सकते।
Apple डेवलपर API संदर्भ DispatchSemaphore init(value:)इनिशियलाइज़र के लिए निम्नलिखित चर्चा भी देता है:
मूल्य के लिए शून्य पास करना तब उपयोगी होता है जब दो धागों को किसी विशेष घटना को पूरा करने के लिए सामंजस्य स्थापित करने की आवश्यकता होती है। शून्य से अधिक मूल्य पास करना संसाधनों के एक सीमित पूल के प्रबंधन के लिए उपयोगी है, जहां पूल का आकार मूल्य के बराबर है।
उपयोग:
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue(label: "com.company.app.queue", attributes: .concurrent)
let semaphore = DispatchSemaphore(value: 0)
queue.async {
print("#1 started")
Thread.sleep(forTimeInterval: 5)
print("#1 finished")
semaphore.signal()
}
queue.async {
print("#2 started")
Thread.sleep(forTimeInterval: 2)
print("#2 finished")
semaphore.signal()
}
queue.async {
semaphore.wait()
semaphore.wait()
print("#3 finished")
}
/*
prints:
#1 started
#2 started
#2 finished
#1 finished
#3 finished
*/
Apple डेवलपर API संदर्भ के बारे में बताता है OperationQueue:
ऑपरेशन कतारें libdispatchअपने संचालन के निष्पादन को आरंभ करने के लिए पुस्तकालय (ग्रैंड सेंट्रल डिस्पैच के रूप में भी जानी जाती हैं) का उपयोग करती हैं।
उपयोग:
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let operationQueue = OperationQueue()
let blockOne = BlockOperation {
print("#1 started")
Thread.sleep(forTimeInterval: 5)
print("#1 finished")
}
let blockTwo = BlockOperation {
print("#2 started")
Thread.sleep(forTimeInterval: 2)
print("#2 finished")
}
let blockThree = BlockOperation {
print("#3 finished")
}
blockThree.addDependency(blockOne)
blockThree.addDependency(blockTwo)
operationQueue.addOperations([blockThree, blockTwo, blockOne], waitUntilFinished: false)
/*
prints:
#1 started
#2 started
#2 finished
#1 finished
#3 finished
or
#2 started
#1 started
#2 finished
#1 finished
#3 finished
*/
# 7। का उपयोग करते हुए OperationQueueऔर OperationQueueके addBarrierBlock(_:)(आईओएस 13 की आवश्यकता है)
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let operationQueue = OperationQueue()
let blockOne = BlockOperation {
print("#1 started")
Thread.sleep(forTimeInterval: 5)
print("#1 finished")
}
let blockTwo = BlockOperation {
print("#2 started")
Thread.sleep(forTimeInterval: 2)
print("#2 finished")
}
operationQueue.addOperations([blockTwo, blockOne], waitUntilFinished: false)
operationQueue.addBarrierBlock {
print("#3 finished")
}
/*
prints:
#1 started
#2 started
#2 finished
#1 finished
#3 finished
or
#2 started
#1 started
#2 finished
#1 finished
#3 finished
*/