iOS多线程 - NSThread介绍

来源:互联网 发布:java自学步骤 编辑:程序博客网 时间:2024/05/22 06:28

多线程实现技术方案

pThread、NSThread、GCD、NSOperation

NSThread
NSThread是经过苹果封装后的,面向对象的,我们可以直接来操控线程对象。

我们创建一个iOS项目来演示

////  ViewController.m//  TestThread//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // 创建一个按钮,点击按钮,就执行    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];    btn.frame = CGRectMake(100, 100, 100, 30);    [btn setTitle:@"NSThread" forState:UIControlStateNormal];    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [self.view addSubview:btn];    // 给按钮绑定点击事件    [btn addTarget:self action:@selector(clickNSThread) forControlEvents:UIControlEventTouchUpInside];}// 此方法内部完成,创建线程并执行线程-(void) clickNSThread{    NSLog(@"主线程执行");    // 创建线程    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(runThread1) object:nil];    // 开启(执行线程)    [thread1 start];}// 线程执行的方法- (void)runThread1{    NSLog(@"子线程执行");    for (int i=1; i<=10; i++) {        NSLog(@"%d",i);        sleep(1);    }}@end

我们首先创建了一个按钮,点击该按钮,就调用一个方法clickNSThread,该方法会创建线程并执行线程,该线程绑定了一个runThread1方法在子线程中执行。

我们查看打印的llog信息如图:
这里写图片描述
主线程id16268370,子线程id16268763,我们发现确实是2个不同的线程。

NSThread其他创建方式

上面我们是通过alloc init的方法创建并执行线程。
还有另外的方式。

    // 1.通过alloc init方式    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(runThread1) object:nil];    // 开启(执行线程)    [thread1 start];    // 2.通过detachNewThreadSelector方式//    [NSThread detachNewThreadSelector:@selector(runThread1) toTarget:self withObject:nil];    // 3.通过performSelectorInBackground方式//    [self performSelectorInBackground:@selector(runThread1) withObject:nil];

除了1,还有2,3创建NSThread的方式。
注意第三种方式,通过方法名我们可以看出就是在后台执行,也就是在子线程执行。
这里我们要介绍与之对应的一个方法performSelectorOnMainThread,在主线程执行。
我们来修改一下代码,在子线程中,循环到最后一次,就回到主线程。

- (void)runThread1{    NSLog(@"子线程执行");    for (int i=1; i<=10; i++) {        NSLog(@"%d",i);        sleep(1);        if(i == 10){            [self performSelectorOnMainThread:@selector(runMainThread) withObject:nil waitUntilDone:YES];        }    }}// 主线程执行的方法- (void)runMainThread{    NSLog(@"回到主线程执行");}

这里写图片描述
从log可以看出:果然回到了主线程(线程ID一致)。

NSThread的其他属性

上面我们知道,创建NSThread的三种方式,我们推荐使用第一种。
因为第一种可以设置线程的属性。
比如我们可以给线程设置name

-(void) clickNSThread{    NSLog(@"主线程执行");    // 1.通过alloc init方式    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(runThread1) object:nil];    [thread1 setName:@"name_thread1"];    // 开启(执行线程)    [thread1 start];    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(runThread1) object:nil];    [thread2 setName:@"name_thread2"];    // 开启(执行线程)    [thread2 start];}

创建了2个线程,并设置了名称,都绑定了runThread1方法,我们在runThread1里获取设置的线程名称。

    // [NSThread currentThread].name 获取线程名称    NSLog(@"%@,子线程执行",[NSThread currentThread].name);

这里写图片描述
这样我们在程序调试的时候,就可以跟进设置的线程名称来判别。

另一个属性:优先级。
通过setThreadPriority方法设置,取值0~1,值越大的优先级越高。

    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(runThread1) object:nil];    [thread1 setName:@"name_thread1"];    [thread1 setThreadPriority:0.2];    // 开启(执行线程)    [thread1 start];    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(runThread1) object:nil];    [thread2 setName:@"name_thread2"];    [thread2 setThreadPriority:0.5];    // 开启(执行线程)    [thread2 start];

这里写图片描述
通过打印的log,可以看出thread2的优先级比thread1高。

0 0
原创粉丝点击