多线程:线程的属性

//
//  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值