多线程理解

这是让我自己加深记忆的,大神勿喷
 
GCD(Grand Central Dispatch)是苹果公司开发的,以优化应用程序支持多核和其他对称多处理系统的系统
GCD是函数级编程,所以更高效,功能也更强大
任务:
具有一定功能的代码段,一般是一个block或者函数。
分发队列:
GCD以队列的方式进行工作,FIFO(先进先出)
GCD会根据分发队列的不同,创建合适数量的线程执行队列中的任务。
队列分两种
一种是串行队列:(1,系统提供的,2自定义)
一种是并行队列:(1,系统提供的,2自定义)
//采用系统的方式添加串行的任务
- (IBAction)SerialMainAction:(UIButton *)sender {
//    系统提供的串行队列其实就是主线程队列
//    1拿到主线程
    dispatch_queue_t main = dispatch_get_main_queue();
//    2给主线程队列添加任务
    dispatch_async(main, ^{
        NSLog(@"omg%@",[NSThread currentThread]);
    });
    dispatch_async(main, ^{
        NSLog(@"two%@",[NSThread currentThread]);
    });
    dispatch_async(main, ^{
        NSLog(@"three%@",[NSThread currentThread]);
    });
    dispatch_async(main, ^{
        NSLog(@"four%@",[NSThread currentThread]);
    });
    dispatch_async(main, ^{
        NSLog(@"five%@",[NSThread currentThread]);
    });
    
}

上面的是串行队列,上面也说了,串行队列其实就是主线程队列,添加的几个任务是为了看出执行顺序

下面的是自定义的方式添加串行任务,里面的几个任务也是为了查看执行顺序

//采用自定义的方式添加串行任务
- (IBAction)SerialBySelfAction:(UIButton *)sender {
    //创建串行队列
    dispatch_queue_t serialQueue = dispatch_queue_create("yan5", DISPATCH_QUEUE_SERIAL);
//    2添加任务
    dispatch_async(serialQueue, ^{
        NSLog(@"one%@",[NSThread currentThread]);
    });
    dispatch_async(serialQueue, ^{
        NSLog(@"two%@",[NSThread currentThread]);
    });
    dispatch_async(serialQueue, ^{
        NSLog(@"three%@",[NSThread currentThread]);
    });
    dispatch_async(serialQueue, ^{
        NSLog(@"four%@",[NSThread currentThread]);
        
    });
    dispatch_async(serialQueue, ^{
        NSLog(@"five%@",[NSThread currentThread]);
    });
    
    
    
}

接下来是并行队列,第一种还是系统方式创建并行队列

 1 - (IBAction)GlobalAction:(UIButton *)sender {
 2 //    1拿到并行队列
 3     dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 4     dispatch_async(global, ^{
 5         NSLog(@"one%@",[NSThread currentThread]);
 6     });
 7     dispatch_async(global, ^{
 8         NSLog(@"two%@",[NSThread currentThread]);
 9     });
10     dispatch_async(global, ^{
11         NSLog(@"three%@",[NSThread currentThread]);
12     });
13     dispatch_async(global, ^{
14         NSLog(@"four%@",[NSThread currentThread]);
15     });
16     dispatch_async(global, ^{
17         NSLog(@"five%@",[NSThread currentThread]);
18     });
19     
20 }

然背后是自定义创建并行队列

//通过自定义方式创建并行队列
- (IBAction)CurrentAction:(UIButton *)sender {
    dispatch_queue_t concurrent = dispatch_queue_create("1146", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(concurrent, ^{
        NSLog(@"one%@",[NSThread currentThread]);
    });
    dispatch_async(concurrent, ^{
        NSLog(@"two%@",[NSThread currentThread]);
    });
    dispatch_async(concurrent, ^{
        NSLog(@"three%@",[NSThread currentThread]);
    });
    dispatch_async(concurrent, ^{
        NSLog(@"four%@",[NSThread currentThread]);
    });
    dispatch_async(concurrent, ^{
        NSLog(@"five%@",[NSThread currentThread]);
    });
}

上面的四种方法其实就是方法名的不同,在拿到队列时会有不一样,添加任务的方法是一样的以下是四种创建队列的方法名

一  系统串行队列  dispatch_queue_t main = dispatch_get_main_queue();

二 自定义串行队列 dispatch_queue_t serialQueue = dispatch_queue_create("yan5", DISPATCH_QUEUE_SERIAL);

三 系统并行队列 dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

四 自定义并行队列 dispatch_queue_t concurrent = dispatch_queue_create("1146", DISPATCH_QUEUE_CONCURRENT);

另外,自定义队列时都会有一个标记,也就是“”里面的东西

 

还有三个方法,1让任务延迟一段时间执行,2重复执行一个任务,3让程序运行期间只运行一次下面时代码

//让任务延迟一段时间执行
- (IBAction)DelayAction:(UIButton *)sender {
    double time = 3.0;
//    第一个参数:从什么时候开始延迟
//   第二个参数 延迟多长时间
    dispatch_time_t Dtime = dispatch_time(DISPATCH_TIME_NOW, time * NSEC_PER_SEC);
//    拿到队列
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    添加延时任务
    dispatch_after(Dtime, global, ^{
        NSLog(@"睡醒了");
    });
}
//重复执行一个任务
- (IBAction)RepeatAction:(UIButton *)sender {
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_queue_t global = dispatch_get_main_queue();//不能这样用
    NSArray * array = @[@"小胖",@"麻静",@"索菲"];
//    添加重复任务
//    第一个参数,重复多少次
//    第二个参数,放在哪个队列
//    任务的block块
    dispatch_apply(array.count, global, ^(size_t t) {
        NSLog(@"hahahaha arr[%zu]%@",t,array[t]);
    });
}

//让程序在运行期间只运行一次
- (IBAction)onlyOneAction:(UIButton *)sender {
//    1创建一个静态的线程
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        NSLog(@"就这一次");
    });
}

 

posted @ 2016-01-06 23:01  努力奋斗ing  阅读(164)  评论(0编辑  收藏  举报