多线程

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,assign)NSInteger count;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//单利

//    static dispatch_once_t onceToken;

//    dispatch_once(&onceToken,^{

//

//    });

//线程互斥

//创建线程锁

NSLock *lock =[[NSLock alloc]init];

//为了防止循环引用

self.count =100;//票数

//创建并行队列

dispatch_queue_t queue =dispatch_queue_create("com.selltikets.www", DISPATCH_QUEUE_CONCURRENT);

__weak ViewController *weakself = self;

for (int i=0; i<10; i++) {

dispatch_async(queue, ^{

[lock lock];//枷锁

for (int j =0; j<10; j++) {

NSLog(@"买到了第%ld张票",weakself.count);

weakself.count--;

}

[lock unlock];//解锁

});

}

// Do any additional setup after loading the view, typically from a nib.

}

/*1.GCD是最简单的多线程,也是效果最高的一种方式,全部是C语言代码编码编写的API,也是是苹果公司主推的一种多线程

2.GCD 通过queue来实现多线程

3.GCD里面有多重queue 一种是串行serial一种是并行concurrent.

*/

//串行队列

- (IBAction)serialqueue:(UIButton *)sender {

//关键字serial 特点,第一个任务执行完成,第二个任务才开始执行,一次类推

//有两种方式

#pragma  mark ---串行队列第一种

//    dispatch_queue_t  queue =dispatch_get_main_queue();

//    //往队列里面添加任务

//    dispatch_async(queue, ^{

//        NSLog(@"这是第一个任务,当前线程%@,是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"这是第二个任务,当前任务是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"这是第三个任务,当前任务是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"这是第四个任务,这是当前线程%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

#pragma mark ---串行队列第二种--------------

/*

获取串行队列第二种方式:自己创建队列,

*

*

穿件串行队列的第二种方式

@Pram "serialQueue" 队列的名字(苹果主推使用反向域名去命名)

@pram DISPATCH_QUEUE_SERIAL 队列的类型

手动创建的串行不在主线程中

*/

//

//    dispatch_queue_t queue =dispatch_queue_create("com.serialQueue.www", DISPATCH_QUEUE_SERIAL);

//    dispatch_async(queue, ^{

//        NSLog(@"这是第一个任务,当前线程是%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"这是第二个任务呀当前线程%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"这是第三个任务,当前任务是:%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"这是第四个任务,当前任务是%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

}

//并行队列

