zoukankan      html  css  js  c++  java
  • iOS中的串行,并行,分组,一次,障碍,延迟,反复执行(GCD)

    #import "OneViewController.h"
    
    @interface OneViewController ()
    
    @end
    
    @implementation OneViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark 方法
    //依靠C语言来执行,效率最高
    
    //串行队列
    - (IBAction)serialBtn:(id)sender {
        //1.创建行对列
        
        //1.获取系统创建好的串行对列,在主线程中是实现线程同步
        dispatch_queue_t queue1 = dispatch_get_main_queue();
        
        
        //2.自己创建串行队列,任务在子线程中实现线程同步
        dispatch_queue_t queue2 = dispatch_queue_create("com.lanou3g.oa", DISPATCH_QUEUE_SERIAL); //字符串 -- 唯一标示的作用(反域名形式),DISPATCH_QUEUE_SERIAL -- 指定为串行
        //2.往队列中添加任务
        dispatch_async(queue2, ^{
            NSLog(@"任务一:%@",[NSThread currentThread]);
            
        });
        dispatch_async(queue2, ^{
            NSLog(@"任务二:%@",[NSThread currentThread]);
            
        });
        dispatch_async(queue2, ^{
            NSLog(@"任务三:%@",[NSThread currentThread]);
            
        });
        dispatch_async(queue2, ^{
            NSLog(@"任务四:%@",[NSThread currentThread]);
            
        });
        
        //释放
        dispatch_release(queue2);
        
    }
    
    //并行(并发)队列
    - (IBAction)parallelBtn:(id)sender {
        //1.创建并发队列,系统创建好了四个
        //1.使用系统创建好的并发队列
        dispatch_queue_t queue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 参数分别是:优先级,预留参数
        //2.自己创建并发队列
        dispatch_queue_t queue2 = dispatch_queue_create("com.lanou3g.oa", DISPATCH_QUEUE_CONCURRENT); // 参数1.唯一标示,参数二 指定为并发队列
        
        //3.往队列中添加任务
        dispatch_async(queue2, ^{
            NSLog(@"任务1: %@",[NSThread currentThread]);
        });
        
        dispatch_async(queue2, ^{
            NSLog(@"任务2: %@",[NSThread currentThread]);
        });
        dispatch_async(queue2, ^{
            NSLog(@"任务3: %@",[NSThread currentThread]);
        });
        dispatch_async(queue2, ^{
            NSLog(@"任务4: %@",[NSThread currentThread]);
        });
        //请求图片
        //跳转到主线程刷新界面
        dispatch_async(dispatch_get_main_queue(),^{
        //更新 UI 显示图片
        
        });
        //释放
        dispatch_release(queue2);
        
        
    }
    
    //分组任务
    - (IBAction)groupBtn:(id)sender {
        //1.创建并行队列
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        //2.创建分组
        dispatch_group_t group = dispatch_group_create();
        //3.方分组中添加任务
        dispatch_group_async(group, queue, ^{
            NSLog(@"任务一,请求 0 - 20 M数据");
        });
        dispatch_group_async(group, queue, ^{
            NSLog(@"任务二,请求 0 - 20 M数据");
        });
        dispatch_group_async(group, queue, ^{
            NSLog(@"任务三,请求 0 - 20 M数据");
        });
        dispatch_group_async(group, queue, ^{
            NSLog(@"任务四,请求 0 - 20 M数据");
        });
        dispatch_group_async(group, queue, ^{
            NSLog(@"任务五,请求 0 - 20 M数据");
        });
        //4.当分组内的所有任务完成之后,拼接操作
        dispatch_group_notify(group, queue, ^{
            NSLog(@"拼接数据吧!");
        });
        //释放
        dispatch_release(group);
    }
    //一次
    - (IBAction)oneBtn:(id)sender {
    }
    //障碍
    - (IBAction)obstacleeBtn:(id)sender {
        //障碍任务的作用:可以保证障碍之后的并发的任务,必须在障碍之前的并发的任务执行结束之后,才可以开始执行
        //注意:要添加障碍任务,必须要使用自己创建的并发队列.
        //创建并发队列
         dispatch_queue_t queue = dispatch_queue_create("com.lanou.3g.oa",DISPATCH_QUEUE_CONCURRENT);
        //往队列中添加任务
        dispatch_async(queue, ^{
            NSLog(@"A写入文件");
        });
        dispatch_async(queue, ^{
            NSLog(@"B写入文件");
        });
        dispatch_async(queue, ^{
            NSLog(@"C写入文件");
        });
        dispatch_async(queue, ^{
            NSLog(@"D写入文件");
        });
        //添加(设置)障碍任务
        dispatch_barrier_async(queue, ^{
            NSLog(@"此处是障碍任务");
        });
        dispatch_async(queue, ^{
            NSLog(@"读取文件");
        });
        //释放
        dispatch_release(queue);
    }
    //延迟
    - (IBAction)delayBtn:(id)sender {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"5秒,五秒");
            
        }); //dispatch_get_main_queue 在主线程中执行,如果想要在子线程中执行,此处改为子线程即可
        
    }
    
    //反复执行
    - (IBAction)diReceiveMemoryBtn:(id)sender {
    //    dispatch_apply(10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
    //        NSLog(@"反复执行的次数:%ld,当前线程%@",index,[NSThread currentThread]);
    //    });
        
        dispatch_apply(10, dispatch_get_main_queue(), ^(size_t index) {
            NSLog(@"反复执行的次数:%ld,当前线程%@",index,[NSThread currentThread]);
        });
    }
    @end

    //单例

    #import "Singleton.h"
    
    @implementation Singleton
    //+ (id)mainSingleton{
    //    static Singleton *single = nil;
    //    @synchronized(self){
    //        if(!single){
    //            single = [[Singleton alloc] init];
    //        }
    //        
    //    }
    //    return single;
    //}
    
    + (id)mainSingleton{
    static Singleton *single = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //写只执行一次的代码
        single = [[Singleton alloc] init];
    });
    
    
    
    return single;
    }
    
  • 相关阅读:
    基于visual Studio2013解决C语言竞赛题之1054抽牌游戏
    基于visual Studio2013解决C语言竞赛题之1053洗牌
    基于visual Studio2013解决C语言竞赛题之1052求根
    CSS样式
    CSS引入方式,高级选择器
    CSS基础,选择器
    html基础
    sql server链接查询
    [置顶]动态网页开发基础【笔记】
    sql server链接查询
  • 原文地址:https://www.cnblogs.com/wohaoxue/p/4817445.html
Copyright © 2011-2022 走看看