#import "ViewController.h"
#import "XMGPerson.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
/*
XMGPerson *p1 = [[XMGPerson alloc]init];
NSLog(@"%@",p1.books);
XMGPerson *p2 = [[XMGPerson alloc]init];
NSLog(@"%@",p2.books);
*/
[self barrier];
}
//一次性代码:整个程序运行过程中只会执行一次
/*不能放在懒加载里面的*/
-(void)once
{
NSLog(@"-----");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"----once----");
});
}
//延迟执行
-(void)afterDelay
{
NSLog(@"-----start----");
// NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
// [self performSelector:@selector(run) withObject:nil afterDelay:3.0];
dispatch_queue_t queue = dispatch_get_main_queue();
// dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
/*
GCD里面的时间是以纳秒为单位的 1秒=10的九次方纳秒
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)),queue , ^{
NSLog(@"---block---%@",[NSThread currentThread]);
});
}
//栅栏函数:控制并发队列里面任务的执行顺序
/*不能使用全局并发队列*/
-(void)barrier
{
//1.创建并发队列
dispatch_queue_t queue = dispatch_queue_create("xmg", DISPATCH_QUEUE_CONCURRENT);
//2.异步函数
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download1--%zd--%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download2--%zd--%@",i,[NSThread currentThread]);
}
});
//3.使用栅栏函数
dispatch_barrier_async(queue, ^{
NSLog(@"+++++++++++++++++barrier+++++++++++");
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download3--%zd--%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (NSInteger i = 0; i<10; i++) {
NSLog(@"download4--%zd--%@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
NSLog(@"download5----%@",[NSThread currentThread]);
});
}
-(void)run
{
NSLog(@"---run---");
}
@end