美文网首页
NSThread的使用

NSThread的使用

作者: iOS_tree | 来源:发表于2024-01-06 14:59 被阅读0次

NSThread是一套面向对象的多线程接口。

简单的创建线程使用如下:

- (void)ThreadRun:(id)object {
    NSLog(@"%s %@",__FUNCTION__,object);
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(ThreadRun:) object:@"text"];
    [thread1 start];
    
}

NSThread的属性如下:

//线程名称
@property (nullable, copy) NSString *name API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
//栈区大小
@property NSUInteger stackSize API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
//是否为主线程
@property (readonly) BOOL isMainThread API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
//线程优先级
@property NSQualityOfService qualityOfService API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0)); // read-only after the thread is started
// 当前线程执行代码的堆栈地址
@property (class, readonly, copy) NSArray<NSNumber *>*callStackReturnAddresses;
// 当前线程执行代码的堆栈信息
@property (class, readonly, copy) NSArray<NSString *> *callStackSymbols;
// 当前线程是否是主线程
@property (class, readonly) BOOL isMainThread;
// 获取主线程NSThread对象
@property (class, readonly, strong) NSThread *mainThread;

当我们继承时,重写main方法,main方法即为线程的执行函数

- (void)main API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // thread body method

示例代码如下:

@interface MyThread : NSThread

@end

@implementation MyThread

- (void)main {
    NSLog(@"%@",[NSThread currentThread]);

    NSLog(@"%s",__FUNCTION__);
}

@end

调用Mythread

MyThread *thread1 = [[MyThread alloc] init];
[thread1 start];

相关文章

  • iOS_多线程

    NSThread NSThread是轻量级的多线程开发,使用起来也并不复杂,但是使用NSThread需要自己管理线...

  • iOS 多线程

    NSThread 使用NSThread对象建立一个线程非常方便,但要使用NSThread管理多个线程较困难,不推荐...

  • iOS 多线程(一)---> NSThread

    NSThread的使用 No.1:NSThread创建线程 NSThread有三种创建方式: init方式 det...

  • iOS 多线程

    iOS使用线程的方式 pthread NSThread GCD NSOperation NSThread线程的创建...

  • 1.2、iOS面试题之多线程

    1.简述GCD,NSThread,NSOperation使用,各自优缺点 NSThread: –优点:NSThre...

  • IOS多线程的方式

    pthread的基本使用(需要包含头文件) 需要#import 3 NSThread (1)NSThread的基本...

  • NSThread的使用

    版本:iOS13.7 一、简介 NSThread可让使用者操作线程,要注意的是需要手动管理该线程的生命周期,可使用...

  • NSThread使用

    - (void)viewDidLoad { [super viewDidLoad]; self.view....

  • 多线程之NSThread

    NSThread的使用: 全部API介绍

  • 爱上多线程 之 GCD

    2 pthread 3 NSThread (1)NSThread的基本使用 (2)设置线程的属性 (3)线程的状态...

网友评论

      本文标题:NSThread的使用

      本文链接:https://www.haomeiwen.com/subject/rweindtx.html