NSThread
NSThread *myThread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"jack"];
[myThread start]; // 开始
- 创建线程后自动启动线程
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
- 隐身创建并启动线程
[self performSelectorInBackground:@selector(run:) withObject:nil];
- 让线程睡眠2秒(阻塞2秒)
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
- 启动线程
- 进入就绪状态 -> 运行状态。当线程任务执行完毕,自动进入死亡状态
- (void)start;
● 强制停止线程
- (void)exit;
注意:一旦线程停止(死亡)了,就不能再次开启任务
● 互斥锁使用格式
@synchronized(锁对象)
{ // 需要锁定的代码 }
注意:锁定1份代码只用1把锁,用多把锁是无效的
● 互斥锁的优缺点
● 优点:能有效防止因多线程抢夺资源造成的数据安全问题
● 缺点:需要消耗大量的CPU资源
● 互斥锁的使用前提:多条线程抢夺同一块资源