iOS学习笔记(三十二)————多线程

/ 0评 / 0

多线程操作,iOS中有三种多线程操作,我们来简单看一下

more thread

NSThread

//线程
- (void)threadTest{
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:imageView];

    
    [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];
    
    NSLog(@"11111111111");
}

- (void)loadImage{
    // 代码在主线程中执行的
    // 把网络上的图片 拿到本地data
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://f.hiphotos.baidu.com/image/pic/item/29381f30e924b8990c9439396c061d950a7bf603.jpg"]];
    
    // 是否等 主线程的方法执行过之后 再继续执行 分线程的代码
    [self performSelectorOnMainThread:@selector(showImage:) withObject:data waitUntilDone:YES];
    
    NSLog(@"222222222   %d", [NSThread isMainThread]);
}

- (void)showImage:(NSData *)data{
    imageView.image = [UIImage imageWithData:data];
    
    NSLog(@"33333333   %d", [NSThread isMainThread]);
}

NSOperation

//队列
- (void)operationTest{
    // 使用的流程
    // 1,创建一个操作(绑定一些代码)
    // 2,把这个操作添加到操作队列中,然后操作所绑定的代码 就自动会在分线程中执行了
    
    // 操作队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    // 设置最大并发量
    queue.maxConcurrentOperationCount = 5;
    
    // 执行(调用)操作
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operation1Doing) object:nil];
    
    [queue addOperation:operation1];
    
    
    // 块操作
    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
                                        NSLog(@"222222222   %d",[NSThread isMainThread]);
                                    }];
    
    [queue addOperation:operation2];
    
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:imageView1];
    
    
    // 自定义操作   自动会执行重写的main方法
    DCOperation *operation3 = [[DCOperation alloc] initWithURLString:@"http://f.hiphotos.baidu.com/image/pic/item/29381f30e924b8990c9439396c061d950a7bf603.jpg"];
    operation3.block = ^(NSData *data){
        NSLog(@"111    %d",[NSThread isMainThread]);
        imageView1.image = [UIImage imageWithData:data];
    };
    [queue addOperation:operation3];
}

- (void)operation1Doing{
    NSLog(@"111111111   %d",[NSThread isMainThread]);
}

GCD

//gcd
- (void)gcdTest{
    // GCD
    // grand central dispatch
    // 强大的中央调度
    // GCD是系统底层的一种使用多线程的一种方式,执行效率更高
   
    // 同步  异步
    // 同步,异步 是相对于另外一个任务来说的
    // 同步执行指的是当在执行第一个任务的时候,必须等第一个任务完全执行完之后,再开始第二个任务
    // 异步执行指的是当第一个任务一开始执行,不管是否执行完,就立刻开始第二个任务
    
    // 串行   并发
    // 串行 和 并发是相对于多个任务来说的,
    // 串行指 多个任务 按照一定的顺序,一个执行完之后 执行下一个
    // 并发指 多个任务同时执行
    
    
    // 1,创建串行  或者  并发 队列
    /*
     dispatch_queue_t queue1 = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
     
     dispatch_async(queue1, ^
     {
     NSLog(@"1111111111   %d",[NSThread isMainThread]);
     });
     
     dispatch_async(queue1, ^
     {
     NSLog(@"2222222");
     
     [NSThread sleepForTimeInterval:2];
     });
     
     dispatch_async(queue1, ^
     {
     NSLog(@"333333333");
     });
     */
    
    
    // 2, 获取系统提供的全局并发队列
    /*
     dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     
     dispatch_async(queue2, ^
     {
     NSLog(@"44444444");
     [NSThread sleepForTimeInterval:2];
     });
     
     dispatch_async(queue2, ^{
     NSLog(@"5555555");
     [NSThread sleepForTimeInterval:2];
     });
     */
    
    
    
    // 3,获取到系统内部的主线程队列
    
    /*
     dispatch_queue_t mainQueue = dispatch_get_main_queue();
     
     dispatch_async(mainQueue, ^
     {
     NSLog(@"6666666666  %d",[NSThread isMainThread]);
     });
     
     */
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:imageView2];
    
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
                       NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://h.hiphotos.baidu.com/image/pic/item/8601a18b87d6277f017b73322b381f30e924fc4b.jpg"]];
                       
                       dispatch_async(dispatch_get_main_queue(), ^{
                                          imageView2.image = [UIImage imageWithData:data];
                                      });
                   });
}

- (void)test{
    NSLog(@"123123");
    NSLog(@"123123");
    NSLog(@"123123");
    NSLog(@"123123");
    NSLog(@"123123");
    
}

- (void)test1{
    NSLog(@"123123");
    NSLog(@"123123");
    NSLog(@"123123");
    NSLog(@"123123");
    NSLog(@"123123");
}

如果要深入学习,请访问这里

代码请查看 http://git.oschina.net/zcb1603999/LearningiOS

评论已关闭。