• ios中多线程GCD NSOperation NSThread 相关的操作解析


        //1、GCD 继承自C语言 优点 简单方便

        //开启一个子线程处理耗时的操作

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            

        //在主线程处理UI更新相关的操作

        dispatch_async(dispatch_get_main_queue(), ^{

            

            

        });

        });

        

        //延时操作

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

            

            

        });

        //确保程序只会运行一次,类似于锁

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            

        });

        //确保程序只会运行一次

        @synchronized (self) {

            

        }

            

        // 2、NSOperation NSOperationQueue  是对GCD更高一层的封装 可以添加依赖和设置优先级更好的控制和管理线程的并发操作 缺点:操作比GCD稍慢

        NSBlockOperation *block=[NSBlockOperation blockOperationWithBlock:^{

            

        }];

        

        NSInvocationOperation *invo=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnOperate) object:nil];

        

        NSOperationQueue *queue=[[NSOperationQueue alloc]init];

        //添加依赖,设置操作

        [block addDependency:invo];

        //设置操作优先级

        block.queuePriority=NSOperationQueuePriorityLow;

        invo.queuePriority=NSOperationQueuePriorityHigh;

        [queue addOperation:block];

        [queue addOperation:invo];

        

        //3、NSThread 是一种轻量级的 需要管理线程的生命周期和并发操作

        NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(addBtnTypeCss:) object:nil];

        //线程的开启

        [thread start];

        //线程的取消

        [thread cancel];

        //等到某个日期

        [NSThread sleepUntilDate:[NSDate date]];

        //设置等待多久唤起

        [NSThread sleepForTimeInterval:1];

  • 相关阅读:
    MapReduce Demo
    Hadoop Api 基本操作
    Dapper with MVC MiniProfiler
    使用Azure Blob存储
    Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.
    Netbeans导入Nutch1.2
    Error 2103 “Unhandled Error in Silverlight Application“ 解决办法
    读取nutch爬取内容方法
    转 nutch网页快照乱码解决方法
    fast neural style transfer图像风格迁移基于tensorflow实现
  • 原文地址:https://www.cnblogs.com/bigant9527/p/14046801.html
Copyright © 2020-2023  润新知