随笔分类 - 03B 多线程
摘要:参考:http://blog.csdn.net/totogo2010/article/details/8010231
阅读全文
摘要:GCD全称为Grand Central Dispatch 在IOS4才开始,用来实现多线程。它是IOS多线程抽象层次最高的一层,下面还有更加轻量级的Cocoa operations,和Thread。 当看到GCD的使用我仿佛看到了Android中的Handler和AsynTask。[cpp] view plaincopy 在主线程中任意方法中加入: dispatch_async(dispatch_get_gloabal_queue(0, 0), ^{ //加入耗时操作 //...... dispatch_async...
阅读全文
摘要:iOS 支持多个层次的多线程编程,层次越高的抽象程度越高,使用起来也越方便,也是苹果最推荐使用的方法。下面根据抽象层次从低到高依次列出iOS所支持的多线程编程范式:1, Thread;2, Cocoa operations;3, Grand Central Dispatch (GCD) (iOS4 才开始支持)下面简要说明这三种不同范式:Thread 是这三种范式里面相对轻量级的,但也是使用起来最负责的,你需要自己管理thread的生命周期,线程之间的同步。线程共享同一应用程序的部分内存空间,它们拥有对数据相同的访问权限。你得协调多个线程对同一数据的访问,一般做法是在访问之前加锁,这会导致一定
阅读全文
摘要:为线程设置一个名字 [mythread setName:@"第一个子线程"];
阅读全文
摘要:// 初始化锁对象ticketCondition = [[NSCondition alloc] init];//开始第一个线程。ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];[ticketsThreadone setName:@"Thread-1"];[ticketsThreadone start];//开始第二个线程。ticketsThreadtwo = [[NSThread alloc] initWithTarget:self se
阅读全文
摘要:GCD(Grand Central Dispatch)是一个大的主题。它可以提高代码的执行效率与多核的利用率。是苹果的开源项目,如果你是一个追求软件效率的开发人员,这门技术你一定要有所研究。 今天要介绍的是如何从网上下载大量的文件, 方法有许多,传通的就是用多线程,面iOS中有很好用的Operator Queeu, 或者用传通的NStrhead, pthread. 今天我不介绍这些方法,而是得用GCD来完成这个下载任务。 今天这个demo, 展示面几个功能: 1. 预定义了大量的要下载的图片URL2. 创建一个GCD queue并开始下载图片3. 在动画出现消失的时候,同时显示图片 主要代码.
阅读全文
摘要:1。GCD之dispatch queuehttp://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html2。iOS中GCD的魔力http://blog.csdn.net/favormm/article/details/64532603。官方 ,内容真的很多http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.htmlhttp://developer.app
阅读全文
摘要:IOS中,如果要在主线程中启动一个子线程,可以又两种方法:[cpp][NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil];这是在cocoa早期提供的方法,因此你可以在任何版本的ios和mac上调用此方法。在 OS X v10.5(or later)和IOS中,苹果又提供了一种方法,可以允许你获得你的thread句柄,并且更方便的让主线程控制子线程。[cpp]NSThread* myThread = [[NSThread alloc] initWithTarge
阅读全文
摘要:NSLog(@"main thread:%@",[NSThread currentThread]);
阅读全文
摘要:[cpp]view plaincopy-(IBAction)startThreadButtonPressed:(UIButton*)sender{threadStartButton.hidden=YES;threadValueLabel.text=@"0";threadProgressView.progress=0.0;//新的线程[NSThreaddetachNewThreadSelector:@selector(startTheBackgroundJob)toTarget:selfwithObject:nil];}创建新的线程,线程的函数为 startTheBackgr
阅读全文
摘要:在使用多线程时,要注意:有些view使用子线程获得数据,但是view上的按钮却在主线程上。所以,当使用子线程时,要适当控制view上按钮的可用性。否则会出现页面错版。
阅读全文
摘要:线程创建与启动NSThread的创建主要有两种直接方式:[NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil];和NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThreadMainMethod:) object:nil];[myThread start];这两种方式的区别是:...
阅读全文
浙公网安备 33010602011771号