Multithreading Supplement---DAY02

Source: Internet
Author: User

01-The main difference between concurrent queues and global concurrent queues created with the CREATE function:

1. Global concurrent queues exist by default in the entire application, with high priority, low priority, default priority, and a total of four concurrent queues in the background priority.

2. When using the Grid function, Apple officially stipulates that the fence function is only valid when used with the Creste function.

02-xcode after version 7.0. HTTP request Issue modification Info.plist nsapptransportsecurity modified to dictionary type, add Nsallowarbitraryloadskey modified to Yes.

#####################

01-Singleton Mode

(1) Singleton mode in the process of running a program, there is only one instance of a class

(2) share a single resource throughout the application (this resource needs to be created only once)

02-ARC implementation of a single case

(1) Provide a static modified global variable inside the class

(2) Provide a class method to facilitate external access

(3) Overriding the +allocwithzone method to ensure that only one memory space is allocated once for a single object

(4) for rigor, rewrite the-copywithzone method and the-mutablecopywithzone method

03-MRC implementation of a single case rarely used, self-check code.

#######################

2.NSOperation Basic Use

1.NSOperation is the packaging of GCD, two core concepts [queue + operation]

The 2.NSOperation itself is an abstract class, only its subclasses. The three subclasses are: Nsblockoperation,nsinvocationoperaion, and custom-inherited classes that inherit from Nsoperation. Nsoperation and Nsoperationqueue are used together to achieve multithreading concurrency.

(3) Related code

"' OBJC

Nsinvocationoperation

1. Encapsulation operation

/*

First parameter: target object

Second parameter: The method to be called by the operation, with a maximum of one parameter

The third argument: call the parameter passed by the method, and if the method does not accept the argument, the value is nil

*/

Nsinvocationoperation *operation = [[Nsinvocationoperation alloc]

Initwithtarget:self selector: @selector (run) Object:nil];

2. Start the operation

[Operation start];

-------------------------------------------------

Nsblockoperation

1. Encapsulation operation

/*

Nsblockoperation provides a class method for encapsulating operations in this class of methods

*/

Nsblockoperation *operation = [Nsblockoperation blockoperationwithblock:^{

Executing in the main thread

NSLog (@ "---download1--%@", [Nsthread CurrentThread]);

}];

2. Append operation, append operation executed in child thread

[Operation Addexecutionblock:^{

NSLog (@ "---download2--%@", [Nsthread CurrentThread]);

}];

[Operation Addexecutionblock:^{

NSLog (@ "---download3--%@", [Nsthread CurrentThread]);

}];

3. Start the execution action

[Operation start];

----------------------------------------------

03 Custom Nsoperation

How do I encapsulate an operation?

Custom Nsoperation, by overriding the internal main method to implement encapsulation operations

-(void) Main

{

NSLog (@ "--main--%@", [Nsthread CurrentThread]);

}

How do I use it?

1. Instantiate a custom Action object

Xmgoperation *op = [[Xmgoperation alloc]init];

2. Perform the action

[op start];

