多线程之NSThread

NSThread的使用:

// 1. 使用类方法创建线程并自动启动线程
    [NSThread detachNewThreadSelector:@selector(ThreadMethod) toTarget:self withObject:nil];
    [NSThread detachNewThreadWithBlock:^{
        // run ...
        [self ThreadMethod];
    }];

// 2. init创建并启动
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(ThreadMethod) object:nil];
    // 设置线程名
    [thread setName:@"NSThread"];
    // 设置优先级,优先级从0到1,1最高
    [thread setThreadPriority:0.9];
    // 启动
    [thread start];

//3.隐式创建,直接启动 
[self performSelectorInBackground:@selector(ThreadMethod:) withObject:@"NSThread3"];

全部API介绍

//获取当前线程
+(NSThread *)currentThread;

//创建线程后自动启动线程
+ (void)detachNewThreadWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));

+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullable id)argument;

//是否是多线程
+ (BOOL)isMultiThreaded;

//线程字典
- (NSMutableDictionary *)threadDictionary;

//线程休眠到什么时间
+ (void)sleepUntilDate:(NSDate *)date;

//线程休眠多久
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

//退出线程
+ (void)exit;

//线程优先级
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;

- (double)threadPriority;
- (void)setThreadPriority:(double)threadPriority;

//调用栈返回地址
+ (NSArray *)callStackReturnAddresses NS_AVAILABLE(10_5, 2_0);
+ (NSArray *)callStackSymbols NS_AVAILABLE(10_6, 4_0);

//线程名字
- (NSString *)name;
- (void)setName:(nullable NSString *)name NS_AVAILABLE(10_5, 2_0);

//获取栈的大小
- (NSUInteger)stackSize NS_AVAILABLE(10_5, 2_0);
- (void)setStackSize:(NSUInteger)s NS_AVAILABLE(10_5, 2_0);

//是否是主线程
- (BOOL)isMainThread NS_AVAILABLE(10_5, 2_0);
+ (BOOL)isMainThread NS_AVAILABLE(10_5, 2_0); // reports whether current thread is main

// 获得主线程对象
+ (NSThread *)mainThread;

//实例化方法
- (id)init NS_AVAILABLE(10_5, 2_0); // designated initializer
- (id)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
- (id)initWithBlock:(void (^)(void))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));

//是否正在执行
- (BOOL)isExecuting NS_AVAILABLE(10_5, 2_0);

//是否执行完成
- (BOOL)isFinished NS_AVAILABLE(10_5, 2_0);

//是否取消线程
- (BOOL)isCancelled NS_AVAILABLE(10_5, 2_0);

//取消线程
- (void)cancel NS_AVAILABLE(10_5, 2_0);

//线程启动
- (void)start NS_AVAILABLE(10_5, 2_0);

- (void)main NS_AVAILABLE(10_5, 2_0); // thread body method


//多线程通知
FOUNDATION_EXPORT NSString * const NSWillBecomeMultiThreadedNotification;
FOUNDATION_EXPORT NSString * const NSDidBecomeSingleThreadedNotification;
FOUNDATION_EXPORT NSString * const NSThreadWillExitNotification;


//与主线程通信
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray *)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 *)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));




你可能感兴趣的:(多线程之NSThread)