GCD 的使用
来源:互联网 发布:mysql 查询加分页 编辑:程序博客网 时间:2024/06/14 15:21
// ViewController.m
// UsedGCD
//
// Created by jinxin on 16/8/1.
// Copyright © 2016年 zhaixingzhi. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self mainSync];;
}
/**
* 主队列 + 异步任务 : 没有开启新线程,任务还是逐个完成的
*/
-(void)mainAsync
{
dispatch_queue_t queue =dispatch_get_main_queue();
dispatch_async(queue, ^{
for (int
i = 0; i< 6; i++) {
NSLog(@"1----%@",[NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i< 6; i++) {
NSLog(@"2----%@",[NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i< 6; i++) {
NSLog(@"3----%@",[NSThreadcurrentThread]);
}
});
}
/**
* 主队列 + 同步任务:会造成死锁的进程 【切记】:不能字啊主队列中添加同步任务
*/
-(void)mainSync
{
NSLog(@"---1");
dispatch_queue_t queue =dispatch_get_main_queue();
dispatch_sync(queue, ^{
NSLog(@"不会打印");
});
NSLog(@"---2");
}
/**
* 串行队列 +异步任务,开启了新的线程,任务是逐个完成的
*/
-(void)serialAsync
{
dispatch_queue_t queue =dispatch_queue_create("queue",NULL);
dispatch_async(queue, ^{
for (int
i = 0; i< 6; i++) {
NSLog(@"1----%@",[NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i< 6; i++) {
NSLog(@"2----%@",[NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i< 6; i++) {
NSLog(@"3----%@",[NSThreadcurrentThread]);
}
});
}
/**
* 串行队列 + 同步任务:没有开启新的线程,任务是逐个完成
*/
-(void)serialSync
{
//创建串行队列
dispatch_queue_t queue =dispatch_queue_create("queue",DISPATCH_QUEUE_SERIAL);//这里可以用DISPATCH_QUEUE_SERIAL或者NULL都是串行队列
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-1--%@",[NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-2--%@",[NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-3--%@",[NSThreadcurrentThread]);
}
});
}
/**
* 全局队列 + 异步任务 : 开启新的线程,任务是并发的
*/
-(void)globalAsync
{
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-1--%@",[NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-2--%@",[NSThreadcurrentThread]);
}
});
dispatch_async(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-3--%@",[NSThreadcurrentThread]);
}
});
}
/**
* 全局队列 + 同步任务 : 没有开启新线程 ,任务是逐个完成的
*/
-(void)globalSync
{
//获得全局队列
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-1--%@",[NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-2--%@",[NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-3--%@",[NSThreadcurrentThread]);
}
});
}
/**
* 并发队列 + 异步任务 : 开启了新线程,任务是并发的
*
*/
-(void)concurrentAsunc
{
//创建并发队列
dispatch_queue_t queue =dispatch_queue_create("myQueue",DISPATCH_QUEUE_CONCURRENT);
//异步任务
dispatch_async(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-1--%@",[NSThreadcurrentThread]);
}
});
//异步任务
dispatch_async(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-2--%@",[NSThreadcurrentThread]);
}
});
//异步任务
dispatch_async(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-3--%@",[NSThreadcurrentThread]);
}
});
}
/**
* 并发队列 + 同步任务 : 没有开启新的线程,任务逐个进行
*/
-(void)concurrentSync
{
//创建并行队列
dispatch_queue_t queue =dispatch_queue_create("myQueue",DISPATCH_QUEUE_CONCURRENT);
//在对垒中创建任务
//同步任务
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-1--%@",[NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-2--%@",[NSThreadcurrentThread]);
}
});
dispatch_sync(queue, ^{
for (int i =0; i < 5; i++) {
NSLog(@"-3--%@",[NSThreadcurrentThread]);
}
});
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- 多线程GCD的使用
- GCD的使用.
- GCD的使用
- 多线程GCD的使用
- 多线程GCD的使用
- iOS GCD的使用
- GCD的使用
- 多线程GCD的使用
- 多线程GCD的使用
- 多线程GCD的使用
- GCD的使用
- 多线程GCD的使用
- GCD的使用
- 多线程GCD的使用
- gcd的使用
- GCD的简单使用
- GCD的基本使用
- GCD的使用
- Linux 高可用(HA)集群之DRBD详解
- windows server 下80端口被System进程占用的解决方法
- hadoop 2.x简单介绍
- Android 面试问题参考
- 取出json中[{},{}] 的数据 ,使用 for-in 来循环对象的属性
- GCD 的使用
- linux 压缩文件夹的一部分
- Spring boot 九 集成 FreeMark
- PTA 树的同构
- java源码修炼(三) getModel()
- spring boot的一些配置二
- RAC环境中threads变更后如何确保goldengate继续正常复制
- linux shell 脚本之深入浅出的grep的用法
- php 字符串编码转换