更加深刻的去理解,并发队列,串队列,栅栏队列,组队列
//
// ViewController.m
// GCD DEV
//
// Created by mac on 2017/2/21.
// Copyright © 2017年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self groupQueue];
}
/* 并发队列 **/
- (void)concurrentQueue {
//并发
dispatch_queue_t dispatch_queue = dispatch_queue_create("com.lee.dev", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(dispatch_queue, ^{
sleep(1);
NSLog(@"1");
});
dispatch_async(dispatch_queue, ^{
sleep(2);
NSLog(@"2");
});
/*
*作用在 并发队列中 ,相当于一个栅栏,只有等这个栅栏的东西执行完了才往下走
*但是注意的如果里面有一个 全局的异步线程
**/
dispatch_barrier_async(dispatch_queue, ^{
sleep(3);
NSLog(@"barrier");
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(3);
NSLog(@"global_queue");
});
});
dispatch_async(dispatch_queue, ^{
NSLog(@"3");
});
dispatch_async(dispatch_queue, ^{
NSLog(@"4");
});
}
/* 同步队列 **/
- (void)serialQueue {
//同步
dispatch_queue_t dispatch_queue = dispatch_queue_create("com.lee.dev", DISPATCH_QUEUE_SERIAL);
dispatch_async(dispatch_queue, ^{
sleep(3);
NSLog(@"同步:1");
});
dispatch_async(dispatch_queue, ^{
sleep(2);
NSLog(@"同步:2");
});
dispatch_async(dispatch_queue, ^{
NSLog(@"同步:3");
});
dispatch_async(dispatch_queue, ^{
NSLog(@"同步:4");
});
}
/*
* group 队列
**/
- (void)groupQueue {
NSArray *strings = @[@"http://img.wdjimg.com/image/video/d999011124c9ed55c2dd74e0ccee36ea_0_0.jpeg",
@"http://img.wdjimg.com/image/video/2ddcad6dcc38c5ca88614b7c5543199a_0_0.jpeg",
@"http://img.wdjimg.com/image/video/6d6ccfd79ee1deac2585150f40915c09_0_0.jpeg",
@"http://img.wdjimg.com/image/video/2111a863ea34825012b0c5c9dec71843_0_0.jpeg",
@"http://img.wdjimg.com/image/video/b4085a983cedd8a8b1e83ba2bd8ecdd8_0_0.jpeg",
@"http://img.wdjimg.com/image/video/2d59165e816151350a2b683b656a270a_0_0.jpeg",
@"http://img.wdjimg.com/image/video/dc2009ee59998039f795fbc7ac2f831f_0_0.jpeg"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSError * error = nil;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strings[0]] options:NSDataReadingMappedIfSafe error:&error];
NSLog(@"NSThread_1:error:%@;length:%ld",error,data.length);
});
dispatch_group_async(group, queue, ^{
NSError * error = nil;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strings[1]] options:NSDataReadingMappedIfSafe error:&error];
NSLog(@"NSThread_2:error:%@;length:%ld",error,data.length);
});
dispatch_group_async(group, queue, ^{
NSError * error = nil;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strings[2]] options:NSDataReadingMappedIfSafe error:&error];
NSLog(@"NSThread_3:error:%@;length:%ld",error,data.length);
});
/* 通知**/
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"UI Op");
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end