IOS三种多线程编程的技术

来源:互联网 发布:c语言接口 结构 编辑:程序博客网 时间:2024/05/22 04:47
 •NSThread: –优点:NSThread 比其他两个轻量级,使用简单 –缺点:需要自己管理线程的生命周期、线程同步、加锁、睡眠以及唤醒等。线程同步对数据的加锁会有一定的系统开销
    1    [NSThread detachNewThreadSelector:@selector(dosth:) toTarget:self withObject:nil];    2    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(dosth:) object:nil];    [thread start];
 •NSOperation: –不需要关心线程管理,数据同步的事情,可以把精力放在自己需要执行的操作上 –NSOperation是面向对象的
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];    [operationQueue addOperationWithBlock:^{    }];
 •GCD: –Grand Central Dispatch是由苹果开发的一个多核编程的解决方案。iOS4.0+才能使用,是替代NSThread, NSOperation的高效和强大的技术 –GCD是基于C语言的
    dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);    dispatch_async(concurrentQueue, ^(){    });
1 0
原创粉丝点击