// 创建线程 (一旦启动,就调用self的run方法) object : 用于传参,比如,路径(下载资源的时候)
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
// 启动线程(执行完毕会自动销毁线程)
[thread start];
// 主动销毁线程(还未执行完毕就退出)
[thread exit];
// 创建线程,创建完毕,即刻启动 / detach : 分支 (需要传参,所以run:)
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"a.jpg"];
// 隐式创建线程
[self performSelectorInBackground:@selector(run) withObject:nil];
// 输出当前执行的线程
NSLog(@"%@",[NSThread currentThread]);
// 获取主线程
NSThread *thread = [NSThread mainThread];
// 获取当前线程
NSThread *thread = [NSThread currentThread];
// 实例方法判断目标线程是否为主线程
[thread isMainThread];
// 类方法判断当前执行的代码是否在主线程中
[NSThread isMainThread];
// 给线程设置名字
[thread setName:@"第二线程"];
// 获取线程的名字
[thread name];
// 当前线程进入sleep状态2s
[NSThread sleepForTimeInterval:2];
线程锁 (小括号内的是对象 , 只要是对象且对象唯一 , 就可以作为唯一锁 ,不是唯一锁,没有 )
@synchronized(self)
{
// 相当于原子操作
}