- (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(@"任务三");
}
iOS多线程队列同步异步操作
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...