• 同步,异步,串行,并行队列


      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 
      5 @end
      6 
      7 @implementation ViewController
      8 
      9 - (void)viewDidLoad {
     10     [super viewDidLoad];
     11     // Do any additional setup after loading the view, typically from a nib.
     12     //[self testSyncWithSerialQueue];
     13     //[self testSyncWithConcurrentQueue];
     14     //[self testAsyncWithSerialQueue];
     15     [self testAsyncWithConcurrentQueue];
     16 }
     17 
     18 
     19 
     20 //1.同步方式向串行队列提交代码
     21 //前面的线程执行完成后,才开始执行后面的线程
     22 -(void)testSyncWithSerialQueue
     23 {
     24     //创建串行队列
     25     dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
     26     
     27     //线程一
     28     dispatch_sync(serialQueue, ^{
     29         for (int i=0; i<100; i++) {
     30             NSLog(@"线程一:%d",i);
     31         }
     32     });
     33     
     34     //线程二
     35     dispatch_sync(serialQueue, ^{
     36         for (int i=0; i<100; i++) {
     37             NSLog(@"线程二:%d",i);
     38         }
     39     });
     40 }
     41 
     42 //2.同步方式向并行队列提交代码
     43 //前面的线程执行完成后.才开始执行后面的线程
     44 -(void)testSyncWithConcurrentQueue
     45 {
     46     //创建并行的队列
     47     dispatch_queue_t conQueue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);
     48     
     49     //线程一
     50     dispatch_sync(conQueue, ^{
     51         for (int i=0; i<100; i++) {
     52             NSLog(@"线程一:%d",i);
     53         }
     54     });
     55     
     56     //线程二
     57     dispatch_sync(conQueue, ^{
     58         for (int i=0; i<100; i++) {
     59             NSLog(@"线程二:%d",i);
     60         }
     61     });
     62 }
     63 
     64 //3.异步方式向串行队列提交代码
     65 //前面的线程执行完成后,才开始执行后面的线程
     66 -(void)testAsyncWithSerialQueue
     67 {
     68     //创建串行队列
     69     dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
     70     
     71     //线程一
     72     dispatch_async(serialQueue, ^{
     73         for (int i=0; i<100; i++) {
     74             NSLog(@"线程一:%d",i);
     75         }
     76     });
     77     
     78     //线程二
     79     dispatch_async(serialQueue, ^{
     80         for (int i=0; i<100; i++) {
     81             NSLog(@"线程二:%d",i);
     82         }
     83     });
     84 }
     85 
     86 //3.异步方式向并行队列提交代码
     87 //前面的线程执行和后面的线程同时执行
     88 -(void)testAsyncWithConcurrentQueue
     89 {
     90     //创建并行队列
     91     dispatch_queue_t conQueue = dispatch_queue_create("conQueue", DISPATCH_QUEUE_CONCURRENT);
     92     
     93     //线程一
     94     dispatch_async(conQueue, ^{
     95         for (int i=0; i<100; i++) {
     96             NSLog(@"线程一:%d",i);
     97         }
     98     });
     99     
    100     //线程二
    101     dispatch_async(conQueue, ^{
    102         for (int i=0; i<100; i++) {
    103             NSLog(@"线程二:%d",i);
    104         }
    105     });
    106 }
    107 
    108 - (void)didReceiveMemoryWarning {
    109     [super didReceiveMemoryWarning];
    110     // Dispose of any resources that can be recreated.
    111 }
    112 
    113 @end
  • 相关阅读:
    Jenkins 集成Sonar scanner的使用案例
    Sonarqube 安装 及与Jenkins sonar scanner插件集成部署
    shell if 判断匹配1位数字
    使用tcpdump抓包
    golang + snap7 对西门子plc进行读写
    python基础练习题(题目 求输入数字的平方,如果平方运算后小于 50 则退出)
    python基础练习题(题目 统计 1 到 100 之和)
    python基础练习题(题目 使用lambda来创建匿名函数。)
    微服务状态之python巡查脚本开发
    python基础练习题(题目 计算两个矩阵相加)
  • 原文地址:https://www.cnblogs.com/liaods/p/4788917.html
Copyright © 2020-2023  润新知