Multi-threaded programming 1, c multi-threaded programming

Source: Internet
Author: User

Multi-threaded programming 1, c multi-threaded programming

Each iOS application has a main thread dedicated to updating the display UI and handling users' touch events. Therefore, it cannot execute other time-consuming operations in the main thread, otherwise, the main thread will be congested (with a card connection), resulting in a very bad user experience. The general solution is to put the time-consuming operations in another thread for execution. multi-threaded programming is the best way to prevent the main thread from being congested and increase the running efficiency.

IOS supports multiple levels of multi-threaded programming. The higher the level, the higher the abstraction level, and the more convenient it is to use. It is also Apple's most recommended method. The following lists the multi-threaded programming methods supported by iOS from the abstract level in sequence:

1.Thread: The three methods are relatively lightweight, but the thread lifecycle, synchronization, and locking issues need to be managed, which will lead to certain performance overhead.
2.Cocoa Operations: It is implemented based on OC. NSOperation encapsulates the operations to be executed in an object-oriented manner without worrying about thread management and synchronization. NSOperation is an abstract base class. iOS provides two default implementations: NSInvocationOperation and NSBlockOperation. You can also customize NSOperation.
3.Grand Central Dispatch(GCD or iOS4): it provides some new features and runtime libraries to support multi-core parallel programming. It focuses more on how to improve efficiency on multiple CPUs.

This article briefly introduces the first multi-threaded programming method, mainly using the NSThread class. An NSThread instance represents a thread.

I. NSthread Initialization

1. Dynamic Method

[Java]View plain copy
  1. -(Id) initWithTarget :( id) target selector :( SEL) selector object :( id) argument;
[Java]View plain copy
  1. // Initialize the thread
  2. NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil];
  3. // Set the thread priority (0.0-1.0, 1.0 highest level)
  4. Thread. threadPriority = 1;
  5. // Enable the thread
  6. [Thread start];

Parameter Parsing:

Selector: Method executed by the thread. This selector can only receive one parameter at most.
Target: Selector message sending object
Argument: The unique parameter passed to selector, or nil

 

2. Static Method

[Java]View plain copy
  1. + (Void) detachNewThreadSelector :( SEL) selector toTarget :( id) target withObject :( id) argument;
[Java]View plain copy
  1. [NSThread detachNewThreadSelector: @ selector (run) toTarget: self withObject: nil];
  2. // After the call is completed, a new thread is created and enabled immediately.

3. Implicit thread Creation Method

[Java]View plain copy
  1. [Self defined mselectorinbackground: @ selector (run) withObject: nil];

 

Ii. Obtain the current thread

[Java]View plain copy
  1. NSThread * current = [NSThread currentThread];

 

3. Obtain the main thread

 

[Java]View plain copy
  1. NSThread * main = [NSThread mainThread];

4. Pause the current thread

[Java]View plain copy
  1. // Pause for 2 s
  2. [NSThread sleepForTimeInterval: 2];
  3. // Or
  4. NSDate * date = [NSDate dateWithTimeInterval: 2 sinceDate: [NSDate date];
  5. [NSThread sleepUntilDate: date];

 

5. Inter-thread Communication

1. Execute operations on the specified Thread

[Java]View plain copy
  1. [Self defined mselector: @ selector (run) onThread: thread withObject: nil waitUntilDone: YES];

2. Execute operations on the main thread

 

[Java]View plain copy
  1. [Self defined mselecw.mainthread: @ selector (run) withObject: nil waitUntilDone: YES];

 

3. Execute operations in the current thread

 

[Java]View plain copy
  1. [Self defined mselector: @ selector (run) withObject: nil];

Vi. Advantages and Disadvantages

1. Advantage: NSThread is lighter than the other two multi-threaded solutions, allowing you to control thread objects more intuitively.

2. Disadvantages: You need to manage the thread lifecycle and synchronize the thread. Thread Synchronization locks data with a certain amount of system overhead

 

 

Article Source: http://blog.csdn.net/q199109106q/article/details/8566222

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.