- NSThread
创建多线程的方法有很多种,这里只说两种:
①可以获取线程对象,给线程设置名称等
// 创建多线程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(textThread) object:nil];
// 启动多线程
[thread start];
②创建线程的方法简单
[NSThread detachNewThreadSelector:@selector(textThread) toTarget:self withObject:nil];
打印当前线程所在队列
NSLog(@"%@" , [NSThread currentThread]);
使用互斥锁的前提:多条线程抢夺一块资源
还有就是,线程安全.我们用到互斥锁.
@synchronized(self) {
// 这里进行方法的操作
}
互斥锁优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量的 CPU 资源