iOS开发多线程-创建线程

iOS中实现多线程的方案有4种

一、创建和启动线程简单说明

  一个NSThread对象就代表一条线程

  创建、启动线程

  (1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];

  [thread start];

  // 线程一启动,就会在线程thread中执行self的run方法

 

  主线程相关用法

    + (NSThread *)mainThread; // 获得主线程

    - (BOOL)isMainThread; // 是否为主线程

    + (BOOL)isMainThread; // 是否为主线程

 
  其他用法

  获得当前线程

  NSThread *current = [NSThread currentThread];

 

  线程的调度优先级:调度优先级的取值范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高

  + (double)threadPriority;

  + (BOOL)setThreadPriority:(double)p;

 

  设置线程的名字

  - (void)setName:(NSString *)n;

  - (NSString *)name;

 

  其他创建线程的方式
  (2)创建线程后自动启动线程   [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
  (3)隐式创建并启动线程  [self performSelectorInBackground:@selector(run) withObject:nil];
  上述2种创建线程方式的优缺点
    优点:简单快捷
    缺点:无法对线程进行更详细的设置

二、代码示例

1.使用古老的方式创建

#import "ViewController.h"
#import <pthread.h> // pthread是C语言中的,使用时要导入头文件

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

// 按钮的点击事件
- (IBAction)btnClick {
    // 获取当前线程
    NSThread *current = [NSThread currentThread];
    
    // 主线程
    NSLog(@"btnClick---%@", current);
    
    // 使用for循环执行一些耗时的操作
    pthread_t thread;
    // 多个线程,点击按钮的时候,主线程不会被阻塞
    /**
     *  参数1:线程对象
     *  参数2:线程属性
     *  参数3:指向函数的指针
     *  参数4:函数的{参数}
     */
    pthread_create(&thread, NULL, run, NULL);
}

// C语言函数
void *run(void *data)
{
    // 获取当前线程,是新创建的子线程
    NSThread *current = [NSThread currentThread];
    
    for (NSInteger i = 0; i < 10000; i++) {
        NSLog(@"run---%zd---%@", i, current);
    }
    return NULL;
}

@end

实现效果:

打印结果:



2.使用NSThread创建线程

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

// 按钮的点击事件
- (IBAction)btnClick {
    // 获取到当前线程
    NSThread *current = [NSThread currentThread];
    
    // 主线程
    NSLog(@"btnClick---%@", current);
    
    // 获取主线程的另一种方式
    NSThread *main = [NSThread mainThread];
    NSLog(@"主线程------%@", main);
    
    // 执行一些耗时的操作
    //[self creatNSThread];
    //[self creatNSThread2];
    [self creatNSThread3];
}

/**
 *  NSThread的创建线程方式1
 *  1> 先创建初始化线程
 *  2> start开启线程
 */
- (void)creatNSThread
{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"线程A"];
    // 设置线程的名称
    thread.name = @"线程A";
    // 设置优先级 0.0~1.0 默认是0.5  最高是1.0
    thread.threadPriority = 1.0;
    // 开启线程
    [thread start];
    
    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"线程B"];
    // 设置线程的名称
    thread2.name = @"线程B";
    // 开启线程
    [thread2 start];
}

/**
 *  NSThread创建线程方式2
 *  创建完线程直接(自动)启动
 */
- (void)creatNSThread2
{
    // 分离出一条子线程,自动启动,不能详细的设置线程对象
    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"];
}


/**
 *  NSThread创建线程方式2
 *  隐式创建线程,并且直接(自动)启动
 */
- (void)creatNSThread3
{
    // 开启后台线程,自动启动,不能详细的设置线程对象
    [self performSelectorInBackground:@selector(run:) withObject:@"隐式创建"];
}

- (void)run:(NSString *)str
{
    // 获取当前线程
    NSThread *current = [NSThread currentThread];
    
    // 打印输出
    for (NSInteger i = 0; i < 10; i++) {
        NSLog(@"run---%@---%@", current, str);
    }
}


@end

调用线程1


调用线程2


调用线程3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值