一、创建线程
1、方法一
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadSelectorDYZ) object:nil];
[thread start];
- (void)threadSelectorDYZ{
NSLog(@"%s",__FUNCTION__);
}
2、方法二
NSThread *thread = [[NSThread alloc] initWithBlock:^{
NSLog(@"%s",__FUNCTION__);
}];
[thread start];
3、方法三
[NSThread detachNewThreadSelector:@selector(threadSelectorDYZ) toTarget:self withObject:nil];
- (void)threadSelectorDYZ{
NSLog(@"%s",__FUNCTION__);
}
4、方法四
[NSThread detachNewThreadWithBlock:^{
NSLog(@"%s",__FUNCTION__);
}];
5、隐私方法创建线程
[self performSelectorInBackground:@selector(threadSelectorDYZ) withObject:nil];
- (void)threadSelectorDYZ{
NSLog(@"%s",__FUNCTION__);
}
二、perform
2.1、执行方法,当前是什么线程就在什么线程执行
下面三个方法在NSObject
中
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
- (void)test {
NSLog(@"test:%@",[NSThread currentThread]);
}
- (void)testOne:(NSArray *)str {
NSLog(@"testOne:%@\n%@",[NSThread currentThread],str);
}
- (void)testTwo:(NSArray *)str with:(NSString *)str1{
NSLog(@"testTwo:%@\n%@\n%@",[NSThread currentThread],str,str1);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self performSelector:@selector(test)];
[self performSelector:@selector(testOne:) withObject:@[@"123",@"234",@"345"]];
[self performSelector:@selector(testTwo:with:) withObject:@[@"123",@"234",@"345"] withObject:@"hello world"];
}
2020-04-23 13:48:40.876108+0800 TraceDemo[13692:1917162] test:<NSThread: 0x281782f40>{number = 1, name = main}
2020-04-23 13:48:40.876402+0800 TraceDemo[13692:1917162] testOne:<NSThread: 0x281782f40>{number = 1, name = main}
(
123,
234,
345
)
2020-04-23 13:48:40.876639+0800 TraceDemo[13692:1917162] testTwo:<NSThread: 0x281782f40>{number = 1, name = main}
(
123,
234,
345
)
hello world
2.2、 线程切换,到主线程、指定线程、后台线程(子线程)执行
下面五个方法属于NSThreadPerformAdditions
分类
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
// equivalent to the first method with kCFRunLoopCommonModes
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
// equivalent to the first method with kCFRunLoopCommonModes
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
2.3、延迟调用,在当前线程延迟调用
如果是主线程不会堵住当前线程,子线程会堵住当前线程
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
三、[[NSRunLoop currentRunLoop] run]
如果是主线程,
run
后面的代码都不会走了。如果是子线程,
run
前面如果有定时器time,run
后面的代码也不会走,定时器执行完毕了,就可以走了;run
前面如果没有定时器time,run
后面的代码会走总结:当有任务执行的时候,
run
后面的代码就不会走,没有任务执行了,run
后面的代码就会走了。那么为什么主线程run
后面的代码不会走呢,是因为主线程干其他的事去了,只是你不知道而已。