iOS多线程技术
来源:互联网 发布:apache linux ab 工具 编辑:程序博客网 时间:2024/05/17 09:10
1. NSObject多线程
[selfperformSelectorInBackground:@selector(intoBackground)withObject:nil];
[selfperformSelectorOnMainThread:@selector(intoForword)withObject:nilwaitUntilDone:YES];
2. NSThread多线程
1.[NSThread detachNewThreadSelector:@selector(intoBackThread:) toTarget:self withObject:imageView];
2. NSThread *thread =[[NSThreadalloc] initWithTarget:self selector:@selector(intoBackThread:)object:imageView];
[threadstart];
@autoreleasepool{}
回到主线程
[imageView performSelectorOnMainThread:@selector(setImage:)withObject:image waitUntilDone:YES];
3. NSOperation多线程
1.
// 利用队列设置同时并发的线程个数
[self.queuesetMaxConcurrentOperationCount:4];
for (UIImageView *imageViewin self.dataArray) {
NSInvocationOperation *operation = [[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(operationCilck:)object:imageView];
// 队列(一定要记住)
[self.queueaddOperation:operation];
}
2.
@autoreleasepool {
[self.queuesetMaxConcurrentOperationCount:4];
for (UIImageView *imageViewin self.dataArray) {
NSBlockOperation *blockOperation = [NSBlockOperationblockOperationWithBlock:^{
[NSThreadsleepForTimeInterval:1.0f];
// 在线程中作多线程加载资源
UIImage *image = [UIImageimageNamed:[NSStringstringWithFormat:@"NatGeo%02ld.png",(long)arc4random_uniform(17) + 1]];
// 得到UI线程
[[NSOperationQueuemainQueue] addOperationWithBlock:^{
[imageViewsetImage:image];
}];
}];
[self.queueaddOperation:blockOperation];
}
}
}
回到主线程
[imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
4. GCD多线程
// 队列初始化(全局队列)
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
// 同步:一步一步做 异步:同时做
for (UIImageView *imageViewin self.dataArray) {
// 进入多线程
dispatch_async(queue, ^{
NSLog(@"%@",[NSThreadcurrentThread]);
UIImage *image = [UIImageimageNamed:[NSStringstringWithFormat:@"NatGeo%02ld.png",(long)arc4random_uniform(17) + 1]];
dispatch_async(dispatch_get_main_queue(), ^{
[imageViewsetImage:image];
});
});
}
}
- ios学习--多线程技术
- ios 多线程技术简介
- iOS多线程编程技术
- iOS多线程技术
- iOS多线程技术
- iOS 多线程技术1
- iOS 多线程技术2
- ios多线程技术
- iOS多线程技术
- iOS多线程技术
- iOS多线程技术
- iOS多线程技术
- iOS多线程开发技术
- IOS 多线程技术
- iOS多线程技术-上
- IOS 多线程技术
- IOS多线程技术详解
- iOS多线程编程技术
- 编译mkv库,libmatroska-1.4.4.tar.bz2
- require.js 模态框上利用defind加载js
- android优化之软引用和弱引用
- Objective-C中的@dynamic
- 蒙特卡洛求圆周率PI
- iOS多线程技术
- struts2 - View页面中获取Action的成员变量
- java第四天
- java泛型
- EXTJS4中更改了ext:qtip的支持
- java对时间操作的常用总结
- linux sed命令详解
- 实例对象和字典的相互转化
- uva 1451 Average 平均值