美文网首页
iOS中多线程基本概念总结

iOS中多线程基本概念总结

作者: 小千 | 来源:发表于2019-05-07 12:02 被阅读0次

进程

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

线程

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

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---");

程序崩溃,死锁

相关文章

  • iOS多线程之NSThread

    前面总结了多线程基本概念和iOS多线程PThread的使用,下面接着总结iOS多线程的另外一种实现方案NSThre...

  • iOS开发多线程那些事儿

    iOS中的多线程 iOS中多线程实现的多种技术方案: 多线程的两组基本概念 串行(Serial):在固定时间内只能...

  • iOS多线程:『GCD』详尽总结

    iOS多线程:『GCD』详尽总结 iOS多线程:『GCD』详尽总结

  • iOS开发多线程篇-NSThread

    上篇我们学习了iOS多线程解决方式中的NSOperation,这篇我主要概况总结iOS多线程中NSThread的解...

  • iOS中多线程基本概念总结

    进程 正在执行中的程序,有独立的虚拟内存空间,负责程序运行中的内存分配。 线程 进程中独立的执行单元。一个进程包含...

  • iOS 多线程

    前言:这可能是史上最全面的一篇iOS 多线程博客了(王婆卖瓜一番?),从多线程的基本概念,进程的概念,引出iOS中...

  • iOS原理(八)----多线程

    iOS原理(八)----多线程 基本概念 iOS常见的多线程方案有以下四种: pthread:是一套通用的多线程C...

  • iOS多线程之 pThread

    前面总结了多线程的基本概念,今天学习总结一下多线程的其中一种实现方案pThread 一、基本概念 pThread(...

  • 线程

    iOS 多线程:『GCD』详尽总结 NSThread详解 IOS 多线程编程 『NSOperation、NSOpe...

  • iOS多线程.md

    2018-05-22 iOS多线程-概念iOS多线程:『pthread、NSThread』详尽总结 多线程-概念图...

网友评论

      本文标题:iOS中多线程基本概念总结

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