- (IBAction)concurrentQueue:(UIButton *)sender {

//concurrent 特点:第一个任务执行开始之后,第二个任务,不等第一个执行完毕,直接开始执行.依次类推,后面的任务跟前面的没有关系,先添加的任务不一定先执行,后面添加的不一定最后执行.并行队列会根据队列里面的任务数量CPU使用情况开辟最合适的线程数量,去完成队列里的任务.

//创建有两种方式

#pragma mark -----concurentQueue--第一种方式-------

//    dispatch_queue_t queue=dispatch_queue_create("com.concurrentQueue.www", DISPATCH_QUEUE_CONCURRENT);

//    dispatch_async(queue, ^{

//        NSLog(@"%@%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"第一个任务当前线程%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"第二个任务当前线程%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"第三个任务当前线程%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

//    dispatch_async(queue, ^{

//        NSLog(@"第四个任务,当前线程%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

//    });

#pragma mark -----concurrentQueue__第二种创建方式

//创建globalqueue是苹果里面的全局队列,有四个优先级 第一个参数DISPATCH_QUEUE_PRIORITY_DEFAULT  队列的优先级, 第二个是预留的参数,为了以后使用,目前还没使用写0;

//#define DISPATCH_QUEUE_PRIORITY_HIGH 2

//#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0

//#define DISPATCH_QUEUE_PRIORITY_LOW (-2)

//#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN

dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(queue, ^{

NSLog(@"第一个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第二个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第三任务当前线程%@是否主线称%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第四个任务当前 线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第五个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第六个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第七个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第八个任务当前线程%@是否主线成%d",[NSThread currentThread ],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第九个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第十个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

}

//延迟执行代码(dispatch_after 可以再任何队列中执行,串行 并行,都可以);

- (IBAction)afterbutton:(UIButton *)sender {

/*

第一种

*/

//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

//        NSLog(@"%@%d",[NSThread currentThread],[NSThread isMainThread]);

//    });

//第二种

dispatch_time_t seconds =dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC));

//创建一个全局队列

dispatch_queue_t queue =dispatch_get_global_queue(0, 0);

//添加到队列中

dispatch_after(seconds, queue, ^{

NSLog(@" 我是延迟任务当前任务线程%@是否是主线程 %d",[NSThread currentThread],[[NSThread currentThread]isMainThread] );

});

}//方法结尾括号

//线程组

- (IBAction)groupbutton:(UIButton *)sender {

//线程组:disoatch_group_t主要是吧一些不想关的任务就归为一组

//组里面放的是队列

//dispatch_group_async给组里面的队列添加任务

//dispatch_group_notify 作用是监听组里面的任务,等到组里面的任务全部执行完之后,才会执行里面的任务.

//1.创建组

dispatch_group_t group = dispatch_group_create();

//2.创建全局队列

dispatch_queue_t queue =dispatch_get_global_queue(0, 0);

//3.往组里添加队列

dispatch_group_async(group, queue, ^{

NSLog(@"我是第一个任务当前线程%@是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_group_notify(group, queue, ^{

NSLog(@"我是最后一个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_group_async(group, queue, ^{

NSLog(@"这是第二个任务当前线程%@是否主线程%d",[NSThread currentThread ],[[NSThread currentThread]isMainThread]);

});

dispatch_group_async(group, queue, ^{

NSLog(@"我是第三个任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

}//方法结尾括号

//同时进行读写

- (IBAction)readandwriteatsametime:(UIButton *)sender {

//数据库的读取 可以并发执行,通过GCD里面的并行去实现

//数据库的写入,只能串发执行,通过GCD里面的串行队列去实现;

//但是真正的项目肯定是既有数据的读写也有数据的写入;如何解决" dispatch _barrier_async 在它之前的任务可以区域并发执行,在他之后的任务也可以并发执行

//创建一个并行队列

dispatch_queue_t queue =dispatch_queue_create("con.concurrent.www", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue, ^{

NSLog(@"第一任务当前线程%@是否主线成%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第二任务,当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第三任务当前先成%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第四线程当前线程%@是否主线成%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第五任务当前任务%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_barrier_async(queue, ^{

NSLog(@"我正在读取数据%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第六人物当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第七任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第八任务呀当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第九任务当前线程%@是否主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);

});

dispatch_async(queue, ^{

NSLog(@"第十个任务当前线程%@是否主线程%d",[NSThread currentThread ],[[NSThread currentThread]isMainThread]);

});

}

//多次执行

- (IBAction)moretimes:(UIButton *)sender {

NSArray *array =@[@"1",@"2",@"3"];

//同城和数组配合使用

dispatch_queue_t queue =dispatch_get_global_queue(0, 0);

/*

第一个参数 次数

第二个参数 队列

第三个参数 任务

*/

//index:记录当前执行的第几次  ,是小于随机次数的

dispatch_apply(3, queue, ^(size_t index ) {

NSLog(@"%@",array[index]);

});

}

//async和sync的区别

- (IBAction)syncandasyncdiffrent:(UIButton *)sender {

//async不等block体执行完毕就去执行下面的代码

//sync 会等block体执行完毕之后才会执行block;

dispatch_queue_t queue=dispatch_get_global_queue(0, 0);

//    dispatch_async(queue, ^{

//        NSLog(@"这是第一个任务");

//    });

//    NSLog(@"呵呵");

//    dispatch_async(queue, ^{

//        NSLog(@"这是第二个任务");

//    });

//    NSLog(@"哦");

dispatch_sync(queue, ^{

NSLog(@"第一个任务");

});

NSLog(@"heh");

dispatch_sync(queue, ^{

NSLog(@"第二个任务");

});

NSLog(@"o");

}//

//gcd调用函数指针

- (IBAction)functionpointer:(UIButton *)sender {

dispatch_queue_t queue=dispatch_get_global_queue(0, 0);

/*

第二个参数<#void *context#>函数参数的内容

第三个参数<#dispatch_function_t work#> 函数(函数的返回值为void 参数类型必须为void)

*/

dispatch_async_f(queue, @"hehe", function);

}//

void function(void *context){

NSLog(@"%@",context);

printf("o");

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,513评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,312评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,124评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,529评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,937评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,913评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,084评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,816评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,593评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,788评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,267评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,601评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,265评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,158评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,953评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,066评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,852评论 2 277

推荐阅读更多精彩内容

  • #import "ViewController.h" @interface ViewController () @...
    艾克12138阅读 191评论 0 0
  • NSThread 第一种:通过NSThread的对象方法 NSThread *thread = [[NSThrea...
    攻城狮GG阅读 740评论 0 3
  • 一、前言 上一篇文章iOS多线程浅汇-原理篇中整理了一些有关多线程的基本概念。本篇博文介绍的是iOS中常用的几个多...
    nuclear阅读 1,994评论 6 18
  • 一、前言 本篇博文介绍的是iOS中常用的几个多线程技术: NSThread GCD NSOperation 由于a...
    和珏猫阅读 535评论 0 1
  • 今天上午,我为10月的两场大型朗诵会做准备。我要朗诵裴多菲的《我愿意是急流》和叶芝的《蜉蝣》。这两首诗,很有意思,...
    小坏蛋格瑞特阅读 331评论 2 3