22期_iOS_多线程基本使用

2023-08-23  本文已影响0人  萧修

GCD的队列

进程和线程,异步和同步

dispatch_queue_create("com.testqueue.serial", DISPATCH_QUEUE_SERIAL);
dispatch_queue_create("com.wynter.councurrent", DISPATCH_QUEUE_CONCURRENT)
dispatch_get_main_queue();
//可以设置优先级
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

串行并发同步异步的组合

同步和异步的主要影响:是否开启新的线程
并发和串行的主要影响:任务的执行方式

  1. 串行队列执行异步任务,开辟新的线程,多个异步任务顺序执行
//串行队列异步
- (void)testDemoSA {
    //创建一个串行队列
    dispatch_queue_t queue = dispatch_queue_create("ggx.tongbu", DISPATCH_QUEUE_SERIAL);
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");//1 5 2 4 3
}
  1. 串行队列执行同步任务,当前线程执行,顺序执行
//串行同步,包括死锁
- (void)testDemoSS {
    //创建一个串行队列
    dispatch_queue_t queue = dispatch_queue_create("ggx.tongbu", DISPATCH_QUEUE_SERIAL);
    //异步函数
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        //死锁
        dispatch_sync(queue, ^{
            NSLog(@"3");
        });
    });
    NSLog(@"5");//1 5 2 【同步不开辟线程 向queue串行队列插入任务】死锁
}

//串行同步22
- (void)testDemoSS2 {
    //创建一个串行队列
    dispatch_queue_t queue = dispatch_queue_create("ggx.tongbu", DISPATCH_QUEUE_SERIAL);
    //异步函数
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");//1 5 死锁
}
  1. 并发队列执行异步任务,开辟新的线程,任务异步执行
//并发异步
- (void)testDemoCA {
    //创建一个并行队列
    dispatch_queue_t queue = dispatch_queue_create("ggx.bingxing", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");//15243
}
  1. 并发队列执行同步任务,不开辟线程,任务顺序执行
//并发同步
- (void)testDemoCS {
    //创建一个并行队列
    dispatch_queue_t queue = dispatch_queue_create("ggx.bingxing", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"1");
    dispatch_sync(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");//12435
}
  1. 全局队列同步任务:不会开辟新的线程,任务顺序执行
//全局队列同步
- (void)testDemoGS {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSLog(@"1");
    dispatch_sync(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");//12435
}
  1. 全局队列异步任务,开辟新的线程,操作无序执行,队列前如果有其他任务,会等待其执行完毕
//全局异步
- (void)testDemoGA {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");//15243
}

主队列同步:主队列添加的同步任务,永远不会执行,会造成死锁

//主队列同步
- (void)testDemoMS {
    dispatch_queue_t queue = dispatch_get_main_queue();
    NSLog(@"1");
    dispatch_sync(queue, ^{
        //1 【同步任务 向主队列插入任务 ,主队列任务等待块的执行 ,里面块的任务优先完成,但是这个任务插入到块之后(串行队列)】
        NSLog(@"2");
//        dispatch_async(queue, ^{
//            NSLog(@"3");
//        });
//        NSLog(@"4");
    });
    NSLog(@"5");//15243
}

主队列(串行)异步:都在主线程执行,不存在异步概念

//主队列异步
- (void)testDemoMA {
    dispatch_queue_t queue = dispatch_get_main_queue();
    NSLog(@"1");
    NSLog(@"11_%@",[NSThread currentThread]);//<_NSMainThread: 0x6000020d0440>{number = 1, name = main}
    dispatch_async(queue, ^{
        NSLog(@"2");
        NSLog(@"22_%@",[NSThread currentThread]);//22_<_NSMainThread: 0x6000020d0440>{number = 1, name = main}
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"55_%@",[NSThread currentThread]);
    NSLog(@"5");//12435
}

一道面试题,AC都可以

//函数执行顺序
- (void)wbinterDemo{
    dispatch_queue_t queue = dispatch_queue_create("com.lg.cooci.cn", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
        NSLog(@"1");
    });
    
    dispatch_async(queue, ^{
        NSLog(@"2");
    });

    dispatch_sync(queue, ^{ NSLog(@"3"); });
    
    NSLog(@"0");

    dispatch_async(queue, ^{
        NSLog(@"7");
    });
    dispatch_async(queue, ^{
        NSLog(@"8");
    });
    dispatch_async(queue, ^{
        NSLog(@"9");
    });
    // A: 1230789
    // B: 1237890
    // C: 3120798
    // D: 2137890
}
- (void)mapGCD {
    // 3.快速遍历
    // 开多个线程进行遍历
    /*
       第一个参数:遍历的次数
       第二个参数:队列(并发队列)
       第三个参数:index 索引
    */
    dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t iteration) {
        NSLog(@"%zd---%@",index,[NSThread currentThread]);
    });
}
上一篇下一篇

猜你喜欢

热点阅读