In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of an example analysis of multithreading in iOS. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Three multithreading technologies of iOS
1 、 NSThread
-advantages: NSThread is lighter and easier to use than the other two
-disadvantages: need to manage thread lifecycle, thread synchronization, locking, sleep, wake up, etc. The locking of data by thread synchronization will have some system overhead.
2 、 NSOperation
You don't need to worry about thread management and data synchronization, you can focus on the operations you need to perform.
3 、 GCD
Based on C language
Basic use basic use of NSThread
Method 1:
/ / block let thread = Thread.init {print ("1,-this is thread\ (Thread.current)")} thread.start ()
Method 2:
/ / SEL let thread2 = Thread.init (target: self, selector: # selector (text), object: nil) thread2.start () @ objc func text () {print ("2,-- this is thread\ (Thread.current)")}
Method 3:\
Self.performSelector (inBackground: # selector (text2), with: nil)
Output
Tips: you can add latency to any thread.
Print (thread.isCancelled) / / whether to cancel print (thread.isExecuting) / / whether to execute print (thread.isFinished) / / whether to complete print (thread.isMainThread) / / whether it is the basic use of the main thread NSOperation
Method 1:
Let queue = OperationQueue.init () / / maximum number of threads queue.maxConcurrentOperationCount = 2 queue.addOperation {sleep (1) print ("1111")} queue.addOperation {print ("2222")}
The output is as follows:
Method 2: priority can be set
Let queue = OperationQueue.init () let op = BlockOperation.init {print ("op")} op.queuePriority = .normal / / set priority queue.addOperation (op) let op2 = BlockOperation.init {print ("op2")} op2.queuePriority = .normal queue.addOperation (op2) queue .addOperation {print ("op3")}
Make a slight modification
Let queue = OperationQueue.init () let op = BlockOperation.init {sleep (1) print ("op")} op.queuePriority = .normal / / set priority queue.addOperation (op) let op2 = BlockOperation.init {sleep (1) print ("op2")} op2.queuePriority =. VeryHigh queue.addOperation (op2) queue.addOperation {print ("op3")}
The output is as follows:
The priorities are as follows:
Basic use of GCD
Method 1: queue.async async
Let queue = DispatchQueue.init (label: "com.zjb.concurrent", attributes: .concurrent) for i in 0.15 {queue.async {sleep (1) print ("this is\ (Thread.current)\ (I)")}}
Method 2: queue.sync synchronization
Let queue = DispatchQueue.init (label: "com.zjb.concurrent", attributes: .concurrent) for i in 0.15 {queue.sync {sleep (1) print ("this is\ (Thread.current)\ (I)")}}
Attach a piece of code on the network
For i in 1.. 10 {DispatchQueue.global (qos: DispatchQoS.QoSClass.default). Async {NSLog ("DispatchQoS.QoSClass.default,% d", I)} DispatchQueue.global (qos: DispatchQoS.QoSClass.background). Async {NSLog ("DispatchQoS.QoSClass.background,% d") I)} DispatchQueue.global (qos: DispatchQoS.QoSClass.unspecified). Async {NSLog ("DispatchQoS.QoSClass.unspecified,% d", I)} DispatchQueue.global (qos: DispatchQoS.QoSClass.userInitiated). Async {NSLog ("DispatchQoS.QoSClass.userInitiated,% d") I)} DispatchQueue.global (qos: DispatchQoS.QoSClass.userInteractive). Async {NSLog ("DispatchQoS.QoSClass.userInteractive,% d", I)} DispatchQueue.global (qos: DispatchQoS.QoSClass.utility). Async {NSLog ("DispatchQoS.QoSClass.utility,% d") I)}}
The highest priority userInteractive and the lowest background
Thank you for reading! This is the end of this article on "sample analysis of multithreading in iOS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.