2022-12-29ios-NSThread创建线程的4种方法

代码如下,非常简单:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //方式1 创建线程,
    NSThread *st = [[NSThread alloc]initWithTarget:self selector:@selector(demo) object:nil];
    //启动线程
    [st start];
    //方式2创建线程
    [NSThread detachNewThreadSelector:@selector(demo) toTarget:self withObject:nil];
    //方式3创建线程
    [self performSelectorInBackground:@selector(demo) withObject:nil];
    //方式4创建线程,带参数
    NSThread *nst = [[NSThread alloc]initWithTarget:self selector:@selector(test:) object:@"abcderf"];
    [nst start];
}

- (void)demo {
    NSLog(@"我是子线成1");
}

- (void)test:(NSString *)str {
    NSLog(@"str=%@",str);
}

@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容