What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?
2011-10-23 17:07
686 查看
Each program runs in at least one thread. You can think of each thread as a separate process of program execution, each running parallel to the others.
If you have some kind of user interface, or other code that needs to listen to events (like network ports), you need a run loop. Every NSThread automatically gets its own run loop, and you very rarely have to concern yourself with them directly.
The run loop is also in charge of creating and releasing autorelease pools.
[EDIT: See comments for more discussion about autorelease pools. The most important point to keep in mind is that new threads must take care of setting up an autorelease pool. For example, methods that are invoked with detachNewThreadSelector (see below) should
have the following as their first and last lines:
The same applies for threads spawned using other techniques.]
In the main thread, where all the UI handling is taking place, the run loop is very important, since it keeps the interface reactive. That's why you should never run code that's time consuming on the main thread: it will eat up all time on
the thread and the run loop will not be allowed to run often enough, resulting in a locked or slow interface. If you need to perform time consuming calculations, or keep a task running in the background, you should create a new thread. Again, you probably
don't have to think about the new run loop being created. An easy way of performing a method in a new thread:
Inter-thread communication can be tricky, and you should be aware of the methods
tips on sending NSNotifications across threads here.)
Timers are also handled by run loops. In contrast to run loops, you are likely to often be using timers directly in your program. The very easiest way of creating a timer is:
but sometimes you want to create and manage NSTimer objects yourself, for example to be able to cancel and re-use a timer.
An NSTask is used to run another program as a subprocess of the current one. It's a bit similar to starting a separate thread, but if a subprocess crashes, your main program will keep running. Communication between programs is also very different
from communication between several threads in the same process.
You tagged your question with "iphone", and on the iPhone you will never be using NSTasks.
NSOperations are used when you need to handle a larger amount of different tasks, placing them in queues and/or processing them in separate threads (although they don't have to run in separate threads). If your application needs to
create just a few, specialized threads, then there is no reason to use the NSOperation class. But if you will routinely generate tasks (like communicating with a server) that must be kept track of, NSOperation and NSOperationQueue will come in handy.
If you have some kind of user interface, or other code that needs to listen to events (like network ports), you need a run loop. Every NSThread automatically gets its own run loop, and you very rarely have to concern yourself with them directly.
The run loop is also in charge of creating and releasing autorelease pools.
[EDIT: See comments for more discussion about autorelease pools. The most important point to keep in mind is that new threads must take care of setting up an autorelease pool. For example, methods that are invoked with detachNewThreadSelector (see below) should
have the following as their first and last lines:
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ]; [code here] [pool release];
The same applies for threads spawned using other techniques.]
In the main thread, where all the UI handling is taking place, the run loop is very important, since it keeps the interface reactive. That's why you should never run code that's time consuming on the main thread: it will eat up all time on
the thread and the run loop will not be allowed to run often enough, resulting in a locked or slow interface. If you need to perform time consuming calculations, or keep a task running in the background, you should create a new thread. Again, you probably
don't have to think about the new run loop being created. An easy way of performing a method in a new thread:
[NSThread detachNewThreadSelector:@selector(theSelector) toTarget:self withObject:nil];
Inter-thread communication can be tricky, and you should be aware of the methods
performSelector:onThread:withObject:waitUntilDone:and
performSelectorOnMainThread:withObject:waitUntilDone:(Great
tips on sending NSNotifications across threads here.)
Timers are also handled by run loops. In contrast to run loops, you are likely to often be using timers directly in your program. The very easiest way of creating a timer is:
[self performSelector:@selector(aSelector) withObject:nil afterDelay:1.0];
but sometimes you want to create and manage NSTimer objects yourself, for example to be able to cancel and re-use a timer.
An NSTask is used to run another program as a subprocess of the current one. It's a bit similar to starting a separate thread, but if a subprocess crashes, your main program will keep running. Communication between programs is also very different
from communication between several threads in the same process.
You tagged your question with "iphone", and on the iPhone you will never be using NSTasks.
NSOperations are used when you need to handle a larger amount of different tasks, placing them in queues and/or processing them in separate threads (although they don't have to run in separate threads). If your application needs to
create just a few, specialized threads, then there is no reason to use the NSOperation class. But if you will routinely generate tasks (like communicating with a server) that must be kept track of, NSOperation and NSOperationQueue will come in handy.
相关文章推荐
- What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?
- What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop ?
- What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?
- Design: What is the basic difference between precast and prefabricated structures?
- Multi-thread: What is the difference between OpenMP and MPI
- Multi-thread: What is the difference between OpenMP and MPI
- What is the difference between a process and a thread?
- What is the difference between task and thread?
- What is the difference between a thread and a process?
- Multi-thread: What is the difference between OpenMP and MPI
- Multi-thread: What is the difference between OpenMP and MPI
- Multi-thread: What is the difference between OpenMP and MPI
- 进程与线程的区别 What is the difference between process and thread
- Multi-thread: What is the difference between OpenMP and MPI
- Multi-thread: What is the difference between OpenMP and MPI
- What is the difference between arguments and parameters?
- What is the difference between addView and addViewInLayout?
- What is the difference between a definition and a declaration?
- What is the difference between '/n' and '/r/n'? /n /r/n的区别是什么
- Linux Kernel: What is the major difference between the buffer cache and the page cache?