Created
May 4, 2018 06:07
-
-
Save zhangkn/3e265030afdd521faed4d7a152666dc0 to your computer and use it in GitHub Desktop.
创建、启动线程
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1、创建、启动线程 | |
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; | |
[thread start];//启动线程 // 线程一启动,就会在线程thread中执行self的run方法 | |
// 2、主线程相关用法 | |
+ (NSThread *)mainThread; // 获得主线程 | |
- (BOOL)isMainThread; // 是否为主线程 | |
+ (BOOL)isMainThread; // 是否为主线程 | |
//3、 其他用法 | |
// 1)获得当前线程 | |
NSThread *current = [NSThread currentThread]; | |
// 2)线程的调度优先级 | |
+ (double)threadPriority; | |
+ (BOOL)setThreadPriority:(double)p; | |
- (double)threadPriority; | |
- (BOOL)setThreadPriority:(double)p;//调度优先级的取值范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高 | |
// 3)线程的名字 | |
- (void)setName:(NSString *)n; | |
- (NSString *)name; | |
//4、其他创建线程方式 | |
// 创建线程后自动启动线程 | |
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil]; | |
// 隐式创建并启动线程 | |
[self performSelectorInBackground:@selector(run) withObject:nil]; | |
// 上述2种创建线程方式的优缺点:优点:简单快捷 ; 缺点:无法对线程进行更详细的设置 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment