Operation (multithreading) and operation Multithreading

Source: Internet
Author: User

Operation (multithreading) and operation Multithreading
1. Process and thread 1.1 Process

  • Process: A running application is called a process.
  • Processes are independent and run in dedicated and protected memory.
  • Two processes cannot communicate with each other.

In plain terms, two apps are enabled simultaneously on the mobile phone. These two apps must be in different processes. Therefore, the two apps are independent, and data in the memory cannot be transferred to each other, and there is no way to communicate between the two apps.

Is there any way between the two apps to communicate? I am talking about the normal situation. Of course there are still abnormal situations, such as using a very small number of tools for inter-process communication provided by iOS.

Thread 1.2
  • Thread: A process must have at least one thread to execute a task.
  • A thread is used to work.
  • Once the program is started, the process starts. By default, a thread is enabled for a process.

Working thread? Yes, too many people live, or don't want to wait for each other to waste time. Of course, you can find a few people to do it at the same time.
For example, in a project, the product manager's requirements have not been fully written, and it does not prevent the design of a rough data framework. For example, the demand is not completely determined, and the UI kids shoes should be designed in advance. It's a big deal ~ HOHO ~ It sounds like a joke.

1.3 Multithreading
  • At the same time as a single-core CPU, the CPU can only process one thread, and only one thread is executing the task.

  • Simultaneous execution of multiple threads: in fact, the CPU switches between multiple threads quickly (scheduling tasks ).

  • If the speed of the CPU scheduling thread is fast enough, it causes multithreading.At the same timeExecutedIllusion

  • If there are many threads, the CPU will continuously schedule tasks among multiple threads. The result is that a large amount of CPU resources are consumed and the efficiency is reduced:

    • The scheduling frequency of each thread is reduced.
    • The thread execution efficiency will decrease

How many cores does the iPhone have?
A7: iPhone 5S, dual core
A8: iPhone 6, iPhone 6 Plus, dual-core
A9: iPhone 6 S, iPhone 6 S Plus, dual-core
A10: iPhone 7, iPhone 7 Plus, 2 + 2 cores

1.4 multithreading in iOS

As mentioned earlier, once the iOS App is running, a thread is enabled by default. This thread is usually calledMain thread"

Main thread:

If the main thread has too many operations and is too time-consuming, the App will become choppy. Therefore, we usually place time-consuming operations in sub-threads. After obtaining the results, we return to the main thread to refresh the UI.

2. Operation

 

Let's take a look at the basic usage:

1 // use Operation 2 private func basicOperation () {3 // Step 1: Create Operation 4 let op = Operation. init () 5 // Step 2: Put the code to be executed into operation 6 op. completionBlock = {7 8 print (# function, # line, Thread. current) 9} 10 // Step 3: Create OperationQueue11 let opQueue = OperationQueue. init () 12 // Step 4: add Operation to the thread 13 opQueue. addOperation (op) 14}

 

Use BlockOperation to create operatoin and run it directly. Let's see which thread will execute.

1 // create a simple BlockOperation 2 private func CreatBasicBlockOperation () {3 // use BlockOperation to create operation 4 let operation = BlockOperation. init {5 // print to see in which Thread 6 7 print (# function, # line, Thread. current) 8} 9 10 // run operation directly to see which thread is running 11 operation. start () 12}

 

Print the running result:

This is what we say. After Operation is created, if it runs directly, it will be executed in the current thread. That is to say, if the main thread is created and started, it will be executed in the main thread; if it is created in the subthread and started, it will be executed in the subthread.

 

3. Basic Demo

In this example, the requirements are as follows:
1. Load the data of each image in the subthread
2. After the image data is downloaded, it is displayed.
3. When you start to request data, let the indicator start to rotate
4. After all the images are downloaded, the indicator stops rotating.

 

3. Basic Demo

In this example, the requirements are as follows:
1. Load the data of each image in the subthread
2. After the image data is downloaded, it is displayed.
3. When you start to request data, let the indicator start to rotate
4. After all the images are downloaded, the indicator stops rotating.

 

3.2 do catch in Swift

This is a difference between Swift and OC. There is an optional value in Swift, which is not the focus of this time. For more information about shoes, see :? And! Use

There are four methods in Swift to handle errors:

 

Because do catch is used in the Demo, let's just say do catch.
Do catch should be used in Swift's standard try.

The following is the general format of the do-catch statement. If the code in the do clause throws an error, it will be caught by the catch clause and determined which clause will handle the error.

1 do {2     try expression3     statements4 } catch pattern 1 {5     statements6 } catch pattern 2 where condition {7     statements8 }
3.3 Priority

There are two priorities in the Mind Map. One belongs to Operation and the other belongs to OperationQueue. Let's take a look at what both are.

3.3.1 priority in Operation

Operation is calledqualityOfService

1 public enum QualityOfService : Int {2     case userInteractive3     case userInitiated    4     case utility    5     case background    6     case `default`7 }
1 userInteractive: highest priority, used for user interaction event 2 userInitiated: Second high priority, used for the event that the user needs to execute immediately 3 utility: normal priority, used for general Task 4 background: lowest priority, used for unimportant tasks 5 default: default priority. This priority is set by default for both the main thread and the thread that does not set the priority.

 

3.3.2 priority in operationQueue

In operationQueue, the attribute indicating the priority isqueuePriorityIndicates the priority of the operation in the queue.

1 public enum QueuePriority : Int {2     case veryLow3     case low4     case normal5     case high6     case veryHigh7 }

These priorities are relative. They do not mean that they must be executed at the highest priority. There is no strict order. However, there is a tendency to allocate resources. If the queue needs to have a strict execution order, or add dependencies, this is what we will share in the next article.

4. Case studies

Operation basic applications and low priority cases.
After implementation, the effect is as follows:

 

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.