各队列同步异步操作

51 篇文章 0 订阅
- (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(@"任务三");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值