美文网首页
多线程(用于优化)

多线程(用于优化)

作者: Dove_Q | 来源:发表于2016-10-07 12:06 被阅读23次

    NSThread

    - (void)viewDidLoad {
        [super viewDidLoad];
        //在子线程上执行
        //第一种方法:
        [NSThread detachNewThreadSelector:@selector(demoFunc) toTarget:self withObject:nil];
        //第二种方法:
        [self performSelectorInBackground:@selector(demoFunc2) withObject:nil];
        //在主线程上执行
        //waitUntilDone: 等待执行完子线程里面的内容再执行后面的
        [self performSelectorOnMainThread:@selector(demoFunc) withObject:nil waitUntilDone:NO];
        
        NSLog(@"主线程");
    }
    
    - (void)demoFunc{
        NSLog(@"子线程");
    }
    
    - (void)demoFunc2{
        NSLog(@"第二子线程");
    }
    

    队列

    简单的添加线程
    注: 队列产生的线程不会根据任务总数来生成,一个线程可能会执行多个任务

        //新建队列
        NSOperationQueue *queue = [NSOperationQueue new];
        //任务
        NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"子线程1");
        }];
        //讲任务添加到队列
        [queue addOperation:blockOp];
        //在队列里直接添加任务
        [queue addOperationWithBlock:^{
            NSLog(@"子线程2");
            //将任务添加到主线程
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                NSLog(@"主线程2");
            }];
        }];
        NSLog(@"主线程1");
    

    设置线程最大个数和取消任务执行

        NSMutableArray *ops = [NSMutableArray array];
        for (int i = 0; i < 100; i++) {
            NSLog(@"------> %d", i);
            NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
                NSLog(@"i = %d", i);
            }];
            [ops addObject:op];
        }
        NSOperationQueue *queue = [NSOperationQueue new];
        //设置线程的最大个数,若为1就会一个个的依次执行
        queue.maxConcurrentOperationCount = 1;
        [queue addOperations:ops waitUntilFinished:NO];
        NSLog(@"主线程");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //队列里的任务执行取消
            [queue cancelAllOperations];
        });
    

    给线程添加依赖关系,使其有序

        NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"子线程1");
        }];
        NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"子线程2");
        }];
        NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"子线程3");
        }];
        NSOperationQueue *queue = [NSOperationQueue new];
        [queue addOperations:@[op1, op2, op3] waitUntilFinished:NO];
        //添加依赖关系,先执行op1然后执行op2再执行op3
        [op2 addDependency:op1];
        [op3 addDependency:op2];
        NSLog(@"主线程");
    

    C方法

    简单的主线程和子线程

        //获取主队列
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        //按照默认优先级获取子队列
        dispatch_queue_t subQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        //在主线程异步执行
        dispatch_async(mainQueue, ^{
            NSLog(@"主线程2");
        });
        //在子线程异步执行
        dispatch_async(subQueue, ^{
            NSLog(@"子线程");
        });
        NSLog(@"主线程1");
    
    
        dispatch_async(queue, ^{
            
            NSData *data = [NSData dataWithContentsOfURL:[NSURL url]];//....
            UIImage *image = [UIImage imageWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                //如果要给界面赋值,应该在主线程上操作
                [UIImageView new].image = image;
            });
        });
    

    自定义串行队列(serial)

        //自定义串行队列: 在同一个子线程依次执行
        dispatch_queue_t customQueue = dispatch_queue_create("xuqian.queue1", DISPATCH_QUEUE_SERIAL);
        for (int i = 0; i < 100; i++) {
            dispatch_async(customQueue, ^{
                NSLog(@"i = %d", i);
            });
        }
        NSLog(@"主线程");
    

    自定义并行队列(concurrent)

        dispatch_queue_t queue = dispatch_queue_create("franfkan.queu3", DISPATCH_QUEUE_CONCURRENT);
       dispatch_async(queue, ^{
           NSLog(@"1");
       });
        dispatch_async(queue, ^{     
            NSLog(@"2");
        });
        dispatch_async(queue, ^{      
            NSLog(@"3");
        });
        NSLog(@"done");
    

    死锁

        NSLog(@"hello world");
        //同步执行主队列,造成死锁
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"wellcome");
        });
        NSLog(@"=====");
    

    创建组队列, 执行完所有内容发出通知(子线程里执行网络请求)

        //自定义并行队列
        dispatch_queue_t customQueue = dispatch_queue_create("xuqian.queue", DISPATCH_QUEUE_CONCURRENT);
        //创建组
        dispatch_group_t group = dispatch_group_create();
        //创建信号量(用于子线程里执行网络请求)
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        dispatch_group_async(group, customQueue, ^{
            NSLog(@"1");
            dispatch_semaphore_signal(semaphore);
        });
        dispatch_group_async(group, customQueue, ^{
            NSLog(@"2");
            dispatch_semaphore_signal(semaphore);
        });
        dispatch_group_async(group, customQueue, ^{
            NSLog(@"3");
            dispatch_semaphore_signal(semaphore);
        });
        dispatch_group_async(group, customQueue, ^{
            NSLog(@"4");
            dispatch_semaphore_signal(semaphore);
        });
        //当组里面都执行完成以后(或者说当组为空时)然后在执行
        dispatch_group_notify(group, customQueue, ^{
            //四个请求对应四次信号等待
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            NSLog(@"done");
        });
    

    栅栏

        dispatch_async(queue, ^{
            NSLog(@"1");
        });
        dispatch_async(queue, ^{
            NSLog(@"2");
        });
        //拦截前面的,前面先执行完再执行后面的
        dispatch_barrier_async(queue, ^{
            NSLog(@"barrier");
        });
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        dispatch_async(queue, ^{
            NSLog(@"4");
        });
    

    相关文章

      网友评论

          本文标题:多线程(用于优化)

          本文链接:https://www.haomeiwen.com/subject/cbsyyttx.html