5, multi-threaded basis
inter-thread communication What is called inter-thread communication in a process, threads often do not exist in isolation, and multiple threads need to communicate between threads frequently 1 threads pass data to another thread after performing a specific task in 1 threads,
- The embodiment of inter-thread communication
- 1 threads to pass data to a different 1 a thread
- in the 1 after performing a specific task in a thread, go to another 1 threads continue to perform tasks
Common methods for inter-thread communication perform execute selector selector- (void)Performselectoronmainthread:(SEL)AselectorWithobject:(ID)ArgWaituntildone:(BOOL) wait;- (void)Performselector:(SEL)AselectorOnthread:(Nsthread*)THRWithobject:(ID)ArgWaituntildone:(BOOL) wait;The above two methods are object methods, and objects that inherit nsobject can be used. Code (for example, to load a picture on a network) Here first to a network image download address, pretty HD pictures OH: URL:http://g.hiphotos.baidu.com/zhidao/pic/item/42166d224f4a20a4884b622491529822730ed0f8.jpgBefore hitting the code, add a setting in the Info.plist project file because since Xcode 7.0, it is necessary to manually add the Settings app Transport Security settings to the Info.plist file from the network. Operations that do not use multithreading below we need to take some steps to get the time of the execution of a piece of code
- The first of these methods
Then cut to the chase and send the message to the main thread in the child threads, also two methods:
Note that the above code uses the Nsthread sleep method to get it asleep for 3 seconds.
About this third method, you can look at my summary, and then basically can be achieved without looking at the source code to quickly self-knock out:
Multithreading Basics (V)