基础知识:

下午9:09

一、基础概念

1、什么是GCD

全称是Grand Central Dispath 纯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、异步:可以在新的线程中执行任务,具备开启新线程的能力

7、GCD创建的线程任务有四种方式

二、串行同步  串行异步  并行同步  并行异步的使用

#pragma mark-----串行同步
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(serialQueue, ^{
//        NSLog(@"%@",[NSThread currentThread]);
    });
#pragma mark-----串行异步
    dispatch_queue_t serialQueue1 = dispatch_queue_create("serialQueue1", DISPATCH_QUEUE_SERIAL);
    dispatch_async(serialQueue1, ^{
//        NSLog(@"%@",[NSThread currentThread]);
    });
#pragma mark----并行同步
    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(concurrentQueue, ^{
//        NSLog(@"%@",[NSThread currentThread]);
    });
#pragma mark----并行异步
    dispatch_queue_t concurrentQueue1 = dispatch_queue_create("concurrentQueue1", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(concurrentQueue1, ^{
        NSLog(@"%@",[NSThread currentThread]);
    });

三、具体实例 使用GCD加载多张图片

#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 imageTag;
    UIImageView *myImageView;
    dispatch_queue_t concurentQueue;
    
    NSOperationQueue *operationQueues;
} @end - (void)viewDidLoad {
    [super viewDidLoad];
    imageTag = 100;
    self.view.backgroundColor = [UIColor greenColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self controlBtn];
    /*
     1、创建多个视图
     2、找到并行队列
     3、给这个并行队列指定多个任务
     4、在子线程加载网络资源
     5、回到主线程
     6、更新UI
     */
//    1、创建多个视图
    for (int i=0; i<3; i++) {
        for (int j=0; j<2; j++) {
            myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10+j*200, 40+i*200, 190, 190)];
            myImageView.backgroundColor = [UIColor orangeColor];
            myImageView.tag = imageTag++;
            [self.view addSubview:myImageView];
        }
    }
// 2、找到并行队列
//    使用下面这个方式不按顺序  因为下面这句找的是  系统的全局并行队列
//    concurentQueue = dispatch_get_global_queue(0, 0);
//    这个方式是按顺序的  用的串行队列
    concurentQueue = dispatch_queue_create("concurentQueue", DISPATCH_QUEUE_SERIAL);
    
//    3、指定任务
    for (int index=0; index<6; index++) {
        dispatch_async(concurentQueue, ^{
           [NSThread sleepForTimeInterval:1];
//            加载网络资源
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
            UIImage *image = [UIImage imageWithData:data];
            
//            5、回到主线程
            dispatch_queue_t mainQueue = dispatch_get_main_queue();
            dispatch_sync(mainQueue, ^{
               
//                6、刷新UI
                for (int i=0; i<6; i++) {
                    UIImageView *iamgeView = [self.view viewWithTag:100+index];
                    iamgeView.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(concurentQueue);
        }break;
            
        case 1:{
            //            恢复队列
            dispatch_resume(concurentQueue);
            
        }break;
            
    }
    
    
    
}

iOS 开发线程 gcd的更多相关文章

  1. ios开发多线程--GCD

    引言 虽然GCD使用很广,而且在面试时也经常问与GCD相关的问题,但是我相信深入理解关于GCD知识的人肯定不多,大部分都是人云亦云,只是使用过GCD完成一些很简单的功能.当然,使用GCD完成一些简单的 ...

  2. iOS开发中GCD在多线程方面的理解

    GCD为Grand Central Dispatch的缩写. Grand Central Dispatch (GCD)是Apple开发的一个多核编程的较新的解决方法.在Mac OS X 10.6雪豹中 ...

  3. ios开发:GCD多线程

    ios有三种多线程编程技术,分别是NSThread,Cocoa NSOperation和GCD,GCD全称Grand Central Dispatch 是Apple开发的一个多核编程的解决方法,在iO ...

  4. IOS开发 多线程GCD

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

  5. IOS开发使用GCD后台运行

    什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...

  6. iOS开发 - 线程与进程的认识与理解

    进程: 进程是指在系统中正在运行的一个应用程序,比如同时打开微信和Xcode,系统会分别启动2个进程; 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内; 线程: 一个进程要想执行任务 ...

  7. iOS开发--线程通信

    线程间的通信主要用于主线程与子线程的,也有用于子线程与子线程的 介绍下面几种通信方式 1.利用GCD方式(推荐) - (void)touchesBegan:(NSSet<UITouch *> ...

  8. iOS 开发多线程 —— GCD(1)

    本文是根据文顶顶老师的博客学习总结而来,如有不妥之处,还望指出.http://www.cnblogs.com/wendingding/p/3807716.html 概览: /* 纯c语言,提供了非常多 ...

  9. iOS开发系列-GCD

    概述 GCD是苹果公司为多核的并行运算提出的解决方案.全称是Grand Central Dospatch.纯C语言,提供了非常多强大的函数. GCD自动管理线程的声明周期(创建线程.调度任务.销毁线程 ...

随机推荐

  1. JavaScript基本语法

    本节和CSS语法类似,理解这些语法以后,就可以按照Bootstrap的开发规范去开发自己的各种插件了. ||和&&运算符 ||表示,如果第一个元素可以转换为true,则返回第一个元素的 ...

  2. 课堂笔记--Strom并发模型

    Strom并发模型:     topology是如何运行的?(可与mapreduce对比)         第一层:cluster         第二层:supervisor(host.node.机 ...

  3. Codeforces Round #228 (Div. 1) A

    A. Fox and Box Accumulation time limit per test 1 second memory limit per test 256 megabytes input s ...

  4. 我要崩溃了,要解出这么一段js代码背后的东西,这真是一坨啊,别被高度欺骗了,他还有宽度!!!!!试着按下方向右键

    一坨js代码: function s_gi(un, pg, ss) { var c = "s.version='H.26';s.an=s_an;s.logDebug=function(m){ ...

  5. Ubuntu14.04安装MySQL-python异常: mysql_config: not found,Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-MJWMPd/MySQL-python/

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABUoAAAE8CAIAAACZ6RwMAAAgAElEQVR4nOydaVxTx9fHn9dhKWqltv

  6. sql server 行转列解决方案

    主要应用case语句来解决行转列的问题 行转列问题主要分为两类 1)简单的行转列问题: 示例表: id  sid           course  result 1   2005001 语文     ...

  7. asp.net GridView控件中诗选全选和全不选功能

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. 循序渐进Python3(一)-- 初识Python

    一.Python起源                

  9. linux获取系统启动时间

    1.前言 时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同.linux内核里面用一个名为jiffes的常量来计算时间戳.应用层有time.getdaytime等函数.今天需要 ...

  10. wpf 旋转效果

    <Grid> <Grid.Triggers> <EventTrigger RoutedEvent="Page.Loaded"> <Begi ...