iOS学习笔记-102.多线程01——iOS中多线程的实现方案

多线程01——iOS中多线程的实现方案

一、介绍

这里写图片描述

二、pthread方式实现多线程

说明:pthread的基本使用(需要包含头文件)

#import <pthread.h>

使用的函数是

//第一个参数:线程对象地址
//第二个参数:线程属性
//第三个参数:指向函数的指针
//第四个参数:传递给该函数的参数
int pthread_create(pthread_t _Nullable * _Nonnull __restrict,
        const pthread_attr_t * _Nullable __restrict,
        void * _Nullable (* _Nonnull)(void * _Nullable),
        void * _Nullable __restrict);

pthread创建线程的代码

//================================pthreadDemo================================
- (void)pthreadDemo{
    pthread_t threadId = NULL;
    NSString *name = @"wiming";
    pthread_create(&threadId, NULL ,task1, (__bridge void*)(name));

}

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

结果

<NSThread: 0x60000007e080>{number = 3, name = (null)}----wiming

三、NSThread创建线程

3.1 线程属性的部分方法

//线程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;
//线程优先级
//优先级,取值范围是 0.0~1.0 之间,默认是0.5 最高1.0
- (void)setThreadPriority:(double)
- (double)threadPriority

3.2 detachNewThreadWithBlock 方法创建线程

使用的是 NSThread的下面这个方法来创建线程

//参数是:需要处理的任务代码块
+ (void)detachNewThreadWithBlock:(void (^)(void))block

示例代码

-(void)nsThreadDemo1{
    //分离子线程
    [NSThread detachNewThreadWithBlock:^{
        NSLog(@"detachNewThreadWithBlock : %@",[NSThread currentThread]);
    }];
}

结果

detachNewThreadWithBlock : <NSThread: 0x608000261b40>{number = 3, name = (null)}

3.3 etachNewThreadSelector:(SEL) toTarget:(id) withObject:(nullable id)

使用的是 NSThread的下面这个方法来创建线程

//第一个参数:方法选择器
//第二个参数:目标对象
//第三个参数:传递给调用方法的参数
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(nullable id)argument;

示例代码

-(void)nsThreadDemo2{
    //分离子线程
    [NSThread detachNewThreadSelector:@selector(task2:) toTarget:self withObject:@"detachNewThreadSelector"];
}

-(void) task2:(NSString*)param{
    NSLog(@"%@-----%@",[NSThread currentThread],param);
}

结果

<NSThread: 0x608000267bc0>{number = 3, name = (null)}-----detachNewThreadSelector

3.4 performSelectorInBackground:(SEL) withObject:(nullable id)

使用的是 NSThread的下面这个方法来创建线程

//后台线程
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg

示例代码

-(void)nsThreadDemo3{
    [self performSelectorInBackground:@selector(task2:) withObject:@"performSelectorInBackground"];
}

-(void) task2:(NSString*)param{
    NSLog(@"%@-----%@",[NSThread currentThread],param);
}

结果

<NSThread: 0x6080000753c0>{number = 3, name = (null)}-----performSelectorInBackground

3.5 initWithBlock:(void (^)(void))

使用的是 NSThread的下面这个方法来创建线程

//参数就是任务代码块
- (instancetype)initWithBlock:(void (^)(void))block 

示例代码

-(void)nsThreadDemo4{
    NSThread *thread = [[NSThread alloc]initWithBlock:^{
      NSLog(@"initWithBlock : %@",[NSThread currentThread]);
    }];
    //设置线程的名字
    thread.name = @"nsThreadDemo4";
    //启动线程
    [thread start];
}

结果

initWithBlock : <NSThread: 0x600000264640>{number = 3, name = nsThreadDemo4}

3.6 (instancetype)initWithTarget:(id) selector:(SEL) object:(nullable id)

使用的是 NSThread的下面这个方法来创建线程

- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument

示例代码

-(void)nsThreadDemo5{
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(task2:) object:@"initWithTarget"];
    //名字
    thread.name = @"nsThreadDemo5";
    //优先级,取值范围是 0.0~1.0 之间,默认是0.5 最高1.0
    thread.threadPriority = 1.0;
    [thread start];
}

-(void) task2:(NSString*)param{
    NSLog(@"%@-----%@",[NSThread currentThread],param);
}

结果

<NSThread: 0x60800007ccc0>{number = 3, name = nsThreadDemo5}-----initWithTarget

三、主线程相关用法

+ (NSThread *)mainThread; // 获得主线程
- (BOOL)isMainThread; // 是否为主线程
+ (BOOL)isMainThread; // 是否为主线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值