iOS开发之多线程的五种方法
2016-01-14 17:39
656 查看
#import "ViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//多线程第一种
NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(ThreadOne:)object:nil];
[thread start];
//多线程第二种
[NSThreaddetachNewThreadSelector:@selector(ThreadTwo:)toTarget:selfwithObject:nil];
//多线程第三种
[selfperformSelectorInBackground:@selector(ThreadThree:)withObject:nil];
//多线程第四种
NSOperationQueue *myQueue = [[NSOperationQueuealloc]init];
NSInvocationOperation *invocationOne = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(QueueOne:)object:nil];
NSInvocationOperation *invocationTwo = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(QueueTwo:)object:nil];
[myQueue addOperation:invocationOne];
[myQueue addOperation:invocationTwo];
invocationOne.queuePriority =NSOperationQueuePriorityVeryHigh;
//优先级
//多线程第五种
dispatch_queue_t queue =dispatch_queue_create("test",NULL);
dispatch_async(queue, ^{
NSLog(@"这里进行请求数据操作");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"这里加载界面");
});
});
}
-(IBAction)ThreadOne:(id)sender{
}
-(IBAction)ThreadTwo:(id)sender{
}
-(IBAction)ThreadThree:(id)sender{
}
-(IBAction)QueueOne:(id)sender{
}
-(IBAction)QueueTwo:(id)sender{
}
@end
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//多线程第一种
NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(ThreadOne:)object:nil];
[thread start];
//多线程第二种
[NSThreaddetachNewThreadSelector:@selector(ThreadTwo:)toTarget:selfwithObject:nil];
//多线程第三种
[selfperformSelectorInBackground:@selector(ThreadThree:)withObject:nil];
//多线程第四种
NSOperationQueue *myQueue = [[NSOperationQueuealloc]init];
NSInvocationOperation *invocationOne = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(QueueOne:)object:nil];
NSInvocationOperation *invocationTwo = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(QueueTwo:)object:nil];
[myQueue addOperation:invocationOne];
[myQueue addOperation:invocationTwo];
invocationOne.queuePriority =NSOperationQueuePriorityVeryHigh;
//优先级
//多线程第五种
dispatch_queue_t queue =dispatch_queue_create("test",NULL);
dispatch_async(queue, ^{
NSLog(@"这里进行请求数据操作");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"这里加载界面");
});
});
}
-(IBAction)ThreadOne:(id)sender{
}
-(IBAction)ThreadTwo:(id)sender{
}
-(IBAction)ThreadThree:(id)sender{
}
-(IBAction)QueueOne:(id)sender{
}
-(IBAction)QueueTwo:(id)sender{
}
@end
相关文章推荐
- Python3写爬虫(四)多线程实现数据爬取
- C#实现多线程的同步方法实例分析
- 浅谈chuck-lua中的多线程
- C#简单多线程同步和优先权用法实例
- C#多线程学习之(四)使用线程池进行多线程的自动管理
- C#多线程编程中的锁系统(三)
- C#多线程学习之(六)互斥对象用法实例
- 基于一个应用程序多线程误用的分析详解
- C#多线程学习之(三)生产者和消费者用法分析
- C#多线程学习之(一)多线程的相关概念分析
- C#多线程之Thread中Thread.IsAlive属性用法分析
- 分享我在工作中遇到的多线程下导致RCW无法释放的问题
- C#多线程编程之使用ReaderWriterLock类实现多用户读与单用户写同步的方法
- C#控制台下测试多线程的方法
- Ruby 多线程的潜力和弱点分析
- C#中WPF使用多线程调用窗体组件的方法
- C#如何对多线程、多任务管理(demo)
- C#实现多线程的Web代理服务器实例
- c#实现多线程局域网聊天系统
- PHP使用CURL实现多线程抓取网页