NSThread and nsthread for iOS Multithreading

Source: Internet
Author: User

NSThread and nsthread for iOS Multithreading
I. Basic concepts of NSThread

NSThread is a lightweight multi-threaded programming method based on threads (compared with GCD and NSOperation). An NSThread object represents a thread and needs to manually manage the thread lifecycle to handle thread synchronization and other issues.

Ii. Create and start a thread

1. Dynamic instantiation-first create and then manually start

NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (loadingImage) object: nil]; // thread startup, execute the self loadingImage method [thread start] In the thread;

2. Static instantiation-self-starting after creation

// Create an auto-start instance and run the loadingImage method [NSThread detachNewThreadSelector: @ selector (loadingImage) toTarget: self withObject: nil];

3. Implicit instantiation-self-starting after creation

// Create an auto-start instance and execute the loadingImage method [self generated mselectorinbackground: @ selector (loadingImage) withObject: nil];
Iii. thread control

1. Pause

+ (void)sleepUntilDate:(NSDate *)date;+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

The pause of NSThread will block the current thread.

2. Cancel

- (void)cancel

NSThreadCanceling a thread does not immediately stop and exit the thread, but only marks the status (whether the thread needs to exit ).

3. The thread is stopped.

+ (void)exit

 NSThreadThe stop method immediately terminates all threads except the main thread (whether or not the task is being executed) and exits,Use it with caution!Otherwise, memory problems may occur.

Iv. NSThread

1. Some common methods

// Obtain the main thread + (NSThread *) mainThread; // determine whether the main thread (Object method)-(BOOL) isMainThread; // determine whether the main thread (class method) + (BOOL) isMainThread; // obtain the current thread NSThread * current = [NSThread currentThread];

2. thread priority settings

// [NSThread setThreadPriority: 1.0] Before iOS8; // (0.0,-1.0, 1.0) after ios8. // [NSThread setQualityOfService: NSQualityOfServiceUserInitiated] After ios8. /* The enumerated values of qualityOfService are as follows: NSQualityOfServiceUserInteractive: highest priority, used for User Interaction Events NSQualityOfServiceUserInitiated: second highest priority, used for events that need to be executed immediately by the user NSQualityOfServiceDefault:, the main thread and threads with no priority set are set to NSQualityOfServiceUtility by default: normal priority, used for normal tasks NSQualityOfServiceBackground: the lowest priority for unimportant tasks */

3. Inter-thread Communication

// 1. Specify the current thread to execute the operation [self defined mselector: @ selector (run)]; [self defined mselector: @ selector (run) withObject: nil]; [self defined mselector: @ selector (run) withObject: nil afterDelay: 3.0f]; --------------------------- split line operator // 2. Specify to execute operations in the main thread (such as updating the UI) [self defined mselecw.mainthread: @ selector) withObject: nil waitUntilDone: YES]; --------------------------- split line operator // 3. Specify to operate in another thread (main thread-> New thread) // specify to be a thread newThread [self executor mselector: @ selector (run) onThread: newThread withObject: nil waitUntilDone: YES]; // specify this as the background thread [self defined mselectorinbackground: @ selector (run) withObject: nil];
5. Thread Synchronization

Multithreading inevitably leads to competition for shared resources (such as memory and data sources) During Concurrent execution of different threads, which leads to data inconsistency (dirty data ), even serious deadlocks.

Thread Synchronization means that only one thread is allowed to access a certain resource within a certain period of time, which is like dispatch_barrier or dispatch_semphore in GCD.

Currently, NSLock and @ synchronized are two ways to implement thread locking in iOS.

For more information about thread locks, see: http://www.jianshu.com/p/35dd92bcfe8c

 

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.