#import
"ViewController.h"
@interface
ViewController
()
@end
@implementation
ViewController
-
(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 <</span> 5;
i++) {
[NSThread
sleepForTimeInterval:1];
NSLog(@"++++++++++%@",[NSThread
currentThread]);
}
});
NSLog(@"打印+结束");
dispatch_sync(queue,
^{
for
(int
i
= 0;
i <</span> 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 <</span> 5;
i++) {
[NSThread
sleepForTimeInterval:1];
NSLog(@"+++++++++%@",
[NSThread
currentThread]);
}
});
NSLog(@"打印+完毕");
dispatch_async(queue,
^{
for
(int
i
= 0;
i <</span> 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 <</span> 5;
i++) {
[NSThread
sleepForTimeInterval:1];
NSLog(@"++++++++%@",[NSThread
currentThread]);
}
});
NSLog(@"打印+完毕");
dispatch_sync(queue,
^{
for
(int
i
= 0;
i <</span> 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 <</span> 5;
i++) {
[NSThread
sleepForTimeInterval:1];
NSLog(@"++++++++%@",
[NSThread
currentThread]);
}
});
NSLog(@"打印+完毕");
dispatch_async(queue,
^{
for
(int
i
= 0;
i <</span> 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 <</span> 5;
i++) {
[NSThread
sleepForTimeInterval:1];
NSLog(@"+++++++++%@",
[NSThread
currentThread]);
}
});
NSLog(@"打印+完毕");
dispatch_async(queue,
^{
for
(int
i
= 0;
i <</span> 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(@"任务三");
}
-
(void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any
resources that can be recreated.
}
@end