Multi-threading implementations in iOS

Source: Internet
Author: User

I go, good egg hurts, just write good blog because the hand in the touch panel on the right row, write all lost, also have to re-write, blog Park There is no solution for this situation? I don't want to write.

One, there are four ways to implement multi-threading in iOS

(1) Nsthread traps are very many, defective, but OC, and occasionally use

(2) gcd in Apple in IOS4, can make full use of the device multi-core, and do not consider the thread, performance than nsthread much better

GCD is more deeply researched, so it is often asked in the interview.

(3) Nsoperation encapsulated a lot of use of the function, in some cases, GCD implementation is more difficult to achieve with nsoperation is very simple, Apple recommended everyone use Nsoperation

Because the Nsoperation package is very good, so it is very simple to use, no technical difficulties, in the interview did not ask

Two. How to use Nsthread

Nsthread is easy to use and more flexible.

(1) General method: CurrentThread

Nsthread *thread = [Nsthread CurrentThread];

The current thread, which can be used in all multithreading techniques (referred to as nsthread\gcd\nsopertion) to get the current thread

(2) The first method, the simplest one

/** instantiate Initwithtarget method without parameters */

    // 1. Instantiating a thread object, run is a time-consuming task that works on other threads without affecting the main thread!     Object: nil];         // 2. Start the thread and call the Run method immediately    [thread start];

Instantiate a thread object and place time-consuming tasks on other threads

Must start before the Run method is executed

Initwithtarget method for instantiating/** with parameters */

Object:@ "hehe"];     @" Thread Initwithtarget ";

Note that with the parameter @selector (run:) plus a colon

You can set the Name property of the Thread object

(3) The second method

/** Detach method without parameters */

[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];
-(void) run

A new thread is started immediately, running the Run method

Detach method for/** with parameters */

    // object is the first argument    to call a method [Nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "Hello"];
-(void) Run: (ID) obj

Note that with the parameter @selector (run:) plus a colon

(4) The third method

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.