事情太忙碌,直接用OC记录了,以后使用时,再换成Swift
多线程
1、NSThread
-》最基本的创建方式
/**
创建线程第一种方法,需要调用start方法才可以执行
*/
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
thread.name = @"Custom Thread";
[thread start];
-》直接创建,无须手动启动
/**
* 创建线程后自动启动
*/
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
/**
* 隐式创建并启动线程
*/
[self performSelectorInBackground:@selector(run) withObject:nil];
-》其他方法
/**
* 获得当前线程
*/
NSThread *thread = [NSThread currentThread];
/**
* 获得主线程
*/
NSThread *mainThread = [NSThread mainThread];
-》线程间的通信
/**
* 线程间的通信
*/
[thread performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:true];
2、GCD(重点,充分利用手机的多核)
-》队列
--》并发队列:dispatch_get_global_queue