Status and notification of multithreading 3-NSThread in iOS
In OC, although there are not many actual scenarios for NSThread, if you can carefully understand the NSThread method when learning multithreading, it will be helpful to further study GCD and NSOperation. today, we will discuss some situations about the thread status: the state of a thread in the memory. NSThread provides three Processing Methods: cancel, sleep, and exit) below is a brief look at these situations: 1. -(void) cancel // cancel this method saves information about the currently executed process to the receiver, cancels the process, and reports the status through isCancled. if the process is canceled successfully, isCancled will return YES, otherwise NO; after the process is canceled, the exit method will be called; 2. + (void) sleepUntilDate :( NSDate *) aDate // The NSThread static method for sleep processes. It blocks the execution of the current process based on the given time (aDate, the underlying principle of its implementation is to block the running of the runloop of the process, so that the current process will not be able to get the execution opportunity; like this, there is also a static method: + (void) sleepForTimeInterval :( NSTimeInterval) ti, they use the same principle. 3. + (void) exit // exit the process. This method calls the NSThread class method currentThread to access the current process. This aims to enable the current process to send a notification (NSThreadWillExitNotification) to the notification center. this method can be called manually or automatically after the process ends normally. when you manually call this process, you must release the previously applied resources in advance; otherwise, memory leakage may occur. At the same time, NSThread also provides three methods to query the process execution status: 1. -(BOOL) isExecuting if the process is being executed, this method returns YES; otherwise, NO; 2. -(BOOL) isFinished: if the process ends, YES is returned; otherwise, NO is returned. 3. -(BOOL) isCancelled: if the process is canceled, YES is returned; otherwise, NO is returned. In OC, The NSThread status changes, the Message notification mechanism is used internally to notify the system, in addition, NSThread sends only three types of messages to the system's notification center: 1. NSDidBecomeSingleThreadedNotification has no practical significance at present. Apple only keeps this extended notification and does not trigger the message in the NSThread method. However, it can be understood Based on the literal meaning of the notification, this notification is sent when the process returns to a single thread. However, in a multi-threaded environment, there is no actual processing work, which can be ignored temporarily; 2. when the NSThreadWillExitNotification notification is sent to the notification center, it does not contain the userinfo dictionary information (friends who use the notification center should know the userinfo drop, so I will not explain it ), this notification is triggered when the process executes the exit method. you can listen to this notification to handle some things before the process ends (just like the last words ). 3. NSWillBecomeMultiThreadedNotification is triggered only once by NSThread, provided that the first process calls start or detachNewThreadSelector: toTarget: withObject: method. some Processing Methods of the notification recipient are performed on the main thread; this is because the notification was performed before the system produced a new sub-thread, so when listening for this notification, the notification method that calls the listener will be performed in the main thread. in fact, the basic methods and status notifications provided by NSThread can give us a general understanding of a process's lifecycle, and what status changes the process has taken in this lifecycle, and simple interaction between processes (using the notification center). This gives us a better understanding of the basic principles of their internal implementation when using GCD and NSOperate.