iOS 多线程 GCD的代码测试

2018-04-26  本文已影响17人  开心的小赵

前言

iOS讲多线程的例子很多,我就不再多说了,我主要是用代码去测试,下面是我的代码。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self tongbuchuanxing];
//    [self tongbubingxing];
//    [self yibuchuanxing];
//    [self yibubingxing];
//    [self groupTest];
//    [self yibuchuangxingshunxuzhixing];
    [self yibubingxingshunxuzhixing];
    
}

- (void)tongbuchuanxing{
    // 创建队列
    dispatch_queue_t queue = dispatch_queue_create("zq.test.com", DISPATCH_QUEUE_SERIAL);
    
    //主线程执行
    dispatch_sync(queue, ^{
        NSLog(@"1");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"2");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"3");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    
    //直接获取主线程队列,崩溃
    //个人理解应该主队列正在在执行任务的时候,然后给主队列又添加了新的任务,导致崩溃
    //说明主队列不能同时进行访问
//    NSLog(@"%@",dispatch_get_main_queue());
//    dispatch_sync(dispatch_get_main_queue(), ^{
//        NSLog(@"4");
//        NSLog(@"线程:%@",[NSThread currentThread]);
//    });
    
    
    //开辟新线程去执行同步任务
    [NSThread detachNewThreadWithBlock:^{
        
        dispatch_sync(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"5");
            NSLog(@"线程:%@",[NSThread currentThread]);
        });
        dispatch_sync(dispatch_get_global_queue(0, 0), ^{
            NSLog(@"6");
            NSLog(@"线程:%@",[NSThread currentThread]);
        });
        
        //这里在其他线程去获取主线,在主线程中添加任务,说明刚才我的猜想是正确的
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"7");
            NSLog(@"线程:%@",[NSThread currentThread]);
        });
    }];
}

- (void)tongbubingxing{
    
    //获取全局队列(全局队列是一个并行队列)
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"1");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"2");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    
    //创建一个并行队列
    dispatch_queue_t queue = dispatch_queue_create("zq.test.com", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        NSLog(@"3");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"4");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    
    //由此可知,在同步中无论是串行队列还是并行队列都在主线程中执行
}

- (void)yibuchuanxing{
    //1. 异步会开辟新线程么?
    //2. 主队列是一个串行队列么?是的
    
    dispatch_queue_t queue = dispatch_queue_create("zq.test.com", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        NSLog(@"1");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"2");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"3");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"4");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    //结论 异步会开辟新线程去执行任务
    
    
    
    //到主线程中去执行了
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"5");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"6");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    
}

- (void)yibubingxing{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"1");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"2");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    //这里的线程是主线程
//    dispatch_barrier_sync(dispatch_get_global_queue(0, 0), ^{
//        NSLog(@"围栏");
//        NSLog(@"线程:%@",[NSThread currentThread]);
//    });
    
    dispatch_barrier_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"围栏");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    //前面的执行完成后才能执行后面,那可以添加多个么? 不能添加多个  只能隔断一次
    //这里的线程是线程
    dispatch_barrier_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"围栏");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"4");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    //异步并且得到的顺序就有点乱了,而且 每次都会开辟一个线程
    //1. 如何让他变得有序起来
}

- (void)groupTest{
    //主要作用就是通知任务完成了 该做些什么了
    
    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, globalQueue, ^{
        NSLog(@"1");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_group_async(group, globalQueue, ^{
        NSLog(@"2");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_group_async(group, globalQueue, ^{
        NSLog(@"3");
        NSLog(@"线程:%@",[NSThread currentThread]);
    });
    dispatch_group_notify(group, globalQueue, ^{
        NSLog(@"任务全部完成");
    });
}
- (void)yibuchuangxingshunxuzhixing{
    //异步串行队列,结果是按顺序执行的 开辟了一个线程
    dispatch_queue_t queue = dispatch_queue_create("zq.test.com", NULL);
    for (int i = 0; i <6; i++) {
        dispatch_async(queue, ^{
            NSLog(@"%d",i);
            NSLog(@"线程:%@",[NSThread currentThread]);
        });
    }
    
}

- (void)yibubingxingshunxuzhixing{
//    NSLock *lock = [NSLock new];
//    for (int i = 0; i <6; i++) {
//        dispatch_async(dispatch_get_global_queue(0, 0), ^{
//            // 神奇的事情发生了   已经是顺序执行了
//            [lock lock];
//            NSLog(@"%d",i);
//            NSLog(@"线程:%@",[NSThread currentThread]);
//            [lock unlock];
//        });
//
//    }
    
    //如果网络请求本身就是异步并发的呢,我们想要的结果顺序
    //1.信号量  可以解决
    dispatch_semaphore_t xinhaoliang = dispatch_semaphore_create(0);
    for (int i = 0; i <10; i++) {
        dispatch_semaphore_signal(xinhaoliang);
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            //这里也相当于加锁,不明白的话自行百度下
            @synchronized(self){
                dispatch_semaphore_wait(xinhaoliang, DISPATCH_TIME_FOREVER);
                NSLog(@"%d",i);
                NSLog(@"线程:%@",[NSThread currentThread]);
            }
        });
    }
}
@end

写在后面

其中的代码可以自己去测试,我注释的也是我做测试用的。代码中我已经写了一些注释,不明白的地方欢迎留言。

上一篇 下一篇

猜你喜欢

热点阅读