What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop ?

来源:互联网 发布:ecshop 修改sql语句 编辑:程序博客网 时间:2024/04/30 02:16


 (转)http://www.devdiv.com/forum.php?mod=viewthread&tid=38980&extra=page%3D1%26filter%3Dtypeid%26typeid%3D34%26typeid%3D34

 

今天由于看了[复杂问题] 最近在研究多线程,求NSRunLoop和NSThread 使用sample中Vincent的Demo,对NSRunLoop不太了解,就在网上找了找.在stackoverflow上的帖子,现在发出来给大家看看,希望对大家有帮助.
源地址:
What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop ?

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:
  1.   NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
  2.    [code here]
  3.    [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:
  1. [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:
  1. [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.

帖子中没有具体说NSRunLoop,我就又Google了一回.
原文地址

A run loop receives events from two different types of sources. Input sources deliver asynchronous events, usually messages from another thread or from a different application.Timer sources deliver synchronous events, occurring at a scheduled time or repeating interval. Both types of source use an application-specific handler routine to process the event when it arrives.
循环只接受2种来源的事件,一个是输入源,也就是异步的,通常是别的线程,或别的应用程序(我如果没有记错iphone是单进程的,我想这说的是mac机吧),一个是定时器源,这个是同步事件.

shows the conceptual structure of a run loop and a variety of sources. The input sources deliver asynchronous events to the corresponding handlers and cause therunUntilDate: method (called on the thread’s associated NSRunLoop object) to exit. Timer sources deliver events to their handler routines but do not cause the run loop to exit.
异步输入源事件的传入,导致runUntilDate: 函数的推出;而定时器源就不会导致runUntilDate: 的退出.
(我当时就是不明白,Vincent的demo中为什么改变一个值就推出循环了,现在只是可以解释通了.希望大家有具体明白的,给我解释下,谢谢了)

循环和事件源的结构图: