对多线程的理解,IOS中有几种实现多线程的方法
2016-08-23 18:53
363 查看
1、好处:
1、使用线程可以把程序中占据时间长的任务放到后台去处理,如图片、视频的下载
2、发挥多核处理器的优势,并发执行让系统运行的更快、更流畅,用户体验更好
缺点:
1、大量的线程降低代码的可读性,
2、更多的线程需要更多的内存空间
3、当多个线程对同一个资源出现争夺的时候要注意线程安全的问题。
iOS有三种多线程编程的技术:
1、NSThread(两种创建方式)
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];
NSThread *myThread = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
[myThread start];
2、NSOperationQueue
NSOperationQueue*oprationQueue= [[NSOperationQueuealloc] init];
oprationQueueaddOperationWithBlock:^{
//这个block语句块在子线程中执行
}
http://alloc.sinaapp.com/wp/?p=237
3、Grand Central Dispatch (GCD)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//耗时的操作
dispatch_async(dispatch_get_main_queue(), ^{
//更新界面
});
});
http://blog.csdn.net/totogo2010/article/details/8016129
PS:不显示的创建线程的方法:
用NSObject的类方法performSelectorInBackground:withObject:创建一个线程:[Obj performSelectorInBackground:@selector(doSomething) withObject:nil];
文/一首白城(简书作者)
原文链接:http://www.jianshu.com/p/522c37d817de
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
1、使用线程可以把程序中占据时间长的任务放到后台去处理,如图片、视频的下载
2、发挥多核处理器的优势,并发执行让系统运行的更快、更流畅,用户体验更好
缺点:
1、大量的线程降低代码的可读性,
2、更多的线程需要更多的内存空间
3、当多个线程对同一个资源出现争夺的时候要注意线程安全的问题。
iOS有三种多线程编程的技术:
1、NSThread(两种创建方式)
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];
NSThread *myThread = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
[myThread start];
2、NSOperationQueue
NSOperationQueue*oprationQueue= [[NSOperationQueuealloc] init];
oprationQueueaddOperationWithBlock:^{
//这个block语句块在子线程中执行
}
http://alloc.sinaapp.com/wp/?p=237
3、Grand Central Dispatch (GCD)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//耗时的操作
dispatch_async(dispatch_get_main_queue(), ^{
//更新界面
});
});
http://blog.csdn.net/totogo2010/article/details/8016129
PS:不显示的创建线程的方法:
用NSObject的类方法performSelectorInBackground:withObject:创建一个线程:[Obj performSelectorInBackground:@selector(doSomething) withObject:nil];
文/一首白城(简书作者)
原文链接:http://www.jianshu.com/p/522c37d817de
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- 谈谈你对多线程开发的理解?ios中有几种实现多线程的方法?
- 对多线程开发的理解,以及几种实现方法
- ios中的几种多线程实现
- ios多线程的几种方法
- 多线程的几种实现方法详解
- 黑马程序员——java多线程的几种实现方法及多窗口售票小程序
- 多线程有几种实现方法?同步有几种实现方法?
- 多线程实现的几种常用的方法
- 【最近面试遇到的一些问题】多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么
- 多线程的几种实现方法
- 多线程有几种实现方法
- 多线程有几种表示方法,都是什么?同步有几种实现方法,都是什么?
- 多线程有几种实现方法?同步有几种实现方法?静态同步方法和非静态有何不同?—— Java经典面试题系列
- 多线程的几种实现方法详解
- 多线程的几种实现方法详解
- 多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么?
- ios中的几种多线程实现
- 多线程有几种实现方法,都是什么?同步有几种实现方法,都是什么?
- ios中的几种多线程实现
- 实现KMP算法的几种理解方法