//
// 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