Each program runs in at least one thread (called main thread). You can think that each thread is an independent processor, and each thread runs in parallel. Thread is used to execute multiple code in parallel in the same process.
If your application only has one thread, the thread needs to complete all the tasks of the application. It must respond to events, update application windows, and complete all operations on application results. In this case, the problem arises. There is only one thread and it can only do one thing at a time. What will happen if your application has an operation that takes a long time? Your code is busy calculating the results it needs, and your application stops responding to user events and updating Windows. If this operation takes a long time, the user may think that the application has lost the response and shut it down. If you move the operation to another thread
Thread is only used for Event Response and update Windows. This is why Code that has been continuously consumed (congested) should not be run in main thread.
A Method for running functions in the new thread:
[NSThread detachNewThreadSelector:@selector(theSelector) toTarget:self withObject:nil];
If you have some user interfaces or other code that needs to listen to events (such as network interfaces), you need to use the run loop. Each thread creates its own run loop, which we usually do not need to care about. Run loop is also responsible for the creation and release of autorelease pool.
For each newly created thread, you need to create an autorelease pool for it. If you have a method to call detachnewthreadselector, you need to use the following autorelease pool code to include it:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];[code here][pool release];
Run loop (cfrunloop) is an event processing loop. You can arrange your work and coordinate the events you receive. The purpose of running loop is to keep your thread busy when processing an event and sleep when no event is processed.
Run loop is a set of architectures used to manage asynchronous messages received by threads. When the run loop is running, it monitors one or more event sources in the thread. When an event arrives, the system wakes up the thread and sends the event to the run loop. Then, the run loop forwards the event to the specified handler again. If no event is received or the event processing is complete, run loop sets the thread to sleep. You may not set the run loop for the thread you created, but it will provide a good user experience. Run
Loop provides the possibility of creating threads that run for a long time and consume a small amount of resources. Because run loop sets the thread to sleep if no event is received. To configure the run loop, all you need to do is to create your thread, get reference to the run loop object, install your event processor, and run the run loop. Cocoa automatically configures the run loop for you in the main thread. If you want to create another long-running thread, you must configure the run loop yourself.
Like its name, the run loop is a loop in your thread and processes the received events. Your code provides control statements to execute the run loop-in other words, your code provides a while or for loop to drive the run loop. In your loop, you use the run loop object to "run" the event processing code. The event processing code mainly receives events and calls the event processing function.
Run loop receives messages from two different event sources. Input sources (cfrunloopsource) Ships asynchronous messages, usually from another thread or another application. Timer sources (cfrunlooptimer) delivers synchronous messages within the scheduled time or repetition interval. Both event sources use the processing method specified by the application to process the events that arrive. Shows the run loop and different event source structures.
Input sources ships asynchronous messages to the corresponding processor and causes the exit of rununitdate: method (called when the run loop is associated with the thread. Timer sources delivers the synchronous Message to the corresponding processor but does not cause the run loop to exit. In order to perform additional processing on the input sources, run loops can also generate corresponding notifications Based on the run loop behavior. Registering run loop observers (cfrunloopobserver) can receive these notifications and use them to perform additional processing on the thread.
Your application does not need to create or display the managed nsunloop object. Every nsthread object, including the main thread of the application, will automatically create a nsunloop object. If you need to access the run loop of the current thread, you can use the class method of the nsunloop object currentrunloop.
Timer (timer) is also processed through run loop. Compared with run loop, you usually use timer directly in your program.
The simplest way to create a timer is as follows:
[self performSelector:@selector(aSelector) withObject:nil afterDelay:1.0];
In some cases, you may also create your own nstmer object so that you can release and reuse timer on your own.