IOS multi-thread NSThread

Source: Internet
Author: User

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. In ios, there are three methods to use multithreading: (1) NSThread is a relatively primitive method to use threads. It is a lightweight method, compared with other methods, more intuitive control of thread objects. 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 encapsulate the operations to be executed in an object-oriented method, and then place the operations in an 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 is a thread class. An NSThread instance represents a thread. Obtain the main thread code NSThread * mainThread = [NSThread mainThread]; obtain the current thread code NSThread * currentThread = [NSThread currentThread]; Create NSThread (1) dynamic method: to create a thread using the object method, you must 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];

 

Code of a method called by enabling a thread
-(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 the Code [NSThread detachNewThreadSelector: @ selector (startThread :) toTarget: self withObject: @ "jredu"]; (3) enable the thread code by using the implicit method [self Checking mselectorinbackground: @ 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];

 

Perform an operation on a thread (1) run the operation code in the specified Thread [self defined mselector: @ selector (run) onThread: thread1 withObject: nil waitUntilDone: YES]; (2) run the operation code in the main thread [self defined mselecw.mainthread: @ selector (run) withObject: nil waitUntilDone: YES]; (3) run the operation code in the current thread [self defined mselector: @ selector (run) withObject: nil];

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.