iOS 线程的基本介绍

来源:互联网 发布:数据库是什么软件 编辑:程序博客网 时间:2024/06/07 06:47

一个应用至少要有一个或多个进程,进程活跃状态说明当前软件正在运行
一个进程在手机里一般就代表一个应用
一个进程要执行很多线程(任务),一个线程一般就代表一个任务或多个任务

  • NSThread: 轻量级的多线程
    缺点: 需要我们手动管理线程的生命周期,线程锁,数据同步等事情,而线程锁会对系统有一定的开销

  • NSOperation: 特点是不需要我们操心线程管理和数据同步等事情.执行效率不是特别高

  • GCD:(grand,central,dispatch)特点:非常强大的多线程,充分利用系统的多核,底层是C语言,使用起来非常方便,非常高效,是苹果在iOS4时推出来的,目前到ios9了,非常成熟,是用来替代前两个多线程的

    NSThread

- (IBAction)threadBtn:(UIButton *)sender {    //创建一个多线程(方式1)    //NSThread *theard = [[NSThread alloc] initWithTarget:self selector:@selector(cycle) object:nil];    //开始执行线程任务    [theard start];    //开辟子线程方式2    [NSThread detachNewThreadSelector:@selector(cycle) toTarget:self withObject:nil];}- (void)cycle{    for (int i = 0; i < 1000; i++) {        NSLog(@"====== %d", i);    }    NSLog(@"是否为主线程 = %d", [NSThread isMainThread]);    //回到主线程    [self performSelectorOnMainThread:@selector(goBackToMain) withObject:nil waitUntilDone:YES];}- (void)goBackToMain{    NSLog(@" goBackToMain 是否为主线程 = %d", [NSThread isMainThread]);}

NSOperation

- (IBAction)operationBtn:(UIButton *)sender {//    NSOperation  是一个抽象类,我们通常用它的两个子类来创建线程    //第一个子类创建线程    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(cycle) object:nil];    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(cycleTwo) object:nil];    //第二个子类    NSBlockOperation *blockOper = [NSBlockOperation blockOperationWithBlock:^{        for (int i = 0; i < 1000; i++) {            NSLog(@"blockOper ^^^^^^^^^^^^^^^^^^^^^^^^ %d", i);        }    }];    //线程队列//    NSOperationQueue 队列主要是用来执行任务的,会将系统闲置的队列拿来使用,节省系统资源    //队列一般分为串行队列和并行队列    //串行队列特点:一个一个执行任务,第一个执行完在执行第二个    //并行队列:多个任务一起执行,谁先执行完不一定    //注意:串行队列可以在主线程上完成,也可以在子线程上进行    //但并行队列只能在子线程上进行    //创建队列,默认是子线程队列    //前者任务等待后者执行完才执行(实现串行)    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    //设置最大并发(同时)执行的任务数量    [queue setMaxConcurrentOperationCount:1];    //设置队列任务依赖关系    [blockOper addDependency:operation1];    //添加任务到队列中    [queue addOperation:operation1];    [queue addOperation:operation2];    [queue addOperation:blockOper];    NSBlockOperation *block2 = [NSBlockOperation blockOperationWithBlock:^{        NSLog(@"block2是否在主线程 = %d", [NSThread isMainThread]);    }];    //回到主线程    [[NSOperationQueue mainQueue] addOperation:block2];    //此时blockOper任务是在主线程上执行}- (void)cycle{    for (int i = 0; i < 1000; i++) {        NSLog(@"====== %d", i);    }    NSLog(@"是否为主线程 = %d", [NSThread isMainThread]);    //回到主线程    [self performSelectorOnMainThread:@selector(goBackToMain) withObject:nil waitUntilDone:YES];}- (void)cycleTwo{    for (int i = 0; i < 1000; i++) {        NSLog(@" >>>>>>>>>>>>> %d", i + 2000);    }}

GCD

- (IBAction)GCDBtn:(UIButton *)sender {    //GCD分串行和并行    //串行队列    //调用系统主线程实现串行队列    /*    //mainQueue是主线程队列    dispatch_queue_t mainQueue = dispatch_get_main_queue();    //执行任务    //参数1:设置队列    dispatch_async(mainQueue, ^{        NSLog(@"是否在主线程1 = %d", [NSThread isMainThread]);//这是在主线程执行的    });    dispatch_async(mainQueue, ^{        NSLog(@"是否在主线程2 = %d", [NSThread isMainThread]);    });    dispatch_async(mainQueue, ^{        NSLog(@"是否在主线程3 = %d", [NSThread isMainThread]);    });     */    //自定义串行队列    //参数1:队列标识符    //参数2:设置队列类型    /*    dispatch_queue_t serialQueue = dispatch_queue_create("com.lanou3g.GCD.serial", DISPATCH_QUEUE_SERIAL);    //执行队列任务    dispatch_async(serialQueue, ^{        NSLog(@"是否在主线程1 = %d", [NSThread isMainThread]);//这是在子线程执行的    });    dispatch_async(serialQueue, ^{        NSLog(@"是否在主线程2 = %d", [NSThread isMainThread]);    });    dispatch_async(serialQueue, ^{        NSLog(@"是否在主线程3 = %d", [NSThread isMainThread]);    });    dispatch_async(serialQueue, ^{        NSLog(@"是否在主线程4 = %d", [NSThread isMainThread]);    });    dispatch_async(serialQueue, ^{        NSLog(@"是否在主线程5 = %d", [NSThread isMainThread]);    });    dispatch_async(serialQueue, ^{        NSLog(@"是否在主线程6 = %d", [NSThread isMainThread]);    });     */    //系统并行队列    //获得全局队列    //参数1:设置队列优先级别    //参数2:系统预留参数设置为0    /*    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_async(globalQueue, ^{        NSLog(@"是否在主线程1 = %d", [NSThread isMainThread]);    });    dispatch_async(globalQueue, ^{        NSLog(@"是否在主线程2 = %d", [NSThread isMainThread]);    });    dispatch_async(globalQueue, ^{        NSLog(@"是否在主线程3 = %d", [NSThread isMainThread]);    });    dispatch_async(globalQueue, ^{        NSLog(@"是否在主线程4 = %d", [NSThread isMainThread]);    });    dispatch_async(globalQueue, ^{        NSLog(@"是否在主线程5 = %d", [NSThread isMainThread]);    });    dispatch_async(globalQueue, ^{        NSLog(@"是否在主线程6 = %d", [NSThread isMainThread]);    });    dispatch_async(globalQueue, ^{        NSLog(@"是否在主线程7 = %d", [NSThread isMainThread]);    });    */    //在并行线程里下载图片//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//    });    /*    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_async(globalQueue, ^{        NSURL *url = [[NSURL alloc] initWithString:@"http://img3.douban.com/view/site/small/public/6510ea4ea1cc8e6.jpg"];        NSData *data = [NSData dataWithContentsOfURL:url];        UIImage *image = [UIImage imageWithData:data];        //回到主线程        dispatch_queue_t mainQueue = dispatch_get_main_queue();        dispatch_async(mainQueue, ^{            //刷新页面            self.myImageView.image = image;        });    });     */    //设置多少秒后执行    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        NSLog(@"打水吧");    });    //block里的代码只执行一次(用这个创建单例)    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        NSLog(@"我只走一次");    });}
0 0
原创粉丝点击