```

-2.2 Nsoperationqueue Basic use

(1) Two types of queues in nsoperation

01 The primary queue is obtained through Mainqueue, and all tasks placed in the main queue are executed on the primary thread.

02 Non-Home Row Direct Alloc init out of queue. The non-Home column also has concurrent and serial capabilities to control whether the task is executed concurrently or serially by setting the maximum Concurrency Number property

(2) Related code

"' OBJC

Custom Nsoperation

-(void) customoperation

{

1. Create a queue

Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];

2. Encapsulation operation

Benefits: 1. Information hiding

2. Code Reuse

Xmgoperation *OP1 = [[Xmgoperation alloc]init];

Xmgoperation *OP2 = [[Xmgoperation alloc]init];

3. Adding actions to the queue

[Queue ADDOPERATION:OP1];

[Queue ADDOPERATION:OP2];

}

Nsblockoperation

-(void) block

{

1. Create a queue

Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];

2. Encapsulation operation

Nsblockoperation *OP1 = [Nsblockoperation blockoperationwithblock:^{

NSLog (@ "1----%@", [Nsthread CurrentThread]);

}];

Nsblockoperation *OP2 = [Nsblockoperation blockoperationwithblock:^{

NSLog (@ "2----%@", [Nsthread CurrentThread]);

}];

[Op2 addexecutionblock:^{

NSLog (@ "3----%@", [Nsthread CurrentThread]);

}];

[Op2 addexecutionblock:^{

NSLog (@ "4----%@", [Nsthread CurrentThread]);

}];

3. Adding actions to the queue

[Queue ADDOPERATION:OP1];

[Queue ADDOPERATION:OP2];

Add: A simple method

[Queue addoperationwithblock:^{

NSLog (@ "5----%@", [Nsthread CurrentThread]);

}];

}

Nsinvocationoperation

-(void) invocation

{

/*

Queues in the GCD:

Serial queue: Self-created, home row

Concurrent queues: Created by yourself, global concurrent queues

Nsoperationqueue

Main queue: [Nsoperationqueue mainqueue]; everything placed in the master queue is executed in the main thread

Non-Home column: [[[Nsoperationqueue Alloc]init], concurrent and serial, default is concurrent execution

*/

1. Create a queue

Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];

2. Encapsulation operation

Nsinvocationoperation *OP1 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (download1) object: NIL];

Nsinvocationoperation *OP2 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (download2) object: NIL];

Nsinvocationoperation *OP3 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (download3) object: NIL];

3. Add encapsulated operations to the queue

[Queue Addoperation:op1];//[op1 start]

[Queue ADDOPERATION:OP2];

[Queue ADDOPERATION:OP3];

}

2.3nsoperation Other uses

1, set maximum concurrency [control task concurrency and serial]

(1) Set maximum concurrent number "control task concurrency and serial"

"' OBJC

1. Create a queue

Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];

2. Set the maximum number of concurrent

Note: This property needs to be set before the task is added to the queue.

This property controls whether the queue is executed serially or concurrently

If the maximum concurrent number equals 1, then the queue is serial, if it is greater than 1 then it is parallel

The maximum number of concurrency for the system has a default value of-1, and if the property is set to 0, no tasks are performed

Queue.maxconcurrentoperationcount = 2;

```

(2) Suspension and recovery and cancellation

"' OBJC

Set pause and resume

Suspended set to Yes to pause, suspended set to No for recovery

Pause indicates that the next task in the queue is not resumed and the suspend operation can be resumed

if (self.queue.isSuspended) {

self.queue.suspended = NO;

}else

{

self.queue.suspended = YES;

}

Cancel all operations inside the queue

After cancellation, the next operation of the currently executing operation will no longer execute, and will never be executed, as if all subsequent tasks were removed from the queue

The cancel operation is not recoverable

[Self.queue cancelalloperations];

---------Custom Nsoperation Cancel the operation--------------------------

-(void) Main

{

Time-consuming Operation 1

for (int i = 0; i<1000; i++) {

NSLog (@ "Task 1-%d--%@", I,[nsthread CurrentThread]);

}

NSLog (@ "+++++++++++++++++++++++++++++++++");

Apple's official recommendation is that each time a time-consuming operation is performed, the current queue is canceled, and if so, exit directly

The advantage is that it can improve the performance of the program

if (self.iscancelled) {

Return

}

Time-consuming Operation 2

for (int i = 0; i<1000; i++) {

NSLog (@ "Task 1-%d--%@", I,[nsthread CurrentThread]);

}

NSLog (@ "+++++++++++++++++++++++++++++++++");

}

```

#########################

Multi-Image Download Comprehensive instance program

1> involving knowledge points

1 Dictionary Turn model

2 storing data to Shahe, loading data from Shahe

3 for bitmap settings (cell refresh problem)

4 How to cache memory (using Nsdictionary)

05 Some fault-tolerant processing in the process of program development

06 How to refresh the specified line of TableView (to resolve data confusion issues)

Nsoperation and inter-thread communication related knowledge

Multithreading Supplement---day02

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.