IOS多线程之GCD

今天刚好有点时间,就写点IOS开发中经常用到的关于多线程使用最多的实现方法GCD,废话不多说,直接进入正题。

1.GCD是什么?

Grand Central Dispatch (多线程的优化技术) GCD是一套底层API,基于C语言开发的多线程机制,提供了新的模式编写并发执行的程序
特点:
    1.允许讲一个程序切分为多个单一任务,然后提交到工作队列中并发或者串行的执行
    2.为多核的并行运算提出了解决方案,自动合理的利用cpu内核(比如双核,四核)
    3.自动的管理线程的生命周期(创建线程,调度任务,销毁线程)我们都不需要处理,只需要dispatch即可,完全不需要我们去手动管理,只需要分配任务即可。
    4.配合block,使得使用起来更加方便灵活

2.什么是Queue队列?

GCD使用了队列的概念,解决了NSThread难于管理的问题,队列实质上就是数组的概念,通常我们把要执行的任务放到队列中管理
特点:
    1.按顺序执行,先进先出
    2.可以管理多线程的并发的任务,设置主线程
    3.GCD的队列是任务的队列,而不是线程的队列

3.GCD的任务队列有三种

1.自定义队列
2.获取全局队列
3.获取主队列

3.什么是任务?

任务即操作,你想要干什么,说白了就是一段代码,在GCD中,任务就是一个block
任务的两种执行方式:
同步执行:
    只要是同步的,都会在当前的线程中执行,不会另外开辟线程的
异步执行:
    只要是异步任务,都会开启新线程,在开启的线程中执行

4.什么是串行队列?

依次完成每一个任务

5.什么是并行队列?

好像所有的任务都是在同一时间执行的

6.mainQueue(主队列,串行);全局队列(Global Queue);自己创建的队列(Queue)

如果将任务放在一个队列中,那么任务将按照放入队列中的顺序在新开的一个线程中来执行,如果想让多个任务同时进行,可以加在多个队列中来解决

代码如下:

//
//  ViewController.m
//  01_GCDDemo
//
//  Created by lcc on 15/9/16.
//  Copyright (c) 2015年 lcc. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"current:%@",[NSThread currentThread]);
    NSLog(@"isMulti:%d\n\n",[NSThread isMultiThreaded]);

    //1.创建自己的队列
    [self createMyQueues];
}
#pragma mark -GCD任务队列-
/**
    GCD的任务队列有三种
    1.自定义队列
    2.获取全局队列
    3.获取主队列
 */

-(void)createMyQueues{
    //1.如何实现一个类似NSthread的功能?
    //2.如何将一个任务交给队列管理?
    //dispatch 它是GCD技术常用函数及变量名的前缀
    dispatch_queue_t myQueue = dispatch_queue_create("MyQueue", NULL);
    //参数一:给个运行的标记,可以为NULL
    //参数二:属性,一般为NULL

    //1.同步演示
//    dispatch_sync(myQueue, ^{
//        NSLog(@"current:%@",[NSThread currentThread]);
//        NSLog(@"isMulti:%d\n\n",[NSThread isMultiThreaded]);
//        [self printFirst];
//    });
//    dispatch_sync(myQueue, ^{
//        [self printForth];
//    });
//    dispatch_sync(myQueue, ^{
//        [self printThird];
//    });
//    dispatch_sync(myQueue, ^{
//        [self printSecond];
//    });


    //2.异步演示
//    dispatch_async(myQueue, ^{
//        NSLog(@"current:%@",[NSThread currentThread]);
//        NSLog(@"isMulti:%d\n\n",[NSThread isMultiThreaded]);
//        //不同的线程了,就是异步
//        
//        [self printFirst];
//        [self printSecond];
//        [self printForth];
//        [self printThird];
//    });

    //3.多队列演示
    dispatch_queue_t queue1 = dispatch_queue_create(NULL, NULL);
    dispatch_queue_t queue2 = dispatch_queue_create(NULL, NULL);
    dispatch_queue_t queue3 = dispatch_queue_create(NULL, NULL);
    dispatch_queue_t queue4 = dispatch_queue_create(NULL, NULL);

//    dispatch_async(queue1, ^{
//        NSLog(@"queue1:%@",[NSThread currentThread]);
//        [self printFirst];
//    });
//    dispatch_async(queue2, ^{
//        NSLog(@"queue2:%@",[NSThread currentThread]);
//        [self printSecond];
//    });
//    dispatch_async(queue3, ^{
//        NSLog(@"queue3:%@",[NSThread currentThread]);
//        [self printThird];
//        [self printForth];
//    });

    //4.全部完成后执行一件事(创建队列组)
    //创建组队列
    dispatch_group_t group = dispatch_group_create();
    //将事件统一到组里面
    dispatch_group_async(group, queue2, ^{
        [self printSecond];
    });
    dispatch_group_async(group, queue1, ^{
        [self printFirst];
    });
    dispatch_group_async(group, queue3, ^{
        [self printForth];
        [self printThird];
    });
    //组内方法都执行完后,给个通知,然后做某事
    dispatch_group_notify(group, queue4, ^{
        [self printFinished];
    });

}

#pragma mark -任务-

-(void)printFirst{
    for (NSInteger i = 0; i<10; i++) {
        NSLog(@"第一个任务执行第%ld步",(long)i);
        [NSThread sleepForTimeInterval:1.0];
    }
}
-(void)printSecond{
    for (NSInteger i = 0; i<10; i++) {
        NSLog(@"第二个任务执行第%ld步",(long)i);
        [NSThread sleepForTimeInterval:1.0];
    }
}
-(void)printThird{
    for (NSInteger i = 0; i<10; i++) {
        NSLog(@"第三个任务执行第%ld步",(long)i);
        [NSThread sleepForTimeInterval:1.0];
    }
}
-(void)printForth{
    for (NSInteger i = 0; i<10; i++) {
        NSLog(@"第四个任务执行第%ld步",(long)i);
        [NSThread sleepForTimeInterval:1.0];
    }
}
-(void)printFinished{
    NSLog(@"任务执行完毕");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

OK,大致就是这样,GCD在面试中经常会被问到,重要是理解这种多线程的机制,而且以后在项目中经常会用到GCD去优化程序,所以GCD很重要。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值