线程属性
- 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]);
}
}