多线程

  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];

/* [
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值