1、多线程能提高CPU的使用率 防止主线程堵塞
任何有可能堵塞主线程的任务不要在主线程执行
2、多线程和主线程是同时进行的
3、创建多线程对象的5种方法:
//(1)用alloc创建
NSThread *thread = [NSThread alloc] initWithTarget:self
selector:@selector(mutableThread:) object:@“test”];
[Thread start]; //必须有这一句才能运行
//(2)用类方法创建(刚一创建就开始运行)
[NSThread detachNewThreadSelector:@selector(mutableThread:) toTarget:self withObject:@“test”];
//(3)第三种(perform执行)【优点:不用再写一个方法】
[self performSelectorInBackground:@selector(mutableThread:) withObject:nil];
//(4)block语法启动一个线程(NSOperationQueue是个线程池/线程队)
NSOperationQueue *ThreadQueue = [NSOperation alloc] init];
[ThreadQueue addOperationWithBlock:^{ (要执行的任务的codes) …}];
//(5)NSOperation开启一个线程【优点:设置线程的并发数】
//创建一个线程队列
NSOperationQueue *operationQueue = [NSOperationQueue alloc] init];
/*1、设置线程的并发数
operationQueue.maxConcurrentOperationCount = 1;
(若有多个线程,则会先执行一个,当这一个执行完的时候,再执行下
一个)
2、可以改变线程的优先权
op2.queuePriority = NSOperationQueuePriorityHigh;
*/
//创建一个线程操作对象
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread:) object:nil];
//将对象添加到线程中
[operationQueue addOperation:op];
/* [