ios开发之多线程NSThread

NSThread的两种创建方法

1、实例方法 创建并手动开启线程

NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(selector) object:(nullable id)];
[thread start];//开启线程

2、类方法(无返回值 ) 创建并自动开启线程

[NSThread detachNewThreadSelector:@selector(selector) toTarget:self withObject:(nullable id)];

3、获取当前线程

 thread.name = @"线程名";

    [NSThread currentThread];//获取当前线程
    [NSThread mainThread];//获取主线程
    [NSThread isMainThread];//判断是否是主线程
    [NSThread isMultiThreaded];//判断是否是多线程
    [NSThread setThreadPriority:1.0];//设置线程的优先级(0-1)
    [NSThread sleepForTimeInterval:2];//让线程休眠
  • (void)exit;//
  • (void)cancel;

你可能感兴趣的:(学习笔记)