zoukankan      html  css  js  c++  java
  • 多线程热恋

     1  - (void)gcdTest1 {
     2 
     3     
     4 
     5     //1.GCD应用 单例模式
     6 
     7     
     8 
     9     static dispatch_once_t onceToken;
    10 
    11     dispatch_once(&onceToken, ^{
    12 
    13     
    14 
    15         NSLog(@"执行一次%@",[NSThread currentThread]); 
    16 
    17     });
    18 
    19 }
     1 - (void)gcdTest2 {
     2 
     3     
     4 
     5     //延迟操作
     6 
     7     //1、
     8 
     9     NSLog(@"开始执行");
    10 
    11     
    12 
    13     //    [self performSelector:@selector(handleAction) withObject:nil afterDelay:0];
    14 
    15     
    16 
    17     //2、
    18 
    19     
    20 
    21     //DISPATCH_TIME_NOW 从现在开始
    22 
    23     //delayInSeconds 延迟几秒
    24 
    25     
    26 
    27     dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));
    28 
    29     
    30 
    31     //    when dispatch_time_t
    32 
    33     
    34 
    35     dispatch_after(time, dispatch_get_main_queue(), ^{
    36 
    37         
    38 
    39         NSLog(@"%@",[NSThread currentThread]);
    40 
    41         
    42 
    43     });
    44 
    45  
    46 
    47 }
    - (void)gcdTest2 {
    
        
    
        //延迟操作
    
        //1、
    
        NSLog(@"开始执行");
    
        
    
        //    [self performSelector:@selector(handleAction) withObject:nil afterDelay:0];
    
        
    
        //2、
    
        
    
        //DISPATCH_TIME_NOW 从现在开始
    
        //delayInSeconds 延迟几秒
    
        
    
        dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));
    
        
    
        //    when dispatch_time_t
    
        
    
        dispatch_after(time, dispatch_get_main_queue(), ^{
    
            
    
            NSLog(@"%@",[NSThread currentThread]);
    
            
    
        });
    
     
    
    }
     
     1 - (void)gcdTest3 {
     2 
     3     
     4 
     5     //创建调度组
     6 
     7     dispatch_group_t group = dispatch_group_create();
     8 
     9     //获取全局队列
    10 
    11     dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    12 
    13     //调度组的异步请求
    14 
    15     dispatch_group_async(group, queue, ^{
    16 
    17         
    18 
    19         [NSThread sleepForTimeInterval:2];
    20 
    21         NSLog(@"下载第一张图");
    22 
    23     });
    24 
    25     dispatch_group_async(group, queue, ^{
    26 
    27         [NSThread sleepForTimeInterval:3];
    28 
    29         
    30 
    31         NSLog(@"下载第二张图");
    32 
    33     });
    34 
    35     dispatch_group_async(group, queue, ^{
    36 
    37         [NSThread sleepForTimeInterval:1];
    38 
    39         
    40 
    41         NSLog(@"下载第三张图");
    42 
    43     });
    44 
    45     dispatch_group_async(group, queue, ^{
    46 
    47         [NSThread sleepForTimeInterval:5];
    48 
    49         
    50 
    51         NSLog(@"下载第四张图");
    52 
    53     });
    54 
    55     //notify通知,当所有异步请求完成时调用dispatch_group_notify
    56 
    57     dispatch_group_notify(group, queue, ^{
    58 
    59         NSLog(@"更新UI");
    60 
    61     });
    62 
    63     
    64 
    65 }
     1 //队列调度的第二种方法
     2 
     3 - (void)gcdTest4 {
     4 
     5     
     6 
     7     //创建调度组
     8 
     9     dispatch_group_t group = dispatch_group_create();
    10 
    11     //获取全局队列
    12 
    13     dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    14 
    15     
    16 
    17     //进入队列
    18 
    19     dispatch_group_enter(group);
    20 
    21     dispatch_async(queue, ^{
    22 
    23         
    24 
    25         [NSThread sleepForTimeInterval:2];
    26 
    27         NSLog(@"下载第一张图");
    28 
    29         
    30 
    31         //离开队列
    32 
    33         dispatch_group_leave(group);
    34 
    35     });
    36 
    37     
    38 
    39     dispatch_group_enter(group);
    40 
    41     dispatch_async(queue, ^{
    42 
    43         
    44 
    45         [NSThread sleepForTimeInterval:1];
    46 
    47         NSLog(@"下载第er张图");
    48 
    49         
    50 
    51         dispatch_group_leave(group);
    52 
    53     });
    54 
    55     
    56 
    57     dispatch_group_enter(group);
    58 
    59     dispatch_async(queue, ^{
    60 
    61         
    62 
    63         [NSThread sleepForTimeInterval:3];
    64 
    65         NSLog(@"下载第san张图");
    66 
    67         
    68 
    69         dispatch_group_leave(group);
    70 
    71     });
    72 
    73     
    74 
    75     //等待调度队列wait相当于一个阻塞状态
    76 
    77     dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    78 
    79     
    80 
    81     NSLog(@"更新UI");
    82 
    83     
    84 
    85 }
     1 //中断操作
     2 
     3  
     4 
     5 - (void)gcdTest5 {
     6 
     7     
     8 
     9     //dispatch_barrier_async一定是自定义队列
    10 
    11     //这里指定的并发队列应该是自己通过dispatch_queue_create函数创建的。如果你传的是一个串行队列或者全局并发队列,这个函数等同于dispatch_async函数。
    12 
    13     
    14 
    15     dispatch_queue_t queue = dispatch_queue_create("com.bjsxt", DISPATCH_QUEUE_CONCURRENT); //dispatch_get_global_queue(0, 0);
    16 
    17     
    18 
    19     dispatch_async(queue, ^{
    20 
    21         [NSThread sleepForTimeInterval:1];
    22 
    23         NSLog(@"1");
    24 
    25     });
    26 
    27     
    28 
    29     dispatch_async(queue, ^{
    30 
    31         [NSThread sleepForTimeInterval:3];
    32 
    33         
    34 
    35         NSLog(@"2");
    36 
    37     });
    38 
    39     dispatch_async(queue, ^{
    40 
    41         
    42 
    43         [NSThread sleepForTimeInterval:2];
    44 
    45         
    46 
    47         NSLog(@"3");
    48 
    49     });
    50 
    51     
    52 
    53     //中断操作dispatch_barrier_async
    54 
    55     dispatch_barrier_async(queue, ^{
    56 
    57         NSLog(@"--------");
    58 
    59         [NSThread sleepForTimeInterval:1];
    60 
    61         
    62 
    63     });
    64 
    65     
    66 
    67     dispatch_async(queue, ^{
    68 
    69         
    70 
    71         [NSThread sleepForTimeInterval:1];
    72 
    73         NSLog(@"4");
    74 
    75     });
    76 
    77     dispatch_async(queue, ^{
    78 
    79         NSLog(@"5");
    80 
    81     });
    82 
    83 }
     1 - (void)gcdTest6 {
     2 
     3     
     4 
     5     //遍历操作
     6 
     7     dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
     8 
     9     //iterations 遍历的次数
    10 
    11     dispatch_apply(5, queue, ^(size_t i) {
    12 
    13         NSLog(@"%@",@(i));
    14 
    15     });
    16 
    17 }
  • 相关阅读:
    LeetCode OJ String to Integer (atoi) 字符串转数字
    HDU 1005 Number Sequence(AC代码)
    HDU 1004 Let the Balloon Rise(AC代码)
    HDU 1003 Max Sum(AC代码)
    012 Integer to Roman 整数转换成罗马数字
    011 Container With Most Water 盛最多水的容器
    010 Regular Expression Matching 正则表达式匹配
    007 Reverse Integer 旋转整数
    006 ZigZag Conversion
    005 Longest Palindromic Substring 最长回文子串
  • 原文地址:https://www.cnblogs.com/iOSlearner/p/5307828.html
Copyright © 2011-2022 走看看