NSThread

步骤:

  1. 创建线程
  2. 启动线程
    注:线程在方法实现里创建,就是局部变量,线程执行完后,就会销亡。

具体实现

- (void)run {
   NSLog(@"子线程被调用了");
}
//创建方式一
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"jack"];
[thread start];
//创建方式二:创建后自动启动线程
[NSThread detachNewThreadSelector:@selecotr(run) toTarget:self withObject:nil];
//创建方式三:隐式创建并启动线程
[self performSelectorInBackground:@selecotr(run) withObject:nil];
//回到主线程:使用RunLoop,可以提高程序性能,让程序没那么卡
self performSelectorOnMainThread:[NSThread mainThread] withObject:nil waitUntilDone:NO modes:[[NSRunLoop currentRunLoop].runLoopCommonMode];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容