Multithreading (i) Nsthread

Source: Internet
Author: User

Multi-Threading implementations in iOS:

Technology Language Thread life cycle Frequency of Use
Pthread C Programmers manage themselves Almost no
Nsthread Oc Programmers manage themselves Occasional use
GCD C Automatic Management Frequently used
Nsoperation Oc Automatic Management Frequently used

The state of the thread

how Nsthread is created :

    Create thread mode one    nsthread *threadone = [[Nsthread alloc] initwithtarget:self selector: @selector (testaction) Object:nil];    //Give the thread a name    Threadone.name = @ "Threadone";    Start the thread, execute the Testaction method on the newly opened thread    [threadone start];       Thread mode Two is created and will start automatically    [Nsthread detachnewthreadselector: @selector (testaction) totarget:self Withobject:nil];        Create thread mode three, implicit creation, auto-start    [self performselectorinbackground: @selector (testaction) Withobject:nil];

method to invoke

-(void) testaction{    for (int i = 0; i < 3; i++)    {        NSLog (@ "i =%d, current thread =%@", I,[nsthread CurrentThread]);    }}

Result: You can see that there are 3 threads executing concurrently

Properties of the thread:

    Create a thread    nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (testaction) Object:nil];    Thread name    Thread.Name = @ "WL";    Thread priority, general not set, default 0.5, numeric range 0-1, the higher the value the higher the priority    thread.threadpriority = 0.5;

Common methods, which are class methods that operate relative to the thread in which the code resides

    Get the main thread    [Nsthread mainthread];    Determines whether the main thread, returns a bool value    bool Ismainthread = [Nsthread ismainthread];    Returns a bool value    bool ismultithreaded = [Nsthread ismultithreaded] to determine whether it is multi-threaded;    Remove a thread from a pool of scheduled threads 2s (blocking threads)    [Nsthread sleepfortimeinterval:2];    Remove threads from the pool of scheduled threads until a point in time (blocking threads)    [Nsthread sleepuntildate:[nsdate Datewithtimeintervalsincenow:2]];    Stop thread, thread dead, this thread no longer exists    [Nsthread exit];

Multithreading (i) Nsthread

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.