GCD多线程

来源:互联网 发布:8寸windows平板电脑 编辑:程序博客网 时间:2024/05/20 09:10
//串行队列,线性同步
- (IBAction)handleSyncQueue:(UIButton*)sender {
    //1.获取串行队列
    //(1)获取系统创建好的串行队列,主队列,在主线程中执行
    //dispatch_queue_t queue = dispatch_get_main_queue();
    //(2)自己创建串行队列,在子线程中执行
    dispatch_queue_t queue =dispatch_queue_create("com.lanou3g.wang",DISPATCH_QUEUE_SERIAL);
    //2.往队列中添加任务
    dispatch_async(queue, ^{
        NSLog(@"任务一,当前线程%@", [NSThreadcurrentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务二,当前线程%@", [NSThreadcurrentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务三,当前线程%@", [NSThreadcurrentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务四,当前线程%@", [NSThreadcurrentThread]);
    });
}
//并行队列,线程并发
- (IBAction)handleConcurrentQueue:(UIButton*)sender {
    //1.获取并行队列
    //(1)获取系统创建好的并行队列
    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    //2.往队列中添加任务
    dispatch_async(queue, ^{
        NSLog(@"任务一,当前进程%@", [NSThreadcurrentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务二,当前进程%@", [NSThreadcurrentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务三,当前进程%@", [NSThreadcurrentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务四,当前进程%@", [NSThreadcurrentThread]);
    });
}
//分组任务
- (IBAction)handleGroup:(UIButton*)sender {
    //1.获取并发队列
    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    //2.创建分组
    dispatch_group_t group =dispatch_group_create();
    //往队列中添加分组
    dispatch_group_async(group, queue, ^{
        NSLog(@"请求0~1M数据,当前线程%@", [NSThreadcurrentThread]);
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"请求1~2M数据,当前线程%@", [NSThreadcurrentThread]);
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"请求2~3M数据,当前线程%@", [NSThreadcurrentThread]);
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"请求3~4M数据,当前线程%@", [NSThreadcurrentThread]);
    });
}
//整个程序运行期间,只执行一次
- (IBAction)handleOnce:(UIButton*)sender {
    staticdispatch_once_tonceToken;
    dispatch_once(&onceToken, ^{
       //存放只会执行一次的代码,比如单例对象的创建.
    });
}
//障碍
- (IBAction)handleBarrier:(UIButton*)sender {
    //1.获取并发队列
    //dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //如果要加障碍,必须要使用自己创建的队列
    //自己创建并行队列
    dispatch_queue_t queue =dispatch_queue_create("com.lanou3g.zhang",DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"A,写入");
    });
    dispatch_async(queue, ^{
        NSLog(@"B,写入");
    });
    dispatch_async(queue, ^{
        NSLog(@"C,写入");
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"障碍,读取数据");
    });
    dispatch_async(queue, ^{
        NSLog(@"D,写入");
    });
    dispatch_async(queue, ^{
        NSLog(@"E,写入");
    });
    dispatch_async(queue, ^{
        NSLog(@"F,写入");
    });

}
//延迟
- (IBAction)handleDelay:(UIButton*)sender {
    double second = 10.0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(second *NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
        //存放延迟10.0之后的任务
        NSLog(@"存放延迟10.0之后的任务");
    });
}
//重复执行的任务
- (IBAction)handleApply:(id)sender {
    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    NSArray *arr = @[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff",@"gg"];
    //如果每次只执行一个任务,则在主线程中执行,如果并发执行多次,主线程执行之外,还会分配子线程
    dispatch_apply([arrcount], queue, ^(size_tindex) {
        NSLog(@"任务%lu,当前线程%@", index +1, [NSThreadcurrentThread]);
        NSLog(@"%@", arr[index]);
    });
   
}
原创粉丝点击