- 浏览: 395588 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (325)
- 神经网络 (1)
- javascript (11)
- 数据结构 (2)
- 计算机图形学 (11)
- 模式识别 (1)
- 前端开发 (14)
- 机器学习 (11)
- ios开发 (50)
- Python (9)
- HTML5 (4)
- 计算机视觉 (9)
- 数字图像处理 (7)
- 架构设计 (19)
- 数据库设计 (9)
- 算法设计 (59)
- Java (37)
- 其他 (3)
- 游戏开发 (5)
- c++ (17)
- Linux (3)
- TCP/IP (2)
- Flex (41)
- 健康 (6)
- AI (2)
- 工具 (1)
- 数据挖掘 (1)
- 性能优化 (6)
- 综合 (2)
- 网络通信 (12)
- Android (2)
- UML (3)
- 软件设计 (11)
- 编程经验 (7)
- J2EE (1)
- 多媒体技术 (3)
- 数学 (7)
- php (4)
- 设计 (1)
- CS (2)
- 计算机理论 (1)
- 信息安全 (1)
最新评论
-
ahead_zhan:
good good good
flex3控件_ModuleLoader -
lonerzf:
好样的。非常感谢楼主
OpenCV视频教程整理 -
lonerzf:
好样的。谢谢~
OpenCV视频教程整理 -
coding1688:
博主说的不错,我在实现瀑布流布局时也用的masonry插件,有 ...
Javascript 瀑布流式布局及其动态效果的实现 -
snowolf:
除非玩游戏,不然没啥win的事情,或者用win的银行客户端,通 ...
macbook安装操作系统的机理分析
转自:http://blog.csdn.net/zhangchu1994/article/details/7286018
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.
NSthread
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 methodsperformSelector:onThread:withObject:waitUntilDone: andperformSelectorOnMainThread:withObject:waitUntilDone:
(Great tips on sending NSNotifications across threads here.)
NSTimer(Timer是否可以理解为在指定时间后开一个线程)
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.
NSTask
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
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.
发表评论
-
IVEngine —— Objective-C的Spring
2013-12-15 20:35 0背景: 只要是Java程序员,相信都听说 ... -
XCode环境变量及路径设置
2013-11-17 16:05 1727转自:http://blog.csdn.net/freedo ... -
OVGap — 沟通 Objective-C 与 Javascript
2013-11-14 15:14 772Github 地址: https://github.com ... -
IOS开发常用命令
2013-10-11 10:41 597统计代码行数 打开terminal, cd到项目根目 ... -
提高在Xcode上的工作效率
2013-08-31 11:05 1052转自:http://liuminqian.github.io ... -
把ipa包上传到AppStore
2013-08-26 21:46 890转自:http://doc.appcan.cn/dev_io ... -
iPhone应用程序中App目录结构
2013-08-26 17:30 926转自:http://blog.csdn.net/justin ... -
iOS运行回路(RunLoop)总结
2013-08-05 13:37 715转自:http://www.cocoachina.com/i ... -
Simple iPhone Keychain Access
2013-08-04 15:54 699转自:http://useyourloaf.com/b ... -
IOS媒体层
2013-07-28 15:18 801转自:http://wd397.blog.163.com/b ... -
nginx上搭建HLS流媒体服务器
2013-07-28 13:31 1347转自:http://blog.csdn.net/ ... -
Compile ffmpeg for iOS 6, support Simulator & armv7 & armv7s
2013-07-23 17:03 770转自:http://witcheryne.iteye.co ... -
UITableView 修改编辑模式下的切图
2013-07-14 21:03 1122把以下代码放在点解 “编辑” 按钮时触发的方法中。 ... -
iOS平台UDID方案比较
2013-07-09 20:27 1475转自:http://www.cocoachina.com/b ... -
25 Free iOS Design Resources
2013-05-12 19:08 515Some great designers around th ... -
谈谈iOS Animation
2013-05-07 09:39 572转自:http://geeklu.com/2012/09/a ... -
IOS文件系统的管理
2013-05-06 17:11 588转自:http://blog.csdn.net/zhuzhi ... -
iOS应用的本地缓存机制设计
2013-05-06 16:18 634参考:http://www.cnblogs.com/peng ... -
iOS并发编程指南(3)
2013-04-28 14:37 706转自:http://mobile.51cto.com/iph ... -
iOS运行回路(RunLoop)总结
2013-04-27 23:24 644转自:http://www.cocoachina.com/i ...
相关推荐
介绍您可能已经知道, NSTimer , NSThread和CADisplayLink实例保留其目标。 如果目标也保留这些类之一的实例,则我们有一个保留周期:目标和实例都不会被释放。 您为什么要保留这些类之一的实例? 假设您有某种需要...
详细介绍CADisplayLink,CADisplayLink和NSTimer的区别。
详细讲解CADisplayLink和NSTimer的区别。
简单的nstimer时间计算器,上面有暂停,清零等简单的功能,免费下载
ios计时器NSTimer的使用 倒计时10秒,计时器 /* NSTimer scheduledTimerWithTimeInterval
NSTimer-Blocks, 在NSTimer上提供块功能,简单分类 自述文件NSTimer上非常简单的类别,它可以使用块。工作原理我认为如果你使用的是一个块,你可以能不需要将任何用户指定的对象传递到计时器。 你只要从街区里得到你...
ios NSTimer应用demo 。iphone6.1 测试通过。具体demo截图和说明在:http://blog.csdn.net/donny_zhang
NStimer 定时器 iOS 倒计时 objective-c
是一个关于NSTimer类的小计时器,主要是为了对比两个类方法创建定时器的区别,以及它们会受到滚动事件的影响,该如何解决这个问题
定时器
关于定时器在tableview上的复用问题
MKImpulse是一个用来代替系统NSTimer的高精度脉冲器 系统的NSTimer是添加到Runloop中的, 在系统繁忙时会造成偏差, 时间越长, 偏差越大 而MKImpulse是基于GCD编写的脉冲器, 精度由CPU时钟进行计算, 误差基本可以...
详细的NSTimer类的使用 文档说明 是开发的很好的帮助文档
常见NSTimer的消毁方法,用一种优雅的方式销毁NSTimer
模仿腾讯新闻图片频道的图片浏览的幻灯片效果.
iOS 中的 NSTimer.pdf
IOS应用源码——nstimer.zip
Swift:UIProgressView+NSTimer+UIstepper
我们都知道NSTimer采用target-action的方式,通常target又是类本身,我们为了方便又把NSTimer声明为属性变量,这样就难免会造成循环引用(需要反复执行计时任务时,如果是单次的任务就不会造成循环引用)。...