An analysis of IOS multithread development--nsthread _ios

Source: Internet
Author: User
Tags gcd

In the development of IOS , multithreading is mainly implemented in three kinds of ways, Nsthread, Nsoperation and GCD, in my previous blog to Nsoperation and GCD have a more detailed implementation, in order to learn the integrity, Today, we mainly implement the use of Nsthread from the code level. Case code uploaded to https://github.com/chenyufeng1991/NSThread.

(1) Initialize and start a thread

-(void) Viewwillappear: (BOOL) animated
{
[super viewwillappear:animated];
Gets the current thread
nsthread *current = [Nsthread CurrentThread];
NSLog (@ "Current thread is%@";
Initialize thread
nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
Sets the priority of the thread (0.0-1.0)
thread.threadpriority = 1.0;
Thread.Name = @ "new thread 1";
[Thread start];
}
-(void) run
{
NSLog (@ "thread Execution");
Gets the current thread
nsthread *current = [Nsthread CurrentThread];
NSLog (@ "Current thread is%@";
Thread hibernation, which can simulate time-consuming operations
[Nsthread Sleepfortimeinterval:2];
Gets the main thread
nsthread *mainthread = [Nsthread mainthread];
NSLog (@ "thread to get the main thread%@", mainthread);
}

CurrentThread, this method is useful and can often be used to determine which thread the execution of a method is in.

  (2) Nsthread can specify that a thread be executed in the background:


The background creates a thread to perform the task, and you need to use the automatic release pool in the invoked method

[self performselectorinbackground: @selector (RUN3) Withobject:nil];

-(void) run3

{

@autoreleasepool {

NSLog (@ "main thread 3:%@, current thread 3:%@", [Nsthread Mainthread],[nsthread  CurrentThread]);

}

}

(3) The child thread performs time-consuming operations and the main thread updates the UI. This is the most commonly used case in multithreaded development. The Performselectoronmainthread method is called in the child thread to update the main thread.

The test invokes the main thread update UI
-(void) Viewwillappear: (BOOL) animated
{
 [super viewwillappear:animated]

 in a child thread; Nsthread *subthread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
 Nsthread can control thread start
 [subthread start];
}

-(void) run
{
 NSLog (@ "main thread 1:%@, current thread 1:%@", [Nsthread mainthread],[nsthread CurrentThread]);
 The following methods need to call
 [self performselectoronmainthread: @selector (Invocationmainthread) in a child thread Withobject:nil Waituntildone:yes];
}

-(void) Invocationmainthread
{
 NSLog (@ "main thread 2:%@, current thread 2:%@", [Nsthread mainthread],[nsthread CurrentThread] );
 NSLog (@ "Invoke main thread update UI");
}

 (4) Similarly, we can create a new class of child threads that inherits from Nsthread. Then rewrite the main method inside, and the main method is the method that the thread will execute when it starts.

@implementation mythread

-(void) main
{
 NSLog (@ "Main method Execution");

@end

Then start as normal as you created it. The thread automatically executes the main method.

You can write a subclass yourself, inherit from Nsthread, you need to override the Main method
/**
 * The code executed is in main, not the @selector.
 Using the Main method, the method executed in the thread belongs to the object itself, so that it can be used anywhere else where this thread method is needed, without having to implement a method again.
 
 while other direct nsthread create threads, the methods executed within the thread are in the current class file.
 */
-(void) Viewwillappear: (BOOL) Animated
{
 [super viewwillappear:animated];

 Mythread *thread = [[Mythread alloc] init];
 [Thread start];
}

(5) One of the most common methods in Nsthread is latency. Delay 2s execution.

 Thread hibernation, which can simulate time-consuming operations
 [Nsthread Sleepfortimeinterval:2];

For multithreading of three implementations, we must be able to skillfully use

Related Article

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.