Brief Introduction
Each thread executes with a certain priority, with a priority of about 0.75 for the UI thread, a default priority of 0.5 for the newly created child thread, and a higher chance of execution for the higher priority, to show you how to change the thread's priority program Description
Create a new Sing view application, only need to modify the program delegate class, others do not need to be modified, the new two threads, The priority of the thread is changed using the Thread.threadpriority method, but the priority of the thread is output before the priority is changed, and the Run method acts as the thread's executing body
#import "ViewController.h"
@interface viewcontroller ()
@end
@implementation viewcontroller
-(void ) Viewdidload {
[super viewdidload];
NSLog (@ "Thread UI priority is:%g", [Nsthread threadpriority]);
Nsthread *thread1=[[nsthread alloc]initwithtarget:self selector: @selector (run) Object:nil];
thread1.name=@ "Thread A";
NSLog (@ "Thread priority A is:%g", thread1.threadpriority);
thread1.threadpriority=0.0;
Nsthread *thread2=[[nsthread alloc]initwithtarget:self selector: @selector (run) Object:nil];
thread2.name=@ "Thread B";
NSLog (@ "Thread priority B is:%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
The results show and explain