1NSThread.声明线程,启动线程:(第一个参数声明了目标类,第2个参数声明了目标方法,第3个参数为该方法的参数)
NSThread *thread=[[NSThread alloc] initWithTarget:selfselector:@selector(saleTicketMethod:) object:@"线程--1"];
[thread start];
2.IOS跟Android一样都是线程不安全的 也就是所有UI更新都必须在主线程内完成,这样,当我们在另一条线程中想要修改View,如下:
(第一个参数为目标方法,第2个参数为该方法的参数 ,并且只能是1个 。)
[self performSelectorOnMainThread:@selector(updateView:) withObject:str waitUntilDone:YES];
3.当不同的线程抢夺共同的资源时,需要对线程加锁,以下为一个卖票的系统例子:
#import "CSZViewController.h"
@interface CSZViewController ()
{
int _ticketNum;
NSLock *_lock;
}
@end
@implementation CSZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void) updateView:(NSString *) text
{
NSString *str=[NSString stringWithFormat:@"%@ \n%@",self.textView.text,text];
self.textView.text=str;
}
- (void)saleTicketMethod:(NSString *)threadName
{
while (true) {
if (_ticketNum>0) {
[_lock lock];
NSString *str=[NSString stringWithFormat:@"%@线程的票数为:%d",threadName,_ticketNum];
[self performSelectorOnMainThread:@selector(updateView:) withObject:str waitUntilDone:YES];
_ticketNum--;
[_lock unlock];
if ([threadName isEqualToString:@"线程--1"]) {
[NSThread sleepForTimeInterval:1.0];
}else{
[NSThread sleepForTimeInterval:2.0];
}
}else
{
NSString *str=[NSString stringWithFormat:@"售票结束!%@",threadName];
[self performSelectorOnMainThread:@selector(updateView:) withObject:str waitUntilDone:YES];
break;
}
}
}
- (IBAction)threadClick:(id)sender {
_ticketNum=20;
//计算剩余票数
//如果有票,则卖出
//没有则停止;
_lock=[[NSLock alloc] init];
NSThread *thread=[[NSThread alloc] initWithTarget:self selector:@selector(saleTicketMethod:) object:@"线程--1"];
NSThread *thread1=[[NSThread alloc] initWithTarget:self selector:@selector(saleTicketMethod:) object:@"线程--2"];
[thread start];
[thread1 start];
}
@end
---NSOperation的使用是苹果为我们实现多线程通过的一套简洁的API。它为我们避免抢夺同个资源做了屏蔽。同时定义了线程队列的概念,开发人员不用考虑这方面的东西。
其用法主要分为3点:
1,定义线程队列 (设置同时运行的线程数,因为开线程也是需要消费资源的,类比JAVA的线程池)
NSOperationQueue *queue=[[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:5];
2.定义异步线程 。(同样,以下参数也是声明了线程方法所在的类以及所需要的方法参数)
NSInvocationOperation *opera=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(operaSaleMethod:) object:@"线程操作1"];
NSInvocationOperation *opera2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(operaSaleMethod:) object:@"线程操作2"];
3.将队列放进线程中。线程任务就会通过队列自动分配。
[queue addOperation:opera];
[queue addOperation:opera2];
4.对于操作的方法,使用同一资源,不需要做加锁的处理
-(void) operaSaleMethod:(NSString *)threadName
{
while (true) {
if (_ticketNum>0) {
if ([threadName isEqualToString:@"线程操作1"]) {
[NSThread sleepForTimeInterval:2.0];
}else{
[NSThread sleepForTimeInterval:1.2];
}
NSString *str=[NSString stringWithFormat:@"%@线程的票数为:%d",threadName,_ticketNum];
[self performSelectorOnMainThread:@selector(updateView:) withObject:str waitUntilDone:YES];
_ticketNum--;
}else
{
NSString *str=[NSString stringWithFormat:@"%@售票结束!",threadName];
[self performSelectorOnMainThread:@selector(updateView:) withObject:str waitUntilDone:YES];
break;
}
}
}
GCD是用C语言写的支持多线程开发的一套 API,它的模式跟Operation差不多,并在此基础上增加了组的概念,在所有组完成后提供一套触发通知的机制。
1.定义队列
//#define DISPATCH_QUEUE_PRIORITY_HIGH 2
//#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
//#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
//#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
2.定义组
dispatch_group_t group=dispatch_group_create();
3.定义异步任务并添加到队列中
dispatch_group_async(group, queue, ^{
[self gcdSaleMethod:@"线程操作1"];
});
dispatch_group_async(group, queue, ^{
[self gcdSaleMethod:@"线程操作2"];
});
4.队列获取通知
dispatch_group_notify(group, queue, ^{
NSLog(@"线程操作完成");
});
从日志可以看出 只有所有线程跑完了 才会回调该block中的内容
2014-08-11 15:08:38.982 ThreadSample01[5256:4007] 线程操作2售票结束!
2014-08-11 15:08:39.037 ThreadSample01[5256:1903] 线程操作1售票结束!
2014-08-11 15:08:39.042 ThreadSample01[5256:4007] 线程操作完成
分享到:
相关推荐
iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
多线程加载图片
关于iOS 多线程编程知识点,iOS 多线程 NSThread NSOperation GCD
这里详细介绍了iOS多线程 NSThread,GCD,NSOperation的使用。看我就够了!
在iOS开发中,多线程的实现方式主要有三种,NSThread、NSOperation和GCD,我前面博客中对NSOperation和GCD有了较为详细的实现,为了学习的完整性,今天我们主要从代码层面来实现NSThread的使用。案例代码上传至 ...
2、Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的使用)3、GCD 全称:Grand Central Dispatch( iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用) 这三种编程方式从上到下,抽象度...
ios多线程开发的常用四种方式和基本使用,pthread、NSThread、NSOperation\NSOperationQueue、GCD
leetcode 答案 iOS- 在网上找的一些iOS面试资料 1、2018 iOS面试题---UI相关:事件传递,...iOS面试题-----多线程相关之NSOperation、NSOperationQueue、NSThread+runloop实现常驻线程、加锁 10、2019 iOS面试题-----R
对iOS的多线程方式做的总结;Pthreads,NSThread,GCD,NSOperation/NSOperationQueue
IOS多线程编程NSThread的使用方法 NSThread是多线程的一种,有两种方法创建子线程 (1)优点:NSThread 比GCD、NSOperation都轻量级 (2)缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有...
NSThread, NSOperation, Blocks/GCD, pthread都是多线程操作函数 常用 NSThread detachNewThreadSelector NSOperation是在网络上用的多写 继承NSOperation 实现了main函数 Blocks是也是工作线程 5.请描述ios...
iOS多线程编程,GCD的使用,NSOperation的使用,NSThread的使用,异步下载,同步下载,block的使用,ASIhttprequest类库的使用
#iOS的三种多线程技术 和特性 1.NSThread 2.NSOperation/NSOperationQueue 3.GCD (大中心调度)
1.、NSThread2、CocoaNSOperation(IOS多线程编程之NSOperation和NSOperationQueue的使用)3、GCD全称:GrandCentralDispatch(IOS多线程编程之Grand CentralDispatch(GCD)介绍和使用)这三种编程方式从上到下,抽象...
在iOS开发中,实现多线程的的方式有很多种,常用的有pthread、NSThread、GCD与NSOperation。在实际应用中,多线程技术也有着非常多的使用场景,比如异步的网络请求、异步的图片加载、后台执行复杂任务等等。 在iOS...
我们务必要把耗时的操作放到别的线程中去执行,千万不要阻塞主线程。 iOS中有以下3种多线程编程方法: NSThread Grand Centeral Dispatch(GCD) NSOperation和NSOperationQueue 1.NSThread 这是最轻量级的多线程的...
9.2、多线程之NSThread 9.3、多线程之GCD 9.4、多线程之NSOperation 9.5、多线程之线程安全 9.6、死锁 9.7、线程之间的通讯 9.8、GCD高级用法 9.9、深入浅出 iOS 并发编程 10、内存管理 10.1、定
给大家介绍在IOS中为什么使用多线程及多线程实现的三种方法,基本上使用这三种方法实现多线程(NSThread Grand Centeral Dispatch(GCD) NSOperation和NSOperationQueue),感兴趣的朋友可以参考下本