NSTimer,NSTask,NSThread和NSRunloop之间的基本区别是什么? [英] What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?

查看:121
本文介绍了NSTimer,NSTask,NSThread和NSRunloop之间的基本区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSTimer NSTask NSThread

解决方案 div>

每个程序至少在一个线程中运行。您可以将每个线程视为一个单独的程序执行过程,每个线程都与其他线程并行运行。



如果您有某种用户界面或需要其他代码要监听事件(如网络端口),您需要一个运行循环。每个NSThread自动获得自己的运行循环,你很少需要直接关心自己。运行循环还负责创建和释放自动释放池。



init];
[code here]
[pool release];

这同样适用于使用其他技术生成的线程。]



主线程中,所有UI处理正在进行,运行循环非常重要,因为它保持接口无效。这就是为什么你不应该运行在主线程上耗时的代码:它将在线程上消耗掉所有时间,并且运行循环将不允许运行足够频繁,导致锁定或缓慢的接口。如果需要执行耗时的计算,或保持任务在后台运行,则应创建一个新线程。同样,你可能不需要考虑创建的新的运行循环。在新线程中执行方法的简单方法:

  [NSThread detachNewThreadSelector:@selector(theSelector)toTarget:self withObject:零]; 

线程间通信可能很棘手,您应该注意方法 performSelector:onThread:withObject:waitUntilDone: performSelectorOnMainThread:withObject:waitUntilDone:
href =http://www.cocoadev.com/index.pl?NotificationsAcrossThreads =nofollow noreferrer>此处。)



计时器也由运行循环处理。与运行循环相反,您可能经常在程序中直接使用定时器。创建计时器的最简单的方法是:

  [self performSelector:@selector(aSelector)withObject:nil afterDelay:1.0] ; 

但有时您想自己创建和管理NSTimer对象,例如, - 使用计时器。



NSTask 用于运行另一个程序作为当前子进程的子进程。它有点类似于启动一个单独的线程,但如果一个子进程崩溃,你的主程序将继续运行。程序之间的通信与同一进程中的几个线程之间的通信非常不同。



你用iphone标记了你的问题,在iPhone上你永远不会使用Nstasks。



NSOperations 用于当您需要处理大量不同的任务,将它们放入队列和/或单独处理线程(虽然它们不在单独的线程中运行)。如果你的应用程序需要创建几个,专门的线程,那么没有理由使用NSOperation类。但是,如果你将常规生成必须跟踪的任务(如与服务器通信),NSOperation和NSOperationQueue将派上用场。


What is the difference between NSTimer, NSTask, NSThread and NSRunloop and is there a guideline on when to use each of them?

解决方案

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:

   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.

这篇关于NSTimer,NSTask,NSThread和NSRunloop之间的基本区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