美文网首页
iOS - 多线程(二):pthread、NSThread

iOS - 多线程(二):pthread、NSThread

作者: 师大小海腾 | 来源:发表于2020-02-14 02:39 被阅读0次

iOS - 多线程 系列文章

iOS - 多线程(一):初识
iOS - 多线程(二):pthread、NSThread
iOS - 多线程(三):GCD
iOS - 多线程(四):NSOperation
iOS - 多线程(五):线程同步方案

网络配图.jpg

目录

  • 1. pthread
     1.1 pthread 介绍
     1.2 pthread 的简单使用
  • 2. NSThread
     2.1 NSThread 介绍
     2.2 NSThread 的基本使用
     2.3 线程的状态、生命周期
     2.4 线程池的原理
     2.5 线程的属性
  • 相关链接

1. pthread

1.1 pthread 介绍

  • 全称 POSIX Thread,POSIX(Portable Operating System Interface)表示可移植操作系统接口;
  • 一套用 C 语言写的通用的多线程 API;
  • 适用于 Unix / Linux / Windows 等系统;
  • 跨平台/可移植;
  • 使用难度大、使用频率低;
  • 线程生命周期由程序员管理;
  • 现在 iOS 中用到 pthread 的多数情况是使用 pthread_mutex 互斥锁,性能较高。

1.2 pthread 的简单使用

#import <pthread.h>

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //创建子线程
    pthread_t pthread; //线程编号
    /*
     参数一:线程编号的地址
     参数二:线程的属性
     参数三:线程要执行的函数 void * (*)(void *)
     参数四:函数的参数,参数类型:void *
     返回值:0代表成功,非0代表失败
     pthread_create(pthread_t  _Nullable *restrict _Nonnull,
                    const pthread_attr_t *restrict _Nullable,
                    void * _Nullable (* _Nonnull)(void * _Nullable),
                    void *restrict _Nullable)
     */
    int result = pthread_create(&pthread, NULL, demo, NULL);
    if (result == 0) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }
}

void *demo(void *param) {
    NSLog(@"hello,%@",[NSThread currentThread]);
    return NULL;
}

2. NSThread

2.1 NSThread 介绍

  • 使用更加面向对象;
  • 简单易用,可直接操作线程对象;
  • 语言 OC,线程生命周期由程序员管理,偶尔使用。

2.2 NSThread 的基本使用

方式一:需要手动调用 start 方法开启线程

    // SEL
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];
    [thread start];
    // block
    NSThread *thread = [[NSThread alloc] initWithBlock:^{
        NSLog(@"hello,%@",[NSThread currentThread]);
    }];
    [thread start];

方式二:

    // SEL
    [NSThread detachNewThreadSelector:@selector(demo) toTarget:self withObject:nil];
    // block
    [NSThread detachNewThreadWithBlock:^{
        NSLog(@"hello,%@",[NSThread currentThread]);
    }];

方式三:

    // SEL
    [self performSelectorInBackground:@selector(demo) withObject:nil];

2.3 线程的状态、生命周期

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //新建状态
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];
    //就绪状态
    [thread start];  
}

- (void)demo {    
    //运行状态
    for (int i = 0; i < 20; i++) {
        NSLog(@"%d",i);
        if (i == 5) {
            //阻塞状态
            [NSThread sleepForTimeInterval:5];
        }        
        if (i == 10) {
            //线程退出,死亡状态
            [NSThread exit];
        }
    }
}

2.4 线程池的原理

参数名 代表含义
corePoolSize 线程池的基本大小(核心线程池大小)
maximumPoolSize 线程池的最大大小
keepAliveTime 线程池中超过 corePoolSize 数目的空闲线程的最大存活时间
unit keepAliveTime 参数的时间单位
workQueue 任务阻塞队列
threadFactory 新建线程的工厂
handler 当提交的任务数超过 maximumPoolSize 与 workQueue 之和时,任务会交给 RejectedExecutionHandler 来处理

2.5 线程的属性

  • 线程名称
    设置线程名称可以当线程执行的方法内部出现异常的时候记录异常和当前线程。
  • 线程的优先级
    内核调度算法在决定该运行哪个线程时,会把线程的优先级作为考量因素,较高优先级的线程会比较低优先级的线程有更多的运行机会。较高优先级不保证你的线程具体执行的时间,不保证可以等它执行完再执行较低优先级的线程,只是比较低优先级的线程更有可能被调度器选择执行而已。
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];
    //线程名称
    thread.name = @"thread1";
    //线程优先级:范围 0.0-1.0,默认 0.5
    thread.threadPriority = 1.0;
    [thread start];

相关链接

GitHub:https://github.com/dolphin1208/Thread

相关文章

  • OC_NSThread

    原文链接:iOS多线程--彻底学会多线程之『pthread、NSThread』 **NSThread **是苹果官...

  • 多线程(2)——NSThread

    iOS中实现多线程的四种方案 pthread NSThread GCD NSOpreation Pthread:这...

  • iOS多线程.md

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

  • OC多线程分享pthread&NSThread

    一.多线程基础{ 二.pthread{ 三.NSThread{

  • IOS---多线程实现方案二 (GCD)

    IOS---多线程实现方案二 (GCD) 上篇文章讨论了使用PTHread和NSThread的多线程实现。这篇文章...

  • iOS多线程

    iOS中常用的多线程:pthread:C语言 NSThread:OC GCD:C NSOpreration:...

  • 多线程、Runloop、Runtime

    多线程 iOS多线程技术有哪几种方式pthread、NSThread、GCD、NSOperation 1. NSO...

  • iOS多线程

    iOS中常见的多线程方案 pthread NSThread GCD NSOperation GCD GCD执行任务...

  • 关于多线程(NSThread介绍)

    通常在iOS中会遇到四种多线程编程的技术,分别是:(一)pthread(二)NSThread(三)NSOperat...

  • NSOperation学习

    iOS的多线程技术主要有:pthread、NSThread、NSOperation、GCD。怎么选呢?pthrea...

网友评论

      本文标题:iOS - 多线程(二):pthread、NSThread

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