iOS development: changing thread priority

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.