您的位置:首页 > 移动开发 > IOS开发

IOS-线程操作之NSThread/NSOperation/GCD

2014-08-11 14:34 176 查看
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,定义线程队列 (设置同时运行的线程数,因为开线程也是需要消费资源的,类比J***A的线程池)

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] 线程操作完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: