IOS multi-thread NSThread and ios multi-thread nsthread

Source: Internet
Author: User

IOS multi-thread NSThread and ios multi-thread nsthread

A running application is a process. By default, a main thread is enabled for a process, but operations in the main thread are serialized, that is, when multiple tasks need to be completed at the same time, they are executed one by one in sequence. Therefore, to improve efficiency, multiple threads are enabled in the process, and each thread can execute different tasks in parallel.

In addition, in ios programs, operations such as processing user touch events and refreshing interfaces must be implemented in the main thread. Therefore, those operations are resource-consuming, for example, operations such as obtaining data from the network and downloading data can be placed in sub-threads. Otherwise, the main thread may be blocked. Once the main thread is blocked, the user experience may be affected.

There are three methods to use multithreading in ios:

(1) NSThread

NSThread is a primitive way to use threads. It is lightweight and can control thread objects more intuitively than other methods. However, in this way, you need to manage the thread lifecycle. If you want to implement synchronization, you also need to lock it, which will increase the system overhead.

(2) NSOperation and NSOperationQueue

NSOperation encapsulates the operation to be executed in an object-oriented method, and then places the operation in a NSOperationQueue for asynchronous execution, without the need to manage threads and process synchronization issues.

(3) Grand Centeral Dispatch

GCD is a C language API. GCD provides some new features to implement parallel programming for multiple device cores.

In this article, we will first explain how to use NSThread.

NSThread

NSThread is a thread class, and an NSThread instance represents a thread.

Get main thread

Code

NSThread *mainThread = [NSThread mainThread];

Get current thread

Code

NSThread *currentThread = [NSThread currentThread];

NSThread Creation

(1) dynamic method: Create a thread using the object method, and manually enable the thread.

Code

// Initialization thread NSThread * thread1 = [[NSThread alloc] initWithTarget: self selector: @ selector (startThread :) object: @ "ios"]; thread1.name = @ "thread1 "; // enable thread [thread1 start];

How to enable a thread call

Code

-(Void) startThread :( NSString *) parm {NSThread * currentThread = [NSThread currentThread]; for (int I = 0; I <10; I ++) {NSLog (@ "parameter % @, current thread % @, thread name % @", parm, currentThread, currentThread. name );}}

(2) Static Method: Enable the thread through the class method, and the system automatically calls

Code

[NSThread detachNewThreadSelector:@selector(startThread:) toTarget:self withObject:@"jredu"];

(3) Enable the thread by using the implicit method

Code

[self performSelectorInBackground:@selector(startThread:) withObject:@"apple"];

Pause the current thread

Code

// Method 1 [NSThread sleepForTimeInterval: 2]; // method 2 NSDate * date = [NSDate dateWithTimeInterval: 2 sinceDate: [NSDate date]; [NSThread sleepUntilDate: date];

Execute operations on a thread

(1) perform operations on the specified Thread

Code

[self performSelector:@selector(run) onThread:thread1 withObject:nil waitUntilDone:YES];

(2) perform operations in the main thread

Code

[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];  

(3) execute operations in the current thread

Code

[self performSelector:@selector(run) withObject:nil]; 

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.