ios 线程

iOS有三种多线程编程的技术,分别是:
一  NSThread
二  Cocoa NSOperation
三  GCD(全称:Grand Central Dispatch)

这三种编程方式从上到下,抽象度层次是从低到高的,抽象度越高的使用越简单,也是Apple最推荐使用的。

三种方式的优缺点介绍:
1)NSThread:
优点:NSThread 比其他两个轻量级
缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销

Cocoa NSOperation
优点:不需要关心线程管理,数据同步的事情,可以把精力放在自己需要执行的操作上。
Cocoa operation 相关的类是 NSOperation ,NSOperationQueue。
NSOperation是个抽象类,使用它必须用它的子类,可以实现它或者使用它定义好的两个子类:NSInvocationOperation 和 NSBlockOperation。
创建NSOperation子类的对象,把对象添加到NSOperationQueue队列里执行。

GCD
Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法。在iOS4.0开始之后才能使用。GCD是一个替代诸如NSThread, NSOperationQueue, NSInvocationOperation等技术的很高效和强大的技术。现在的iOS系统都升级到7了,所以不用担心该技术不能使用。

ios多线程的几种创建方式

转自:http://my.oschina.net/u/936286/blog/159245

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
     
    //创建线程的第一种方式
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"universe"];
    [thread start];
    [thread release];
     
     
    //创建线程的第二种方式,NSThread类方法
    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"yuzhou"];
     
     
    //创建线程的第三种方法  NSObject方法
    [self performSelectorInBackground:@selector(run:) withObject:@"nsobject thread"];
     
    //创建线程的第四种方式
    NSOperationQueue *oprationQueue = [[NSOperationQueue alloc] init];
    [oprationQueue addOperationWithBlock:^{
        //这个block语句块在子线程中执行
        NSLog(@"oprationQueue");
    }];
    [oprationQueue release];
     
    //第五种创建线程的方式
    NSOperationQueue *oprationQueue1 = [[NSOperationQueue alloc] init];
    oprationQueue1.maxConcurrentOperationCount = 1;//指定池子的并发数
     
    //NSOperation 相当于java中的runnable接口,继承自它的就相当一个任务
    NSInvocationOperation *invation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run:) object:@"invation"];
    [oprationQueue1 addOperation:invation];//将任务添加到池子里面,可以给池子添加多个任务,并且指定它的并发数
    [invation release];
     
    //第二个任务
    NSInvocationOperation *invation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2:) object:@"invocation2"];
    invation2.queuePriority = NSOperationQueuePriorityHigh;//设置线程优先级
    [oprationQueue1 addOperation:invation2];
    [invation2 release];
     
    [oprationQueue1 release];
     
    //调用主线程,用来子线程和主线程交互,最后面的那个boolean参数,如果为yes就是等这个方法执行完了在执行后面的代码;如果为no的话,就是不管这个方法执行完了没有,接着往下走
    [self performSelectorOnMainThread:@selector(onMain) withObject:self waitUntilDone:YES];
     
    //---------------------GCD----------------------支持多核,高效率的多线程技术
    //创建多线程第六种方式
    dispatch_queue_t queue = dispatch_queue_create("name", NULL);
    //创建一个子线程
    dispatch_async(queue, ^{
        // 子线程code... ..
        NSLog(@"GCD多线程");
         
        //回到主线程
        dispatch_sync(dispatch_get_main_queue(), ^{//其实这个也是在子线程中执行的,只是把它放到了主线程的队列中
            Boolean isMain = [NSThread isMainThread];
            if (isMain) {
                NSLog(@"GCD主线程");
            }
        });
    });
     
     
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
 
- (void)onMain
{
    Boolean b = [NSThread isMainThread];
    if (b) {
        NSLog(@"onMain;;%d",b);
    }
}
 
 
- (void) run:(NSString*)str
{
    NSLog(@"多线程运行:::%@",str);
}
- (void) run2:(NSString*)str
{
    NSLog(@"多线程运行:::%@",str);
}

资料1.http://mobile.51cto.com/iphone-403490.htm

原文地址:https://www.cnblogs.com/feng-a/p/4267054.html