您的位置:首页 > 移动开发 > IOS开发

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

2017-09-02 21:00 721 查看
多线程01iOS中多线程的实现方案
一介绍

二pthread方式实现多线程

三NSThread创建线程
1 线程属性的部分方法

2 detachNewThreadWithBlock 方法创建线程

3 etachNewThreadSelectorSEL toTargetid withObjectnullable id

4 performSelectorInBackgroundSEL withObjectnullable id

5 initWithBlockvoid void

6 instancetypeinitWithTargetid selectorSEL objectnullable id

三主线程相关用法

多线程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; // 是否为主线程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: