//
// ViewController.m
// 05-线程属性
//
// Created by gzxzmac on 16/1/28.
// Copyright © 2016年 gzxzmac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSThread *thread = [NSThread currentThread];
NSLog(@"%@",[NSThread currentThread]);
// 输出栈区大小
NSLog(@"%zd",[thread stackSize] / 1024);
NSLog(@"主线程优先级 %f",thread.threadPriority);
// return;
NSThread *thread0 = [[NSThread alloc]initWithTarget:self selector:@selector(demo) object:nil];
// 设置线程的名字 设置线程的名字,在调试的时候可以直接定位到哪个线程出了问题
// 大项目有专有的线程 -> 服务端
thread0.name = @"thread 1";
// 设置线程栈区大小
// 设置栈区大小,必须是4的倍数.如果不是4的倍数,会默认512 kb.
// 栈区大小不要设置太小,太小程序就会崩了.最好是不要修改
thread0.stackSize = 512 * 1024;
// 线程优先级 优先级必须是0.0 - 1.0 ,1.0是优先级最高
// 优先级不是先执行完再执行其他,而是cpu 调度的频率会高点
// 最好不要修改优先级
thread0.threadPriority = 0.99;
[thread0 start];
[NSThread detachNewThreadSelector:@selector(demo) toTarget:self withObject:nil];
}
- (void)demo {
NSThread *thread = [NSThread currentThread];
// 输出栈区大小
NSLog(@"%zd",[thread stackSize] / 1024);
NSLog(@"子线程优先级 %f",thread.threadPriority);
for (int i = 0; i < 20; ++i) {
NSLog(@"%@ %d",[NSThread currentThread],i);
}
}
@end
多线程:线程的属性
最新推荐文章于 2022-09-15 21:53:58 发布