多线程之第三种编程方法GCD

今天将介绍多线程GCD的编程方法:
1、什么是GCD?

全称是Grand Central Dispatch,纯C语言编写,提供非常强大的函数,是目前苹果官网推荐的多线程开发方法,NSOperation便是基于GCD的封装。

2、GCD的优势所在
1》为多核的并行运算提出了解决方案
2》GCD会利用更多的CPU内核,比如双核,四核
3》GCD自动管理线程的生命周期(创建线程,调度任务,销毁线程)
4》程序员只需要告诉GCD想要执行什么任务,不需要编写任何线程管理代码

3、GCD中有两个核心概念
1> 任务:执行什么操作
2> 队列:用来存放任务

4、队列可以分为两大类型(都是用非主队列)
1》串行队列(Serial dispatch Queue):只有一个线程,加入到队列中的操作按添加顺序依次执行,一个任务执行完毕后,才能执行下一个任务。
2》并发执行(Concurrent Dispatch Queue):可以有多个线程,操作起来以后他会将这些线程安排在可用的处理器上,同时保证先进来的任务优先处理。
3》还有一个特殊的队列就是主队列,主队列中永远只有一个线程-主线程,用来执行主线程的操作任务。

5、采用GCD做多线程,可以抽象为两步
1》找到队列(主队列或串行队列或并行队列)
2》在队列中用同步或者异步的方式执行任务

6、执行队列中任务的两种方式
1》同步:只能在当前线程执行任务,不具备开启新线程的能力
2》异步:可以在新的线程中执行任务,具备开启新线程的能力

执行队列有四种方式,具体如下:

#pragma mark *****串行同步*****
   
     1.找到队列
     * 第一个参数:该队列的名字
     
     * 第二个参数:指定队列的类型
     
     
//    1、找到队列
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
    
    /*
     * 2、给队列指定任务 asyn是异步,syn是同步
     
     *第一个参数:任务在哪个队列中执行
     
     *第一个参数:想要执行的操作
     */
    dispatch_sync(serialQueue, ^{
        
        NSLog(@"1===%@",[NSThread currentThread]);
        
    });
    ```

pragma mark ----串行异步----

// 1、找到队列

//dispatch_queue_t serialQueue1 = dispatch_queue_create("serialQueue1", DISPATCH_QUEUE_SERIAL);

// 2、给队列指定异步任务
// dispatch_async(serialQueue1, ^{
// NSLog(@"1 = %@",[NSThread currentThread]);
// });
```

#pragma mark ----并行同步----
////    1、找到队列
//    
//    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
////    2、给队列指定任务
//    dispatch_sync(concurrentQueue, ^{
//        
//        NSLog(@"1-%@",[NSThread currentThread]);
//    });
    ```

pragma mark ----并行异步----

//    1、找到队列

dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
//    2、给队列指定任务
dispatch_async(concurrentQueue, ^{
    
    NSLog(@"1-%@",[NSThread currentThread]);
});    

}



下面以加载一张网络图片来具体介绍:
/*
     * 1、创建视图
     * 2、创建一个串行队列
     * 3、用异步方式执行串行队列中的任务
     * 4、加载网络资源
     * 5、回到主线程
     * 6、更新UI
     
     */

// OneImageViewController.m
// 3.3多线程-GCD
//
// Created by 记忆 on 16/3/3.
// Copyright © 2016年 Remeber. All rights reserved.
//

import "OneImageViewController.h"

define kurl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"

@interface OneImageViewController ()
{
UIImageView *imageView;
}
@end
@implementation OneImageViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    
    
//    1、创建视图
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
    
    [self.view addSubview:imageView];
    ```

// 2、创建一个串行队列
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
```

//    3、用异步方式执行串行队列中的任务
    dispatch_async(serialQueue, ^{
        
        //4、加载网络数据
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
        
        UIImage *image = [UIImage imageWithData:data];
//        5、回到主线程
        // dispatch_get_main_queue 这个函数找到主队列
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_sync(mainQueue, ^{
            
            //6、更新UI
            imageView.image = image;
            
        });
        
    });
    
}
@end

加载多张网络图片:
1.创建多个视图
2.找到并行队列
3.给这个并行队列指定多个任务
4.在子线程加载网络资源
5.回到主线程
6.更新UI

#import "MoreImageViewViewController.h"
#define kurl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
@interface MoreImageViewViewController ()
{
    int imageIndex;
    dispatch_queue_t concurrentQueue;
}
@end
@implementation MoreImageViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self controlBtn];
 
    imageIndex = 100;
    //    1、创建多个视图
    for (int row = 0; row<3; row++) {
        for (int list = 0; list<2; list++) {
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10+list*200, 10+row*200, 200, 200)];
            imageView.backgroundColor = [UIColor brownColor];
            
            imageView.tag = imageIndex++;
            
            [self.view addSubview:imageView];
            
        }
    }
//    2.找到并行队列
    /*
     * dispatch_get_global_queue 获取到系统的全局并行队列
     * 第一个参数:是优先级
     * 第二个参数:保留参数,无用
     */
    //dispatch_queue_t concurrentQueue = dispatch_get_global_queue(0, 0);
    
     concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_SERIAL);//串行队列
    
    
//    3、给这个并行队列指定多个任务
    for (int index = 0; index<6; index++) {
        dispatch_async(concurrentQueue, ^{
            
            [NSThread sleepForTimeInterval:0.5];
            
//            4、获取网络资源
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
            UIImage *image = [UIImage imageWithData:data];
            
//            5、回到主线程
            dispatch_sync(dispatch_get_main_queue(), ^{
                
                //6、更新UI
                UIImageView *imageView = [self.view viewWithTag:100+index];
                imageView.image = image;
                
            });
            
            
            
        });
    }
    
   }

在加载多张图片的时候,可以设置设备锁。

- (void)controlBtn{
   
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"暂停",@"开启",]];
    
    segment.frame = CGRectMake(50, 620, 300, 50);
    
    segment.apportionsSegmentWidthsByContent = YES;
    
    [self.view addSubview:segment];
    
    [segment addTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventValueChanged];
}

- (void)clickSegment:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {  
        case 0:{
            dispatch_suspend(concurrentQueue);
        }break;
            
        case 1:{
            
            dispatch_resume(concurrentQueue)   
        }break;     
    }     
}
@end
#import "GCDLockViewController.h"

@interface GCDLockViewController ()
{
    NSLock *myLock;
}
@end
@implementation GCDLockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //实例化一个线程锁
    myLock = [NSLock new];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
#pragma mark ----线程锁----
    
    __block int ticketNum = 10;
    
    dispatch_queue_t concurrent = dispatch_get_global_queue(0, 0);
    
    for (int index = 0; index<15; index++) {
        
        dispatch_async(concurrent, ^{
            // 第一种上锁的方式
//            [myLock lock];
//            if (ticketNum>0) {
//                ticketNum--;
//                
//                NSLog(@"还剩%d张票",ticketNum);
//            }
            
            //[myLock unlock];
            
            //参数一般是self,与self相关的变量多个线程同时值访问一次
            //第二种上锁方式
            @synchronized(self) {
                if (ticketNum>0) {
                    ticketNum--;
                    
                    NSLog(@"还剩%d张票",ticketNum);
                    
                }

            }
            
        });
        
    }
    
    
}
@end
                      (完)

你可能感兴趣的:(多线程之第三种编程方法GCD)