iOS多线程笔记1--NSThead

总结学习多线程做的一些笔记。
API

@interface NSThread : NSObject  {
@private
    id _private;
    uint8_t _bytes[44];
}
//获取当前线程
@property (class, readonly, strong) 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;
//
@property (readonly, retain) NSMutableDictionary *threadDictionary;
//线程等待到某个时刻唤醒。
+ (void)sleepUntilDate:(NSDate *)date;
//等待多少时间
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
//退出当前线程
+ (void)exit;
//获取线程优先级
+ (double)threadPriority;
//设置线程优先级
+ (BOOL)setThreadPriority:(double)p;
//线程优先级
@property double threadPriority; // To be deprecated; use qualityOfService below
//线程服务质量,替代ios8之前的优先级
@property NSQualityOfService qualityOfService API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0)); 
//获取堆栈地址
@property (class, readonly, copy) NSArray *callStackReturnAddresses 
//堆栈信息,获取方法执行的类和顺序
@property (class, readonly, copy) NSArray *callStackSymbols;
//线程名称,多数用于跟踪崩溃位置。
@property (nullable, copy) NSString *name;
//堆栈大小
@property NSUInteger stackSize;
//是否是主线程
@property (readonly) BOOL isMainThread;
//当前线程是否是主线程
@property (class, readonly) BOOL isMainThread; 
//当前线程的主线程
@property (class, readonly, strong) NSThread *mainThread ;
//初始化方法
- (instancetype)init ;
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument;
- (instancetype)initWithBlock:(void (^)(void))block ;
//线程是否正在执行
@property (readonly, getter=isExecuting) BOOL executing;
//线程是否已经结束
@property (readonly, getter=isFinished) BOOL finished;
//线程是否已经返回
@property (readonly, getter=isCancelled) BOOL cancelled;
//取消
- (void)cancel;
//开始执行
- (void)start;

- (void)main;   // thread body method
@end

线程间通信

1.//在指定线程执行,waitUntilDone:是否等待执行完后再返回。
  [self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];
2.//在主线程上执行。
  [self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
3.//在当前线程执行。
  [self performSelector:@selector(run) withObject:nil];

杂记

1.name属性方便查找bug,调试。

2.主线程挂掉程序不会挂。

3.线程优先级只能保证线程调度可能性高,不能控制CPU调用顺序,可能造成优先级反转。

4.GCD NSOperation 属于并发编程,与多线程无关,控制队列。

5.用alloc init 初始化的NSThread 不能作用于perforSelector参数, 需要使用alloc initWithtarget方法。

   NSThread *t = [[NSThread alloc] initWithTarget:self selector:@selector:(demo) object:nil];
   [t start];
   [self perforSelector:@selector(otherDemo) onThread:t withobject:nil watUtilDone:NO];

demo能执行,otherDemo不执行。
原因:子线程的RunLoop默认不会开启循环,demo方法执行完后线程t结束。
不能用 [[NSRunLoop currentRunLoop] run] 开启runLoop,无线循环会让run后面的代码不能执行,而且runloop不能关闭,如果要循环一次需要使用:

    While (布尔值) {
       [ [NSRunLoop currentRunLoop] sunMode:NSDefaultTunLoopMode beforeDate:[NSDate datewithTimeIntervalSinceNoew:0.1]];//多长时间后循环一次
}

6.原子属性:
— nonatomic:非原子属性,不会对setter方法加锁,如果属性被多线程访问需要使用 atomic。
— atomic:原子属性,保证属性安全性(多线程读写安全),针对多线程设计。
— atomic属性使用自旋锁。

7.OC中定义属性Xcode(非OC语法)会生成 _成员变量,如果重写了setter & getter方法 _成员变量就不自动生成。

8.UIKit框架都是线程不安全 — 为了效率(流畅)。

你可能感兴趣的:(iOS多线程笔记1--NSThead)