您的位置:首页 > 移动开发 > IOS开发

Ios开发:改变线程优先级

2016-06-03 21:21 302 查看

简介

每个线程执行时都具有一定的优先级,对于UI线程的优先级约为0.75,对于新创建的子线程的默认优先级为0.5,对于优先级高的则会获得较高的执行的机会,为大家介绍一下怎样改变线程的优先级

程序说明

新建一个sing view application,只需要修改程序委托类,其它不需要进行修改,新建了两个线程,使用thread.threadPriority方法改变了线程的优先级,但是在改变优先级之前输出了线程的优先级,run方法作为线程的执行体

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"线程UI的优先级为:%g",[NSThread threadPriority]);
NSThread *thread1=[[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
thread1.name=@"线程A";
NSLog(@"线程的优先级A为:%g",thread1.threadPriority);
thread1.threadPriority=0.0;
NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
thread2.name=@"线程B";
NSLog(@"线程的优先级B为:%g",thread2.threadPriority);
thread2.threadPriority=1.0;
[thread1 start];
[thread2 start];
for (int i=0; i<100; i++) {
NSLog(@"-----%@------%d",[NSThread currentThread],i);
}
}
-(void)run
{
for (int i=0; i<100; i++) {
NSLog(@"-----%@------%d",[NSThread currentThread].name,i);
}
}
@end


结果显示及说明

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: