美文网首页
多线程相关《队列与任务》简单了解1

多线程相关《队列与任务》简单了解1

作者: 凯司机 | 来源:发表于2022-08-19 22:55 被阅读0次

    1.关于多线程的基础简介:

    多线程基础简介.png

    注意点:从手动管理到自动管理,使用更多偏向于后两者,充分利用设备的多核。

    2.队列和任务:

    串行对列:按照顺序从左到右,从上到下依次执行。—— 先进先出

    并行对列:看似同时一起执行,实际执行有时间先后,但执行时间有交叉。—— 时间上有交叉

    什么时候会开启一条线程?

    1. 异步一定会开辟新线程嘛?不一定,同步串行一条对列。
    2. 同步一定不会开启新线程?不会

    1.同步串行

    /// 同步串行
    - (void)syncSerial {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_SERIAL);
        dispatch_sync(queue, ^{
            NSLog(@"_任务1_");
        });
        dispatch_sync(queue, ^{
            NSLog(@"_任务2_");
        });
        dispatch_sync(queue, ^{
            NSLog(@"_任务3_");
        });
        NSLog(@"_结束_");
    /*
    执行结果:
    2022-08-19 22:16:55.437781+0800 YDBasicPublicCourse[30677:1915128] _开始_
    2022-08-19 22:16:55.438867+0800 YDBasicPublicCourse[30677:1915128] _任务1_
    2022-08-19 22:16:55.440569+0800 YDBasicPublicCourse[30677:1915128] _任务2_
    2022-08-19 22:16:55.441949+0800 YDBasicPublicCourse[30677:1915128] _任务3_
    2022-08-19 22:16:55.442340+0800 YDBasicPublicCourse[30677:1915128] _结束_
    */
    }
    

    2.异步串行

    /// 异步串行——串行队列中多个异步,只会开辟一条新线程
    - (void)asyncSerial {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_SERIAL);
        dispatch_async(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"_任务2_%@",[NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    执行结果1:
    2022-08-19 22:34:51.896769+0800 YDBasicPublicCourse[30872:1929491] _开始_
    2022-08-19 22:34:51.897045+0800 YDBasicPublicCourse[30872:1929491] _结束_
    2022-08-19 22:34:51.897146+0800 YDBasicPublicCourse[30872:1929575] _任务1_<NSThread: 0x600001528780>{number = 4, name = (null)}
    2022-08-19 22:34:51.897678+0800 YDBasicPublicCourse[30872:1929575] _任务2_<NSThread: 0x600001528780>{number = 4, name = (null)}
    2022-08-19 22:34:51.897902+0800 YDBasicPublicCourse[30872:1929575] _任务3_<NSThread: 0x600001528780>{number = 4, name = (null)}
    
    
    执行结果2:  可能会存在的一种情况。
    2022-08-19 22:36:26.174839+0800 YDBasicPublicCourse[30901:1931698] _开始_
    2022-08-19 22:36:26.175382+0800 YDBasicPublicCourse[30901:1931850] _任务1_<NSThread: 0x600003168740>{number = 5, name = (null)}
    2022-08-19 22:36:26.175599+0800 YDBasicPublicCourse[30901:1931850] _任务2_<NSThread: 0x600003168740>{number = 5, name = (null)}
    2022-08-19 22:36:26.175773+0800 YDBasicPublicCourse[30901:1931850] _任务3_<NSThread: 0x600003168740>{number = 5, name = (null)}
    2022-08-19 22:36:26.175283+0800 YDBasicPublicCourse[30901:1931698] _结束_
    */
    
    /// 异步并发——线程复用、高效率执行、充分利用内核——开辟线程数量有限
    /// 并发对列中的异步不一定会开辟线程  内核数量3-6条
    - (void)asyncConcurrent {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"_任务2_%@",[NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    执行结果:
    2022-08-19 22:44:54.121690+0800 YDBasicPublicCourse[31035:1939697] _开始_
    2022-08-19 22:44:54.121923+0800 YDBasicPublicCourse[31035:1939697] _结束_
    2022-08-19 22:44:54.122020+0800 YDBasicPublicCourse[31035:1939767] _任务1_<NSThread: 0x600003733ec0>{number = 5, name = (null)}
    2022-08-19 22:44:54.122021+0800 YDBasicPublicCourse[31035:1939762] _任务2_<NSThread: 0x600003761780>{number = 6, name = (null)}
    2022-08-19 22:44:54.122091+0800 YDBasicPublicCourse[31035:1939763] _任务3_<NSThread: 0x600003761b00>{number = 4, name = (null)}
    */
    
    /// 同步并发——阻塞当前线程(主线程)、不会开辟新线程。
    - (void)syncConcurrent {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_CONCURRENT);
        dispatch_sync(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
        });
        dispatch_sync(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    执行结果:
    2022-08-19 22:49:43.615796+0800 YDBasicPublicCourse[31099:1944106] _开始_
    2022-08-19 22:49:43.616077+0800 YDBasicPublicCourse[31099:1944106] _任务1_<_NSMainThread: 0x600002040440>{number = 1, name = main}
    2022-08-19 22:49:43.616256+0800 YDBasicPublicCourse[31099:1944106] _任务1_<_NSMainThread: 0x600002040440>{number = 1, name = main}
    2022-08-19 22:49:43.616450+0800 YDBasicPublicCourse[31099:1944106] _任务1_<_NSMainThread: 0x600002040440>{number = 1, name = main}
    2022-08-19 22:49:43.616598+0800 YDBasicPublicCourse[31099:1944106] _结束_
    */
    
    /// 同步并发嵌套——不睡觉
    - (void)syncConcurrentNesting {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_CONCURRENT);
        dispatch_sync(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
            dispatch_async(queue, ^{
                NSLog(@"_任务2_%@",[NSThread currentThread]);
            });
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    2022-08-19 23:11:01.372239+0800 YDBasicPublicCourse[31301:1958263] _开始_
    2022-08-19 23:11:01.372534+0800 YDBasicPublicCourse[31301:1958263] _任务1_<_NSMainThread: 0x600002eb4000>{number = 1, name = main}
    2022-08-19 23:11:01.372760+0800 YDBasicPublicCourse[31301:1958263] _任务3_<_NSMainThread: 0x600002eb4000>{number = 1, name = main}
    2022-08-19 23:11:01.372784+0800 YDBasicPublicCourse[31301:1958342] _任务2_<NSThread: 0x600002efd740>{number = 4, name = (null)}
    2022-08-19 23:11:01.372912+0800 YDBasicPublicCourse[31301:1958263] _结束_
    */
    
    /// 同步并发嵌套——睡二觉
    - (void)syncConcurrentNesting {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_CONCURRENT);
        dispatch_sync(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
            dispatch_async(queue, ^{
                NSLog(@"_任务2_%@",[NSThread currentThread]);
            });
            sleep(2);
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    2022-08-19 23:20:08.872773+0800 YDBasicPublicCourse[31389:1964271] _开始_
    2022-08-19 23:20:08.873963+0800 YDBasicPublicCourse[31389:1964271] _任务1_<_NSMainThread: 0x6000030c4140>{number = 1, name = main}
    2022-08-19 23:20:08.874272+0800 YDBasicPublicCourse[31389:1964374] _任务2_<NSThread: 0x60000308a700>{number = 7, name = (null)}
    2022-08-19 23:20:10.874488+0800 YDBasicPublicCourse[31389:1964271] _任务3_<_NSMainThread: 0x6000030c4140>{number = 1, name = main}
    2022-08-19 23:20:10.874714+0800 YDBasicPublicCourse[31389:1964271] _结束_
    */
    
    /// 同步并发嵌套——睡一觉、取决于内部的耗时时长。
    - (void)syncConcurrentNesting {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_CONCURRENT);
        dispatch_sync(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
            dispatch_async(queue, ^{
                sleep(2);
                NSLog(@"_任务2_%@",[NSThread currentThread]);
            });
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    2022-08-19 23:27:38.195428+0800 YDBasicPublicCourse[31445:1968352] _开始_
    2022-08-19 23:27:38.195734+0800 YDBasicPublicCourse[31445:1968352] _任务1_<_NSMainThread: 0x6000011ac900>{number = 1, name = main}
    2022-08-19 23:27:38.195921+0800 YDBasicPublicCourse[31445:1968352] _任务3_<_NSMainThread: 0x6000011ac900>{number = 1, name = main}
    2022-08-19 23:27:38.196106+0800 YDBasicPublicCourse[31445:1968352] _结束_
    2022-08-19 23:27:40.196172+0800 YDBasicPublicCourse[31445:1968492] _任务2_<NSThread: 0x6000011e0fc0>{number = 7, name = (null)}
    */
    
    /// 同步并发>并发嵌套
    - (void)syncConcurrentNesting {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_CONCURRENT);
        dispatch_sync(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
            dispatch_sync(queue, ^{
                NSLog(@"_任务2_%@",[NSThread currentThread]);
            });
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    2022-08-19 23:36:15.762194+0800 YDBasicPublicCourse[31519:1973664] _开始_
    2022-08-19 23:36:15.763770+0800 YDBasicPublicCourse[31519:1973664] _任务1_<_NSMainThread: 0x600001930000>{number = 1, name = main}
    2022-08-19 23:36:15.769405+0800 YDBasicPublicCourse[31519:1973664] _任务2_<_NSMainThread: 0x600001930000>{number = 1, name = main}
    2022-08-19 23:36:15.773415+0800 YDBasicPublicCourse[31519:1973664] _任务3_<_NSMainThread: 0x600001930000>{number = 1, name = main}
    2022-08-19 23:36:15.776105+0800 YDBasicPublicCourse[31519:1973664] _结束_
    */
    
    /// 异步串行嵌套
    - (void)syncSerialAsync {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_SERIAL);
        dispatch_async(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
            dispatch_async(queue, ^{
                NSLog(@"_任务2_%@",[NSThread currentThread]);
            });
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    执行结果:
    2022-08-19 23:42:36.232114+0800 YDBasicPublicCourse[31569:1977932] _开始_
    2022-08-19 23:42:42.842165+0800 YDBasicPublicCourse[31569:1977932] _结束_
    2022-08-19 23:42:42.842246+0800 YDBasicPublicCourse[31569:1978108] _任务1_<NSThread: 0x60000088d480>{number = 7, name = (null)}
    2022-08-19 23:42:42.842628+0800 YDBasicPublicCourse[31569:1978108] _任务3_<NSThread: 0x60000088d480>{number = 7, name = (null)}
    2022-08-19 23:42:42.842821+0800 YDBasicPublicCourse[31569:1978108] _任务2_<NSThread: 0x60000088d480>{number = 7, name = (null)}
    */
    
    /// 异步串行嵌套——睡一觉
    - (void)syncSerialAsync {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_SERIAL);
        dispatch_async(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
            dispatch_async(queue, ^{
                sleep(2);
                NSLog(@"_任务2_%@",[NSThread currentThread]);
            });
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    执行结果:
    2022-08-19 23:45:51.657954+0800 YDBasicPublicCourse[31604:1980828] _开始_
    2022-08-19 23:45:51.658231+0800 YDBasicPublicCourse[31604:1980828] _结束_
    2022-08-19 23:45:51.658391+0800 YDBasicPublicCourse[31604:1981007] _任务1_<NSThread: 0x6000038c5f40>{number = 6, name = (null)}
    2022-08-19 23:45:51.658562+0800 YDBasicPublicCourse[31604:1981007] _任务3_<NSThread: 0x6000038c5f40>{number = 6, name = (null)}
    2022-08-19 23:45:53.663426+0800 YDBasicPublicCourse[31604:1981007] _任务2_<NSThread: 0x6000038c5f40>{number = 6, name = (null)}
    
    执行结果2:
    2022-08-19 23:50:56.097268+0800 YDBasicPublicCourse[31653:1984727] _开始_
    2022-08-19 23:50:56.097575+0800 YDBasicPublicCourse[31653:1984898] _任务1_<NSThread: 0x6000037de640>{number = 6, name = (null)}
    2022-08-19 23:50:56.097976+0800 YDBasicPublicCourse[31653:1984898] _任务3_<NSThread: 0x6000037de640>{number = 6, name = (null)}
    2022-08-19 23:50:56.097493+0800 YDBasicPublicCourse[31653:1984727] _结束_
    2022-08-19 23:50:58.102417+0800 YDBasicPublicCourse[31653:1984898] _任务2_<NSThread: 0x6000037de640>{number = 6, name = (null)}
    */
    
    /// 异步串行嵌套——睡一觉
    - (void)syncSerialAsync {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_SERIAL);
        dispatch_async(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
            dispatch_async(queue, ^{
                NSLog(@"_任务2_%@",[NSThread currentThread]);
            });
            sleep(2);
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    2022-08-19 23:56:10.330153+0800 YDBasicPublicCourse[31695:1988183] _开始_
    2022-08-19 23:56:10.330474+0800 YDBasicPublicCourse[31695:1988304] _任务1_<NSThread: 0x6000028a8040>{number = 7, name = (null)}
    2022-08-19 23:56:10.330473+0800 YDBasicPublicCourse[31695:1988183] _结束_
    2022-08-19 23:56:12.334013+0800 YDBasicPublicCourse[31695:1988304] _任务3_<NSThread: 0x6000028a8040>{number = 7, name = (null)}
    2022-08-19 23:56:12.334344+0800 YDBasicPublicCourse[31695:1988304] _任务2_<NSThread: 0x6000028a8040>{number = 7, name = (null)}
    */
    

    主对列是串行队列、全局队列是并发队列

    /// 多线程死锁——2个任务相互等待造成的、嵌套死锁
    - (void)multithreads {
        NSLog(@"_开始_");
        dispatch_queue_t queue = dispatch_queue_create("yd.com.gcd", DISPATCH_QUEUE_SERIAL);
        dispatch_async(queue, ^{
            NSLog(@"_任务1_%@",[NSThread currentThread]);
            dispatch_sync(queue, ^{
                NSLog(@"_任务2_%@",[NSThread currentThread]);
            });
            NSLog(@"_任务3_%@",[NSThread currentThread]);
        });
        NSLog(@"_结束_");
    }
    /*
    死锁:
    */
    
    代码块
    
    代码块
    
    代码块
    

    相关文章

      网友评论

          本文标题:多线程相关《队列与任务》简单了解1

          本文链接:https://www.haomeiwen.com/subject/zltpgrtx.html