GCD....使用

来源:互联网 发布:资海网络集团电话 编辑:程序博客网 时间:2024/06/08 09:20
////  ViewController.m//  GCDDemo////  Created by 赵伟争 on 2016/11/9.//  Copyright © 2016年 zwz. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //异步执行 + 并行队列    [self asyncConcurrent];    //异步执行 + 串行队列    [self asyncSerial];    //同步执行 + 并行队列    [self syncConcurrent];    //同步执行+ 串行队列    [self syncSerial];}//异步执行 + 并行队列- (void)asyncConcurrent {    //创建一个并行队列    dispatch_queue_t queue = dispatch_queue_create("com.dispatch.concurrent", DISPATCH_QUEUE_CONCURRENT);//生成一个并发执行队列,block被分发到多个线程去执行 concurrent 并发    NSLog(@"------start------");    //使用异步函数封装三个任务    dispatch_async(queue, ^{        NSLog(@"任务1--- %@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"任务2--- %@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"任务3--- %@", [NSThread currentThread]);    });    NSLog(@"----end-----");}//异步执行 + 串行队列- (void)asyncSerial {    //创建一个串行队列    dispatch_queue_t queue = dispatch_queue_create("com.dispatch.serial", DISPATCH_QUEUE_SERIAL); // serial 串行    NSLog(@"---start---");    //使用异步函数封装三个任务    dispatch_async(queue, ^{        NSLog(@"任务1---%@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"任务2---%@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"任务3---%@", [NSThread currentThread]);    });    NSLog(@"---end---");}//同步执行 + 并行队列- (void)syncConcurrent {    dispatch_queue_t queue = dispatch_queue_create("com.nodispatch.concurrent", DISPATCH_QUEUE_CONCURRENT);    NSLog(@"---start---");    //使用同步函数封装三个任务    dispatch_sync(queue, ^{        NSLog(@"任务1---%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"任务2---%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"任务3---%@", [NSThread currentThread]);    });    NSLog(@"---end---");}//同步执行+ 串行队列- (void)syncSerial{    dispatch_queue_t queue = dispatch_queue_create("com.nodispatch.serial", DISPATCH_QUEUE_SERIAL);    NSLog(@"---start---");    //使用异步函数封装三个任务    dispatch_sync(queue, ^{        NSLog(@"任务1---%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"任务2---%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"任务3---%@", [NSThread currentThread]);    });    NSLog(@"---end---");}//异步执行+主队列- (void)asyncMain{    //获取主队列    dispatch_queue_t queue = dispatch_get_main_queue();    NSLog(@"---start---");    //使用异步函数封装三个任务    dispatch_async(queue, ^{        NSLog(@"任务1---%@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"任务2---%@", [NSThread currentThread]);    });    dispatch_async(queue, ^{        NSLog(@"任务3---%@", [NSThread currentThread]);    });    NSLog(@"---end---");}//同步执行+主队列(死锁)- (void)syncMain{    //获取主队列    dispatch_queue_t queue = dispatch_get_main_queue();    NSLog(@"---start---");    //使用同步函数封装三个任务    dispatch_sync(queue, ^{        NSLog(@"任务1---%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"任务2---%@", [NSThread currentThread]);    });    dispatch_sync(queue, ^{        NSLog(@"任务3---%@", [NSThread currentThread]);    });    NSLog(@"---end---");}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
0 0
原创粉丝点击