• 进击的UI---------------多线程管理


    多线程管理 (NSThread,NSOperationQueue,GCD)
    程序:有源代码生成的可执行应用(qq.app)
    进程:一个正常运行的程序(正在运行的qq)
    线程:程序中独立运行的代码段(接受qq消息的代码)
    (一个进程是由一个或多个线程组成.进程负责资源的调度和分配,线程才是程序真正的执行单元,负责代码的执行)
    单线程(只有唯一的主线,代码顺序执行,易页面假死)和多线程(根据需求开发多个子线程,独立运行,不易页面假死)
    iOS中的UI的添加和刷新必须在主线程中操作;
    // 多线程 (进程,线程)
    // 进程 : 正在运行的一个程序
    // 线程 : 可以运行的一段代码(单线程和多线程)
    // 一个进程里面可以有一个或多个线程,所有的程序都可以写成单线程,但是单线程程序如果里面有大数据的计算,n多图片的加载,会出现卡顿的情况(音频视频的下载).
    // 1 NSThread 2 NSOperationQueue 3 GCD(4.0)
    // ios 启动的线程 是一个串行的队列
    1.NSThread
           // 1
        /*
        // 开辟了一个新的线程(轻量级解决多线程问题的一个类需要手动的管理)
        NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(threadAction:) object:@"BOOM"];
        // object这个参数就是传向Action后面的sender这个参数
        // 手动开启
        [thread1 start];
        [thread1 cancel];*/
           // 2
        /*
        // 开辟新的多线程并自动启动
        [NSThread detachNewThreadSelector:@selector(threadAction:) toTarget:self withObject:@"BOOM"];*/
    //    NSObject *obj = @"BOOM";
    //    [obj performSelectorInBackground:@selector(threadAction:) withObject:self];
        // NSObect 这个基类有一个方法可以实现多线程
    //    [self performSelectorInBackground:@selector(threadAction:) withObject:nil];
    2.NSOperationQueue
     // NSOperation(抽象的数据类1.NSInvocationOperation 2.NSBlockOpertion) NSOperationQueue
    //    NSInvocationOperation *invo1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(invo1Action:) object:nil];
    //    NSInvocationOperation *invo2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(invo2Action:) object:nil];
    //    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
    //        NSLog(@"第三步:把冰箱门关上");
    //    }];
    //    // 初始化队列
    //    NSOperationQueue *myQueue = [[NSOperationQueue alloc]init];
    //    // 最多同时运行的线程数量
    //    myQueue.maxConcurrentOperationCount = 2;
    //    // 添加依赖关系
    //    [block addDependency:invo2];
    //    [invo2 addDependency:invo1];
    //   
    //    [myQueue addOperation:invo1];
    //    [myQueue addOperation:invo2];
    //    [myQueue addOperation:block]; 
    //- (void)invo1Action:(id)sender{
    //    NSLog(@"第一步:把冰箱门打开");
    //}
    //- (void)invo2Action:(id)sender{
    //    NSLog(@"第二步:把大象装进去");
    //}
    3.GCD(Grand Central Dispatch)----Block或函数
    // dispatch(C语言函数)GCD----原则:先进先出(管理多线程)FIFO;
    // 队列
    // 1主队列(一个程序只有一个主队列) (串行队列)
    // 2全局队列 系统默认提供的 (并行队列,先后顺序不一定)
    // 3自定义队列 程序猿自己定义 (并行队列或串行并列)
    ************************************************************************
    // 1主队列(一个程序只有一个主队列) (串行队列)
     //   dispatch_queue_t main = dispatch_get_main_queue();
    //    dispatch_sync(main, ^{
    //        NSLog(@"123");
    //    });// 切记:使用GCD时不能添加同步队列
        // 按顺序执行 没有开辟新线程
        dispatch_async(main, ^{
            NSLog(@"当前线程1 %@",[NSThread currentThread]);
        });
        dispatch_async(main, ^{
            NSLog(@"当前线程2 %@",[NSThread currentThread]);
        });
        dispatch_async(main, ^{
            NSLog(@"当前线程3 %@",[NSThread currentThread]);
        });
    // 2全局队列 系统默认提供的 (并行队列,先后顺序不一定)
        dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        // 添加异步队列的时候开辟了新线程 执行的顺序不一定
        dispatch_async(global, ^{
            NSLog(@"1%@",[NSThread currentThread]);
        });
        dispatch_async(global, ^{
            NSLog(@"2%@",[NSThread currentThread]);
        });
        dispatch_async(global, ^{
            NSLog(@"3%@",[NSThread currentThread]);
        });
        // 添加同步的任务
    //    dispatch_sync(global, ^{
    //        NSLog(@"4%@",[NSThread currentThread]);
    //    });
    // 3自定义队列 程序猿自己定义 (并行队列或串行并列)
    1⃣️:/*
        // 定义串行 第一个参数给一个名字 第二个参数表示是串行
        // 开辟了一个新队列 按顺序执行
        dispatch_queue_t MyQueue = dispatch_queue_create(@"MyQueue".UTF8String, DISPATCH_QUEUE_SERIAL);
        dispatch_async(MyQueue, ^{
            NSLog(@"1%@",[NSThread currentThread]);
        });
        dispatch_async(MyQueue, ^{
            NSLog(@"2%@",[NSThread currentThread]);
        });
        dispatch_async(MyQueue, ^{
            NSLog(@"3%@",[NSThread currentThread]);
        });*/
    2⃣️: /*
        //定义并行 开辟多条新队列 执行顺序不一定
        dispatch_queue_t MyNewQueue = dispatch_queue_create(@"MyNewQueue".UTF8String, DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(MyNewQueue, ^{
            NSLog(@"1%@",[NSThread currentThread]);
        });
        dispatch_async(MyNewQueue, ^{
            NSLog(@"2%@",[NSThread currentThread]);
        });
        dispatch_async(MyNewQueue, ^{
            NSLog(@"3%@",[NSThread currentThread]);
        });
        dispatch_async(MyNewQueue, ^{
            NSLog(@"4%@",[NSThread currentThread]);
        });*/
    额外知识点:
    /*
        //延迟执行
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"我操");
        });
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             NSLog(@"我忍你好久了");
        });
        //重复执行
        dispatch_apply(10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t t) {
            NSLog(@"我要打%zu个",t);
        });*/
    //单例类
    .m
    #import "Person.h"
    static Person *p = nil;
    @implementation Person
    + (instancetype)shareHandle{
        static dispatch_once_t once_Token;
        dispatch_once(&once_Token, ^{
            //咱们以前写的单例类是不完整的,完整的需要GCD处理
            p = [[Person alloc]init];
        });
        return p;
    }
    @end
     
  • 相关阅读:
    正则表达式
    Newtonsoft.Json
    MVC之参数验证(三)
    MVC之参数验证(二)
    MVC之参数验证(一)
    MVC之模型绑定
    导致存储过程重新编译的原因
    IFormattable,ICustomFormatter, IFormatProvider接口
    oracle将id串转换为名字串
    oracle查看表空间大小及使用情况
  • 原文地址:https://www.cnblogs.com/sharkHZ/p/4984048.html
Copyright © 2020-2023  润新知