NSThread

@bingoogolapple 2015-04-11 03:38:55发表于 bingoogolapple/bingoogolapple.github.io iOS

创建和启动线程的三种方式

先创建,后启动

NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:nil];
[thread start];

创建完自动启动

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

隐式创建(自动启动)

[self performSelectorInBackground:@selector(download:) withObject:nil];

常见方法

获取当前线程

+(NSThread *)currentThread;

获取主线程

+(NSThread *)mainThread;

睡眠(暂停)线程

+(void)sleepUnitDeta:(NSDate *)date;
+(void)sleepForTimeInterval:(NSTimeInterval)time

设置线程的名字

-(void)setName:(NSString *)name;
-(NSString *)name;

不要同时开太多的线程(1~3条线程即可,不要超过5条)