多线程举例
举例一:
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
[self performSelector:@selector(test) withObject:nil afterDelay:.0f];
NSLog(@"3---%@",[NSThread currentThread]);
});
}
-(void)test{
NSLog(@"%s",__func__);
}
ProjectTwo[2606:77536] 1---<NSThread: 0x600001b68380>{number = 7, name = (null)}
ProjectTwo[2606:77536] 3---<NSThread: 0x600001b68380>{number = 7, name = (null)}
举例二:
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_sync(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
[self performSelector:@selector(test) withObject:nil afterDelay:.0f];
NSLog(@"3---%@",[NSThread currentThread]);
});
}
-(void)test{
NSLog(@"%s",__func__);
}
ProjectTwo[2697:81240] 1---<NSThread: 0x600003858880>{number = 1, name = main}
ProjectTwo[2697:81240] 3---<NSThread: 0x600003858880>{number = 1, name = main}
ProjectTwo[2697:81240] -[ViewController test]
举例三:
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
NSLog(@"1---%@",[NSThread currentThread]);
[self performSelector:@selector(test) withObject:nil];
NSLog(@"3---%@",[NSThread currentThread]);
});
}
-(void)test{
NSLog(@"%s",__func__);
}
ProjectTwo[2724:82458] 1---<NSThread: 0x600001f47cc0>{number = 7, name = (null)}
ProjectTwo[2724:82458] -[ViewController test]
ProjectTwo[2724:82458] 3---<NSThread: 0x600001f47cc0>{number = 7, name = (null)}
举例四:
- (void)viewDidLoad {
[super viewDidLoad];
NSThread *thread = [[NSThread alloc] initWithBlock:^{
NSLog(@"1---%@",[NSThread currentThread]);
}];
[threadstart];
[self performSelector:@selector(test) onThread:thread withObject:nil waitUntilDone:YES];
}
-(void)test{
NSLog(@"%s",__func__);
}
ProjectTwo[2765:84753] 1---<NSThread: 0x600001031300>{number = 7, name = (null)}
ProjectTwo[2765:84554] *** Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[ViewController performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform'
举例五:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_sync(queue, ^{
NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);
});
NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);
}
ProjectTwo[2785:85589] 执行任务1--<NSThread: 0x6000009ec900>{number = 1, name = main}
(lldb)
举例六:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);
});
NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);
}
ProjectTwo[2825:87240] 执行任务1--<NSThread: 0x6000014d0240>{number = 1, name = main}
ProjectTwo[2825:87240] 执行任务3--<NSThread: 0x6000014d0240>{number = 1, name = main}
ProjectTwo[2825:87240] 执行任务2--<NSThread: 0x6000014d0240>{number = 1, name = main}
举例七:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);
dispatch_queue_t queue = dispatch_queue_create("myqueu", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);
dispatch_sync(queue, ^{
NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);
});
NSLog(@"执行任务4--%@",[NSThreadcurrentThread]);
});
NSLog(@"执行任务5--%@",[NSThreadcurrentThread]);
}
ProjectTwo[2845:88258] 执行任务1--<NSThread: 0x600002dac700>{number = 1, name = main}
ProjectTwo[2845:88347] 执行任务2--<NSThread: 0x600002de11c0>{number = 4, name = (null)}
ProjectTwo[2845:88258] 执行任务5--<NSThread: 0x600002dac700>{number = 1, name = main}
(lldb)
举例八:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);
dispatch_queue_t queue = dispatch_queue_create("myqueu", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue2 = dispatch_queue_create("myqueu2", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);
dispatch_sync(queue2, ^{
NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);
});
NSLog(@"执行任务4--%@",[NSThreadcurrentThread]);
});
NSLog(@"执行任务5--%@",[NSThreadcurrentThread]);
}
ProjectTwo[2867:89315] 执行任务1--<NSThread: 0x600001310480>{number = 1, name = main}
ProjectTwo[2867:89417] 执行任务2--<NSThread: 0x600001350d80>{number = 4, name = (null)}
ProjectTwo[2867:89315] 执行任务5--<NSThread: 0x600001310480>{number = 1, name = main}
ProjectTwo[2867:89417] 执行任务3--<NSThread: 0x600001350d80>{number = 4, name = (null)}
ProjectTwo[2867:89417] 执行任务4--<NSThread: 0x600001350d80>{number = 4, name = (null)}
举例九:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"执行任务1--%@",[NSThreadcurrentThread]);
dispatch_queue_t queue = dispatch_queue_create("myqueu", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"执行任务2--%@",[NSThreadcurrentThread]);
dispatch_sync(queue, ^{
NSLog(@"执行任务3--%@",[NSThreadcurrentThread]);
});
NSLog(@"执行任务4--%@",[NSThreadcurrentThread]);
});
NSLog(@"执行任务5--%@",[NSThreadcurrentThread]);
}
ProjectTwo[2891:90412] 执行任务1--<NSThread: 0x600002420100>{number = 1, name = main}
ProjectTwo[2891:90412] 执行任务5--<NSThread: 0x600002420100>{number = 1, name = main}
ProjectTwo[2891:90486] 执行任务2--<NSThread: 0x600002462580>{number = 7, name = (null)}
ProjectTwo[2891:90486] 执行任务3--<NSThread: 0x600002462580>{number = 7, name = (null)}
ProjectTwo[2891:90486] 执行任务4--<NSThread: 0x600002462580>{number = 7, name = (null)}