IOS-GCDQueue使用

本文详细介绍了GCD在iOS开发中的使用方法,包括异步线程执行任务、主线程更新UI、执行多个任务、延时执行任务、使用Timer定时任务以及信号量的运用。通过具体的代码实例,展示了如何利用GCD进行高效的并发编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  ViewController.m
//  GCD
//
//  Created by YouXianMing on 15/10/19.
//  Copyright © 2015年 ZiPeiYi. All rights reserved.
//

#import "ViewController.h"
#import "GCD.h"

@interface ViewController ()

@property (nonatomic, strong) GCDTimer *timer;
@property (nonatomic, strong) GCDTimer *timer1;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
   /***********异步线程执行耗时任务,主线程更新UI***************/
    /*1.*/
    [[GCDQueue globalQueue] execute:^{
          //异步线程
        [[GCDQueue  mainQueue]execute:^{
           //异步主线程update UI
        }];
    }];
     /*2.*/
    [GCDQueue executeInGlobalQueue:^{
        //异步线程
        [GCDQueue executeInMainQueue:^{
            //异步主线程update UI
        }];
    }];
     /*3.*/
    GCDGroup* groupNor = [GCDGroup new];
    [GCDQueue executeInGlobalQueue:^{
       //异步线程加入线程组
    } inGroup:groupNor];
    [[GCDQueue mainQueue] notify:^{
    //线程组执行以后唤醒主线程更新UI
    } inGroup:groupNor];
    
    
    
/***********执行多个任务***************/
    /*1.执行没有顺序*/
    [[GCDQueue globalQueue] execute:^{
    }];
    [[GCDQueue globalQueue] execute:^{
    }];
    [GCDQueue executeInGlobalQueue:^{
    }];
    [GCDQueue executeInGlobalQueue:^{
    }];

    /*2.异步线程执行没有顺序,notify等待唤醒*/
    GCDGroup* groupNorMore = [GCDGroup new];
    [GCDQueue executeInGlobalQueue:^{
        //异步线程加入线程组
    } inGroup:groupNorMore];
    [[GCDQueue mainQueue] notify:^{
        //线程组执行以后唤醒主线程更新UI
    } inGroup:groupNorMore];
    
  /***********延时执行任务***************/
 
    [[GCDQueue mainQueue] execute:^{
    } afterDelaySecs:3 ];
    
 /**********Timer**************/
    self.timer = [[GCDTimer alloc]init];
    [self.timer event:^{
    } cancelEvent:^{
    } timeIntervalWithSecs:1];
    [self.timer  start];
    [self.timer  destroy];

      //自己开启和关闭
    self.timer1 = [[GCDTimer alloc]init];
    [self.timer1 eventAutoTimesEvent:^(NSInteger times) {
    } completEvent:^{
    } timeIntervalWithSecs:1 withTimes:10];
    
    /**********信号量**************/
    //设置0个信号量
    GCDSemaphore *sem = [[GCDSemaphore alloc] initWithValue:0];
    
    [GCDQueue executeInGlobalQueue:^{
        NSLog(@"1____");
        [sem wait];
        NSLog(@"run task 1");
        sleep(1);
        NSLog(@"complete task 1");
        [sem  signal];
    }];
    [GCDQueue executeInGlobalQueue:^{
        NSLog(@"2____");
        [sem wait];
        NSLog(@"run task 2");
        sleep(1);
        NSLog(@"complete task 2");
        [sem  signal];
    }];
    [GCDQueue executeInGlobalQueue:^{
        NSLog(@"3____");
        [sem wait:3];//等待三秒后执行下边方法
        NSLog(@"run task 3");
        sleep(1);
        NSLog(@"complete task 3");
        [sem  signal];//信号量增加执行其他的
    }];

    
}

@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值