zoukankan      html  css  js  c++  java
  • 多线程的简单使用

     NSOperationQueue的简单使用过程:

    1.  建立一个NSOperationQueue的对象

    2.  建立一个NSOperation的对象

    3.  将operation加入到NSOperationQueue中

         MRC 需要手动释放

    4.  release掉operation

     NSOperationQueue *queue = [NSOperationQueue new];

      NSInvocationOperation *operation = [[NSInvocationOperation alloc]

                                            initWithTarget:self

                                            selector:nil)

                                            object:nil];

    [queue addOperation:operation];

    /***********************************GCD**************************************************/

    //一次性执行

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            //code be executed once

        });

    //延迟调用

        double delayInSeconds = 2.0;

        dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);

        dispatch_after(timer, dispatch_get_main_queue(), ^{

            //code be executed once after delay

        });

        

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

         //code be executed once after delay

        });

    //自定义queque

        dispatch_queue_t fun = dispatch_queue_create("funfun", NULL);

        dispatch_async(fun, ^{

            // your code

        });

    MRC 需要释放

    //dispatch_release(fun);

    //GCD组 同时进行多个任务,任务完成之后再主线程执行其他任务

        dispatch_group_t group = dispatch_group_create();

        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{

            //your code

        });

        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{

            //your code

        });

           dispatch_group_notify(group, dispatch_get_main_queue(), ^{

            //your code

        });

    //多线程的线程安全

      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     dispatch_async(queue, ^{
      // 线程锁
       NSLock *lock =  [[NSLock alloc] init];

        [lock lock];

        //your code

        [lock unlock];

        });
     
     dispatch_async(queue, ^{
     NSLock *lock =  [[NSLock alloc] init];

        [lock lock];

        //your code

        [lock unlock];

    });

  • 相关阅读:
    解决SharePoint 文档库itemadded eventhandler导致的上传完成后,编辑页面保持报错的问题,错误信息为“该文档已经被编辑过 the file has been modified by...”
    解决SharePoint 2013 designer workflow 在发布的报错“负载平衡没有设置”The workflow files were saved but cannot be run.
    随机实例,随机值
    Spring4笔记
    struts2笔记(3)
    struts2笔记(2)
    获取文本的编码类型(from logparse)
    FileUtil(from logparser)
    DateUtil(SimpleDateFormat)
    struts2笔记
  • 原文地址:https://www.cnblogs.com/lhx2015/p/4630735.html
Copyright © 2011-2022 走看看