Three ways to create multi-threaded IOS

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" ><strong> (1)//create thread by NSObject method </strong></span>    //(this method will automatically open a background thread, parameter 1: The method executed in this background thread, Parameter 2: for passing parameters)    [self performselectorinbackground: @selector (Banzhuanplus) Withobject:nil];

(2)//Create a thread through Nsthread (parameter 1: The performer of the method; Parameter 2: The method executed in the thread; parameter 3: Used to pass parameters)
First step: Create a thread
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (banzhuanplus) Object:nil];
Step Two: Execute
[Thread start];

[Thread release];


(3)//nsoperation is an operation unit, used to execute methods, is an abstract class, must be sub-class or use the system to create a good subclass (Nsinvocationoperation or nsblockoperation)
The nsoperation is the smallest operating unit and can only be executed once;

Nsinvocationoperation First step: Create
Nsinvocationoperation *invocation = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector ( Banzhuanplus) Object:nil];
Second step: (Do not set the words are not added to the queue) in the main thread to execute
[Invocation start];

Nsblockoperation First step: Create
Nsblockoperation *block = [Nsblockoperation blockoperationwithblock:^{
[Self banzhuanplus];
}];
Step Two: Execute (execute in main thread)
[Block start];//If added to the queue, don't start.

This queue will automatically help us create a secondary thread.
This queue can only add nsoperation and sub-class objects;
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];
[Queue setmaxconcurrentoperationcount:2];//Sets the maximum number of parallel;
[Queue addoperation:block];//is executed as long as the operation queue is added to the queue;
[Queue addoperation:invocation];

Queue: FIFO
Stack: Advanced back out

The queue involves serial and parallel
Serial: Only one task can be performed at a time
Parallel: Multiple tasks can be performed at one time
(When copying the whole piece, note that there is no annotation in one)


Three ways to create multi-threaded IOS

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.