iOS中多线程基本概念总结

进程

正在执行中的程序,有独立的虚拟内存空间,负责程序运行中的内存分配。

线程

进程中独立的执行单元。一个进程包含一个或多个线程。

1、主线程

一个进程中至少包含一条线程,即主线程。
特点:

  • 如果主线程里有任务就必须等主线程任务执行完才轮到主队列的任务。
  • 如果主线程里有其他队列的任务,那么任务就不用等待,会直接被主线程执行。
    所以,主队列上添加同步任务,同一个队列里边的多个任务相互等待,会造成死锁。

队列

一种FIFO的数据结构,用于管理线程,相当于线程池,负责线程的创建和回收。
串行、并行是针对队列来说的。

1、串行队列

队列中的线程,按照顺序执行。
dispatch_queue_t q = dispatch_queue_create(“....”, dispatch_queue_serial);

2、并行队列

队列中的线程会并发执行。
dispatch_queue_t q = dispatch_queue_create("......", dispatch_queue_concurrent);

3、主队列

每一个应用程序对应唯一一个串行队列。
dispatch_queue_t q = dispatch_get_main_queue();

  • 不能开启同步,会抢占主线程的资源,阻塞主线程,造成死锁。
  • 可以开启异步任务,但不会开启新的线程,只是降低异步任务的优先级,让cpu空闲的时候才去调用。

4、全局队列

系统创建的并行队列.
dispatch_queue_t q = dispatch_get_global_queue(dispatch_queue_priority_default, 0);

同步 & 异步

同步、异步是针对是否阻塞线程来说的。

1、同步

任务优先级高,在线程中有执行顺序,不会开启新的线程。
dispatch_sync

2、异步

任务优先级低,在线程中执行没有顺序,看cpu闲不闲。
在主队列中不会开启新的线程,其他队列会开启新的线程。
dispatch_async

练习

1、并行队列+异步执行

    dispatch_queue_t queueConcurrent = dispatch_queue_create("xq.demo", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"---start---");
    dispatch_async(queueConcurrent, ^{
        NSLog(@"1---%@", [NSThread currentThread]);
    });
    dispatch_async(queueConcurrent, ^{
        NSLog(@"2---%@", [NSThread currentThread]);
    });
    dispatch_async(queueConcurrent, ^{
        NSLog(@"3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
---start---
---end---
1---<NSThread: 0x6000007ff4c0>{number = 3, name = (null)}
3---<NSThread: 0x6000007ff0c0>{number = 5, name = (null)}
2---<NSThread: 0x6000007cc900>{number = 4, name = (null)}

"---start---"和"---end---"的打印,是在主线程队列(串行队列)中执行。三个异步线程是在新建的并发队列中执行,并发队列无顺序,所以3个异步任务的打印顺序随机,异步执行创建新线程,且并行,故创建了3个线程。

2、串行队列+异步执行

    dispatch_queue_t queueSerial = dispatch_queue_create("xq.demo", DISPATCH_QUEUE_SERIAL);
    NSLog(@"---start---");
    dispatch_async(queueSerial, ^{
        NSLog(@"1---%@", [NSThread currentThread]);
    });
    dispatch_async(queueSerial, ^{
        NSLog(@"2---%@", [NSThread currentThread]);
    });
    dispatch_async(queueSerial, ^{
        NSLog(@"3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
---start---
---end---
1---<NSThread: 0x600003a18f80>{number = 3, name = (null)}
2---<NSThread: 0x600003a18f80>{number = 3, name = (null)}
3---<NSThread: 0x600003a18f80>{number = 3, name = (null)}

"---start---"和"---end---"的打印,是在主线程队列(串行队列)中执行。三个异步线程是在新建的串行队列中执行,串行队列按照FIFO顺序执行,所以3个异步任务的打印顺序为1、2、3,异步执行创建新线程,且串行,故创建了1个线程。

3、并行队列+同步执行

    dispatch_queue_t queueConcurrent = dispatch_queue_create("xq.demo", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"---start---");
    dispatch_sync(queueConcurrent, ^{
        NSLog(@"1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queueConcurrent, ^{
        NSLog(@"2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queueConcurrent, ^{
        NSLog(@"3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
---start---
1---<NSThread: 0x60000389d2c0>{number = 1, name = main}
2---<NSThread: 0x60000389d2c0>{number = 1, name = main}
3---<NSThread: 0x60000389d2c0>{number = 1, name = main}
---end---

同步执行,不会创建新线程,只能在当前线程(主线程),且任务完成才能执行下一个任务,故虽然是并行队列,但是只要一个线程,故还是按照FIFO顺序执行。

4、串行队列+同步执行

    dispatch_queue_t queueSerial = dispatch_queue_create("xq.demo", DISPATCH_QUEUE_SERIAL);
    NSLog(@"---start---");
    dispatch_sync(queueSerial, ^{
        NSLog(@"1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queueSerial, ^{
        NSLog(@"2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queueSerial, ^{
        NSLog(@"3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");
---start---
1---<NSThread: 0x600002b42880>{number = 1, name = main}
2---<NSThread: 0x600002b42880>{number = 1, name = main}
3---<NSThread: 0x600002b42880>{number = 1, name = main}
--end---

串行队列,按FIFO顺序执行,同步执行,不会创建新线程,在当前线程(主线程)按顺序执行。

5、主队列+同步执行

    dispatch_queue_t queueMain = dispatch_get_main_queue();
    NSLog(@"---start---");
    dispatch_sync(queueMain, ^{
        NSLog(@"1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queueMain, ^{
        NSLog(@"2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queueMain, ^{
        NSLog(@"3---%@", [NSThread currentThread]);
    });
    NSLog(@"---end---");

程序崩溃,死锁

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容