zoukankan      html  css  js  c++  java
  • iOS学习笔记5GCD smallelephant_A

    ---恢复内容开始---

    直接上代码

    一,异步函数往并发队列中添加任务

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        dispatch_async(queue, ^{

            NSLog(@"下载图片。。。%@",[NSThread currentThread]);

        });

        dispatch_async(queue, ^{

            NSLog(@"下载图片2.。。%@",[NSThread currentThread]);

        });

        dispatch_async(queue, ^{

            NSLog(@"下载图片3.。。%@",[NSThread currentThread]);

        });

        

        

        NSLog(@"主线程。。。%@",[NSThread mainThread]);

    二,异步函数往串行队列中添加任务

     NSLog(@"%@",[NSThread mainThread]);

        

        dispatch_queue_t queue = dispatch_queue_create("dusunan", NULL);

        dispatch_async(queue, ^{

            NSLog(@"下载图片1----%@",[NSThread currentThread]);

        });

        dispatch_async(queue, ^{

            NSLog(@"下载图片2----%@",[NSThread currentThread]);

        });

        

        dispatch_async(queue, ^{

            NSLog(@"下载图片3----%@",[NSThread currentThread]);

        });

     此种方法会开启线程,但是只会开启一个线程

    三,用异步函数往并发队列中添加任务

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        

        

        

        dispatch_sync(queue, ^{

            NSLog(@"下载图片1 %@",[NSThread currentThread]);

        });

        dispatch_sync(queue, ^{

            NSLog(@"下载图片2 %@",[NSThread currentThread]);

        });

        dispatch_sync(queue, ^{

            NSLog(@"下载图片3 %@",[NSThread currentThread]);

        });

     四,同步函数往串行队列中添加任务

     dispatch_queue_t queue = dispatch_queue_create("dusunan", NULL);

        

        dispatch_sync(queue, ^{

            NSLog(@"下载图片1 %@",[NSThread currentThread]);

        });

        dispatch_sync(queue, ^{

            NSLog(@"下载图片2 %@",[NSThread currentThread]);

        });

        dispatch_sync(queue, ^{

            NSLog(@"下载图片3 %@",[NSThread currentThread]);

        });

        

     五,应用

    (1)一次性代码 

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    18 {
    19     if (_log==NO) {
    20         NSLog(@"该行代码只执行一次");
    21         _log=YES;
    22     }
    23 }



    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    26 {
    27     static dispatch_once_t onceToken;
    28     dispatch_once(&onceToken, ^{
    29         NSLog(@"该行代码只执行一次");
    30     });
    31 }


    下面的代码可以取代上面的代码块
  • 相关阅读:
    通过Web启动本地应用程序
    类似百度文库文档预览方式实现
    cas4.0 session中返回更多的用户信息
    word转pdf图片问题
    JSON数据转换成table表格
    2017年各银行卡跨行取款收费标准
    解决win8/8.1系统安装.net framework 3.5出现0x800F0906代码错误
    更改Thunderbird的默认语言
    java获取客户端ip地址
    为jquery ajax请求增加正在运行提示
  • 原文地址:https://www.cnblogs.com/adodo/p/5193421.html
Copyright © 2011-2022 走看看