多线程 GCD同步异步相关 GCD队列组

0.多线程就是指从软件或者硬件上实现多个线程并发执行的技术。

1.OC里实现多线程的方案 :pthread,NSThread ,GCD,NSOperation

2.GCD 同步异步: dipath_async  dispath_sync

3.GCD 串行并行:

 Concurrent 可以让多个任务并发执行(同事执行,自动开启多个线程同事执行任务),并发只在异步函数下有效果

 Serial 任务一个接一个地执行(一个任务执行完后,在执行下一个任务)

4.同步异步:主要印象能不能开启线程

同步:只在当前线程中只修任务,不具备开启线程能力

异步:在新的线程中只修任务,具备开启线程能力

5.窜行并行影响任务执行任务

并行:任务并发执行

串行:一个任务执行完后,再执行下一个任务

  并发队列 串休队列 主队列
同步sync 没有开启新线程,串行执行任务 没有开启新线程,串行执行任务 没有开启新线程,串行执行任务
异步async 有开启新线程,并发执行任务 有开启新线程,串行执行任务 没有开启新线程,串行执行任务

只要是同步执行或者主队了都是不开启线程的,只有异步不是主队列才开启线程。

6.产生死锁: 队列性进行出FIFO特点(只有窜行队列才存在) 同步又是马上执行

    // 问题:以下代码是在主线程执行的,会不会产生死锁?会!

    NSLog(@"执行任务1");    

    dispatch_queue_t queue = dispatch_get_main_queue();

    dispatch_sync(queue, ^{

        NSLog(@"执行任务2");

    });

    NSLog(@"执行任务3");

    // dispatch_sync立马在当前线程同步执行任务

  总结:使用sync且在当前串行队列中执行才会产生死锁

 

- (void)test2

{

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    

    dispatch_async(queue, ^{

        NSLog(@"1");

        // 这句代码的本质是往Runloop中添加定时器

        [self performSelector:@selector(test) withObject:nil afterDelay:.0];

        NSLog(@"3");

        

        //      [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];

        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

    });

}

- (void)test{

    NSLog(@"2");

}

GNUSTep:http://www.gnustep.org/resources/downloads.php

- (void)test

{

    NSLog(@"2");

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NSThread *thread = [[NSThread alloc] initWithBlock:^{

        NSLog(@"1");

        

        [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];

        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

    }];

    [thread start];

    

    [self performSelector:@selector(test) onThread:thread withObject:nil waitUntilDone:YES];

}

7.队列组

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    // 创建队列组

    dispatch_group_t group = dispatch_group_create();

    // 创建并发队列

    dispatch_queue_t queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_CONCURRENT);

    

    // 添加异步任务

    dispatch_group_async(group, queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"任务1-%@", [NSThread currentThread]);

        }

    });

    

    dispatch_group_async(group, queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"任务2-%@", [NSThread currentThread]);

        }

    });

    

    // 等前面的任务执行完毕后,会自动执行这个任务

//    dispatch_group_notify(group, queue, ^{

//        dispatch_async(dispatch_get_main_queue(), ^{

//            for (int i = 0; i < 5; i++) {

//                NSLog(@"任务3-%@", [NSThread currentThread]);

//            }

//        });

//    });

    

//    dispatch_group_notify(group, dispatch_get_main_queue(), ^{

//        for (int i = 0; i < 5; i++) {

//            NSLog(@"任务3-%@", [NSThread currentThread]);

//        }

//    });

    

    dispatch_group_notify(group, queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"任务3-%@", [NSThread currentThread]);

        }

    });

    

    dispatch_group_notify(group, queue, ^{

        for (int i = 0; i < 5; i++) {

            NSLog(@"任务4-%@", [NSThread currentThread]);

        }

    });

}

 

posted @ 2021-05-30 10:29  syh-918  阅读(102)  评论(0)    收藏  举报