iOS多线程中的实际方案之二NSThread

2016-04-24  本文已影响0人  紫菱清风

NSThread


// 创建
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download) object:nil];
// 设置线程的名字
thread.name = @"下载线程01";
// 设置线程的优先级(取值 0.0 - 1.0)
thread.threadPriority = 0.5;
// 启动
[thread start];
-(void) download{
[self performSelectorOnMainThread:@selector(back:) withObject:@"back" waitUntilDone:NO];
}
-(void) back:(id)obj{
  NSLog("%@",obj);
  NSLog("%@,[NSThread currentThread]);
}

2> 创建完自动启动

[NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:nil];

3> 隐式创建(自动启动)// NSObject开启线程

[self performSelectorInBackground:@selector(download:) withObject:nil];
// waitUntilDone:YES 表示当@selector执行完毕后后面的代码才执行。
[self performSelectorOnMainThread:@selector(download:) withObject:nil waitUntilDone:NO];

4> runloop

上一篇下一篇

猜你喜欢

热点阅读