iOS 开发 多线程详解之线程属性设置

线程属性

  • name - 线程名称
  • threadPriority - 线程优先级
  • stackSize - 栈区大小

name - 线程名称

  • 给线程起名字,可以方便运行调试,定位BUG
  • 在大型的商业软件中,都会设计专门的线程做特定的事情,当程序崩溃时可以快速准确的定位BUG

threadPriority - 线程优先级

  • 为浮点数整形,范围在0~1之间,1最高,默认0.5,不建议修改线程优先级
  • 线程的”优先级”不是决定线程调用顺序的,他是决定线程备CPU调用的频率的
  • 在开发的时候,不要修改优先级
  • 多线程开发的原则是越简单越好

stackSize - 栈区大小

  • 默认情况下,无论是主线程还是子线程,栈区大小都是512KB
  • 栈区大小可以设置,最小16KB,但是必须是4KB的整数倍

代码演示

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"主线程栈区空间大小 => %tu",[NSThread currentThread].stackSize/1024);

    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];

    // 给线程起名字,可以方便运行调试,定位BUG
    // 在大型的商业软件中,都会设计专门的线程做特定的事情
    thread1.name = @"download A";

    // 线程调用优先级
    // 线程的"优先级"不是决定线程调用顺序的,他是决定线程备CPU调用的频率的
    // 范围在0~1之间,1最高,默认0.5,不建议修改线程优先级
    thread1.threadPriority = 1.0;

    // 线程就绪
    [thread1 start];

    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];
    thread2.name = @"download B";
    thread2.threadPriority = 0;
    [thread2 start];
}
- (void)demo
{
    NSLog(@"子线程栈区空间大小 => %tu",[NSThread currentThread].stackSize/1024);

    for (int i = 0; i < 10; i++) {
        NSLog(@"%@",[NSThread currentThread]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值