iOS Developer

多线程(NSThread+NSOperation)

2016-05-18  本文已影响52人  简简简简简书

多线程(NSThread+NSOperation)

多线程概述

NSThread

方法 功能
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument 初始化一个子线程,但是需要手动开启
+ (void)detachNewThreadSelector:(SEL)aselector toTarget:(id)aTargetWithObject:(id)anArgument 初始化一个子线程,不需要手动开启
start 开启子线程
cancel 取消当前子线程
[NSThread currentThread] 获取当前线程
[NSThread mainThread] 获取主线程
[NSThread sleepForTimeInterval:] 线程休眠(秒)
    //创建一个线程
    NSThread  *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread:) object:nil];
    thread.name = @"第一个子线程";
    //设置线程的优先级
    thread.threadPriority  = 0.9;
    //开启
    [thread start];
    //再添加一个线程(自动开启,不许要手动开启)
    [NSThread detachNewThreadSelector:@selector(thread2:) toTarget:self withObject:nil];
    //是否是主线程
    BOOL ismain = [NSThread isMainThread];
    NSLog(@"%d",ismain);
    //睡眠
    [NSThread sleepForTimeInterval:3];
    //变成多线程通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isMultipelThread) name:NSWillBecomeMultiThreadedNotification object:nil];
    //变成单线程通知
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isSingleThread) name:NSDidBecomeSingleThreadedNotification object:nil];
    //推出线程通知
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isExitThread) name:NSThreadWillExitNotification object:nil];
    //创建一个线程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread) object:nil];
    [thread start];//开启
    [thread cancel];//取消
#pragma mark----------NSThread的隐式创建
    //开启子线程
    [self performSelectorInBackground:@selector(background1) withObject:nil];
    //子线程执行的方法
#pragma mark----------NSThread的隐式创建
//子线程执行的方法
- (void)background1
{
    NSLog(@"当前线程 %@",[NSThread currentThread]);
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic.nipic.com/2007-11-09/200711912453162_2.jpg"]];
    UIImage*image = [UIImage imageWithData:data];
    //回主线程(第一种方式)
    [self performSelectorOnMainThread:@selector(mainThread:) withObject:image waitUntilDone:YES];
    //回到指定线程(第二种方式)
    //先拿到主线程    yes是先执行selector中的方法,在执行下面的东西 no就是不管selector方法中的有没有执行完,都要接着执行
NSThread *thread =    [NSThread mainThread];
    [self performSelector:@selector(mainThread:) onThread:thread withObject:image waitUntilDone:YES];
}
- (void)mainThread:(UIImage *)image
{
    UIImageView *view = [[UIImageView alloc] initWithImage:image];
    view.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:view];
    NSLog(@"mainThred = %@",[NSThread currentThread]);
}

通知机制

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject
  //注册通知 1. 监听者(消息的接收者) 2.接收到消息执行的方法 3.通知的名字 4.配置信息(附加信息)(注册的时候一定要想着移除)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aaa:) name:@"name" object:nil];
#pragma mark----------通知机制
    - (void)aaa:(NSNotification*)object
{
    NSLog(@"接收到通知");
    NSLog(@"%@",object.object);
}
- (void)dealloc
{
    //一定要移除
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
---------------这里已经是第二个控制器了
//通过按钮跳转回前一个页面时,传值
- (void)bbb
{
    //发送通知(发送可以一对多,只要发送,注册过的界面都会接收到通知)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:@"789456"];
    //跳回上一个界面
    [self dismissViewControllerAnimated:YES completion:nil];
}

NSOperation

方法 功能
- (void)addOperation:(NSOperation *)op 添加任务
- (void)addOperationWithBlock:(void (^)(void))block 添加任务(用block)添加
maxConcurrentOperationCount 最大并发数
suspended 是否暂停队列
operationCount 队列中的任务数量
operations 队列中的所有任务(数组管理)
cancelAllOperations 取消所有任务

NSBlockOperation

    //创建任务(第一种)
    NSBlockOperation *blockOperation  =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"当前线程  %@",[NSThread currentThread]);
    }];
    //加入队列里面(自动开启任务)
    NSOperationQueue *queue  =[[NSOperationQueue alloc] init];
    [queue addOperation:blockOperation];
    //直接创建队列并添加block(第二种)
        [queue addOperationWithBlock:^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        NSData *data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        NSData *data3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
        //回主线程
        [self performSelectorOnMainThread:@selector(aaa) withObject:nil waitUntilDone:YES];
            //添加依赖(第二个依赖于第一个完成之后才能执行)
    [queue.operations[1] addDependency:queue.operations[0]];
    }

NSInvocationOperation

    NSInvocationOperation *invo = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invo) object:nil];
    [queue addOperation:invo];


    
    - (void)invo
{

    NSLog(@"%@",[NSThread currentThread]);
}
上一篇下一篇

猜你喜欢

热点阅读