转自:http://stackoverflow.com/questions/1124207/what-is-the-basic-difference-between-nstimer-nstask-nsthread-and-nsrunloop
Each program runs in at least onethread. 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 arun 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 themain 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 methodsperformSelector:onThread:withObject:waitUntilDone:andperformSelectorOnMainThread:withObject:waitUntilDone:(Great tips on sending NSNotifications across threadshere.)
Timersare 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.
AnNSTaskis 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.
NSOperationsare 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'thaveto 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.
本文深入探讨了iOS应用开发中关键的并发概念:线程、RunLoop、Timer和Task,解释了它们之间的区别,并提供了如何在实际应用中正确使用它们的指导。
379

被折叠的 条评论
为什么被折叠?



