多线程
1.如何创建多线程:
yi:.第一种创建线程的方式:
1.第一个参数:线程执行体对应的方法。第二个参数:线程执行体方法所属的对象。第三个参数:线程知兴替方法执行的实际参数
NSNumber *number = @100;
[NSThread detachNewThreadSelector:@selector(threadOne:) toTarget:self withObject:number];
-(void)threadOne:(NSNumber *)number;
2.第二种方式创建多线程
第一个参数:线程执行体方法所属的对象。第二个参数线程执行体对应的方法。第三个参数线程执行体对应的实际参数
NSString *str = @"hello.thread";
NSThread *t = [[NSThread alloc]initWithTarget:self selector:@selector(threadTwo:) object:str];
3.给线程名字
t.name = @"pp";
4.获得线程的名字
NSThread *t = [NSThread currentThread];
5.获取主线程名字。默认的木有名字
NSThread *mainThread = [NSThread mainThread];
6.线程开始执行
[t start];
7.退出线程
[NSThread exit];
2.如何取消多线程:
线程结束的三种情况
1.线程的执行体执行完成
2.调用[NSThread exit];
3.线程执行时出错抛出异常
取消线程:取消线程只是状态修改了,线程并没有结束
[thread cancel];
if([thread isCancelled])
{
[NSThread exit];}
3.监听线程的退出:
通知中心,NSThreadWillExitNotification是系统定义的通知类型,系统定义的通知不需要我们发送,系统自己会发送,我们自己定义的通知需要自己去发送,就会调用postNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(threadExist:) name:NSThreadWillExitNotification object:nil];
监听线程状态
-(void)threadExist:(NSNotification *)n{
NSThread *t = n.object;
NSLog(@"%@ 退出",t.name);
}
释放
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
程序的优先级
4.线程的优先级
修改优先级
优先级的值在0-1之间,优先级越高,执行的次数相对来说会越高
NSThread *t1 = [[NSThread alloc]initWithTarget:self selector:@selector(tOne) object:nil];
t1.threadPriority = 0;
5.程序就是一段代码块,是静态的概念
进程是运行起来的程序,是动态的概念,进程需要占用内存空间
线程是进程的基本单位,一个进程至少有一个线程,ios默认有一个主线程,用来显示,和操作ui。主线程由系统自动创建,由系统管理,如果主线程不能满足我们的需求,可以进行手动,手动线程需要我们自己管理
6.线程的使用:下载简单图片的使用
NSThread *th = [[NSThread alloc]initWithTarget:self selector:@selector(load) object:nil];
[th start];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
[self performSelectorOnMainThread:@selector(refresh:) withObject:data waitUntilDone:YES];
_imageView.image = [UIImage imageWithData:data];
7.
线程锁:银行取钱问题:
枷锁1.
枷锁
NSLock *_mylock;
初始化锁
-(instancetype)init{
if (self == [super init]) {
_mylock = [[NSLock alloc]init];
}
return self;
}
枷锁
[_mylock lock];
{
代码块
}
解锁
[_mylock unlock];
枷锁2.
@synchronized(self){
代码块
}
二.第二种实现线程的方式
二-1
NSOperationQueue
1.创建队列
_queue = [[NSOperationQueue alloc]init];
2.创建,第一个参数线程执行体方法所属的对象。第二个参数,线程执行体方法,第三个参数,线程执行体方法传递的实际参数
NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(threadOperation) object:nil];
3.开启线程
[_queue addOperation:op1];
4.执行方法
5.线程结束后执行的代码块
[op1 setCompletionBlock:^{
NSLog(@"线程结束了");
}];
二-2
2.第二种常见线程方法
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
线程的执行体
for (int i = 0; i< 100 ; i++) {
NSLog(@"线程2:%d",i);
}
}];
2.线程执行结束的代码块
[op2 setCompletionBlock:^{
NSLog(@"线程2结束");
}];
[_queue addOperation:op2];
第三种方式实现多线程
1.新建一个类继承NSOperation
2.使用NSOperation自定制对象创建线程时的执行体,在main方法里面下载
(举个例子)
-(void)main{
NSURL *url = [NSURL URLWithString:self.urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
3.回到住线程修改ui
[self performSelectorOnMainThread:@selector(refreshUI:) withObject:data waitUntilDone:YES];
}
-(void)refreshUI:(NSData *)data{
self.imageView.image = [UIImage imageWithData:data];
}
1.创建一个队列
_queue = [[NSOperationQueue alloc]init];
2。创建imageOperation对象
ImageOperation *op = [[ImageOperation alloc]init];
设置下载链接
op.urlString = @"";
3.设置显示的视图
op.imageView = _imageView;
4.队列里面同时执行的线程的最大数量,假设队列里加入20个线程,则只有等这10个线程执行完之后才会执行另外10个
_queue.maxConcurrentOperationCount = 10;
[_queue addOperation:op];//添加到队列进行下载