iOS多线程队列同步异步操作

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //正确认识“当前线程”
    dispatch_queue_t queue = dispatch_queue_create("ThirdConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"并行队列异步执行开始%@", [NSThread currentThread]);
        //在同步执行任务
        //当前线程为子线程
        dispatch_sync(queue, ^{
            NSLog(@"XXXXX:%@", [NSThread currentThread]);
        });
    });
    
}

//串行队列同步执行
- (IBAction)serialQueueSync:(id)sender {
    /*明确点:任务的执行顺序; 主线程还是子线程执行
    串行队列: 顺序地执行
    同步执行: 当前线程 + 等待任务执行完毕
     */
    //1.创建串行队列(给定名字+指定队列类型)
    NSLog(@"开始执行啦:%@", [NSThread currentThread]);
    dispatch_queue_t queue = dispatch_queue_create("FirstSerialQueue", DISPATCH_QUEUE_SERIAL);
    //2.添加两个任务到串行队列中(block)
    //3.同步执行两个任务
    dispatch_sync(queue, ^{
        //添加第一个任务(耗时操作)
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++++%@",[NSThread currentThread]);
        }
    });
    NSLog(@"打印+结束");
 
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"----------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"打印—结束");
}
//串行队列异步执行
- (IBAction)serialQueueAsync:(id)sender {
    /*
     串行队列: 顺序地执行
     异步执行: 子线程 + 立即返回(不等待任务)
     */
    dispatch_queue_t queue = dispatch_queue_create("SecondSerialQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        //添加到队列中的任务
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"+++++++++%@", [NSThread currentThread]);
        }
    });
    NSLog(@"打印+完毕");
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"-------%@",[NSThread currentThread]);
        }
    });
    NSLog(@"打印-完毕");
}
//并行队列同步执行
- (IBAction)concurrentQueueSync:(id)sender {
    /*
    并行队列: 同时地执行
    同步执行: 当前线程 + 等待任务执行完毕
     */
    dispatch_queue_t queue = dispatch_queue_create("FirstConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++%@",[NSThread currentThread]);
        }
    });
    NSLog(@"打印+完毕");
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"--------%@",[NSThread currentThread]);
        }
    });
    NSLog(@"打印-完毕");
}
//并行队列异步执行
- (IBAction)concurrentQueueAsync:(id)sender {
    /*
     并行队列: 同时地执行
     异步执行: 子线程 + 立即返回(不等待任务)
     */
    dispatch_queue_t queue = dispatch_queue_create("SecondConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"++++++++%@", [NSThread currentThread]);
        }
    });
    NSLog(@"打印+完毕");
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"--------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"打印-完毕");
}

//全局队列异步执行(掌握)
- (IBAction)globalQueueAsync:(id)sender {
    //结论和并行队列异步执行一样
    //1.获取全局队列(只有这一步不一样)
    /* 参数一:指定全局队列的优先级(主队列优先级最高)
     */
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //2.添加任务
    dispatch_async(queue, ^{
        NSLog(@"++++++%@", [NSThread currentThread]);
    });
    //3.异步执行任务
    NSLog(@"打印+完毕");
}
//主队列异步执行
- (IBAction)mainQueueAsync:(id)sender {
    //1.获取主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    //2.添加到主队列
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"+++++++++%@", [NSThread currentThread]);
        }
    });
    NSLog(@"打印+完毕");
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"---------%@", [NSThread currentThread]);
        }
    });
    NSLog(@"打印-完毕");
}
//主队列同步执行
- (IBAction)mainQueueSync:(id)sender {
//    dispatch_queue_t queue = dispatch_get_main_queue();
    NSLog(@"任务一");
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"任务二");
    });
    NSLog(@"任务三");
}

你可能感兴趣的:(iOS多线程队列同步异步操作)