iOS Developer

多线程二(NSThread)

2017-03-03  本文已影响8人  石头撞地球

一个NSThread实例代表一条线程(了解,较少使用)

初始化

-init
-initWithTarget:selector:object:
-initWithBlock
+detachNewThreadSelector:toTarget:withObject:
+detachNewThreadWithBlock:

暂停取消和销毁

+ sleepUntilDate:
Blocks the current thread until the time specified.
   + sleepForTimeInterval:
Sleeps the thread for a given time interval.
   + exit
Terminates the current thread.
   - cancel
Changes the cancelled state of the receiver to indicate that it should exit.

判断线程状态

Thread’s Execution State

executing
A Boolean value that indicates whether the receiver is executing.
finished
A Boolean value that indicates whether the receiver has finished execution.
cancelled
A Boolean value that indicates whether the receiver is cancelled.
线程间通信

在主线程执行

performSelectorInBackground

在指定线程上执行

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);

在当前线程上执行

-performSelector

在后台线程上执行

-performSelectorInBackground

优缺点

上一篇 下一篇

猜你喜欢

热点阅读