<span id="Label3"></p><p align="left"><p align="left">Process: a process is an application running in a system that is independent of each process and that each process runs within its dedicated and protected memory Space.</p></p><p align="left"><p align="left">Threads: a process that wants to perform a task must have a thread (at least 1 threads per 1 processes) threads are the basic unit of execution for a process, and all tasks of a process (program) are executed in a thread, such as playing music with a cool dog, downloading a movie using thunder, all of which need to be executed in the THREAD. The execution of a task in 1 threads is serial, and if you want to perform multiple tasks in 1 threads, you can only perform these tasks sequentially, that is, at the same time, 1 threads can perform only one task, such as downloading 3 files in 1 threads (file a, file b, file c, respectively).</p></p><p align="left"><p align="left">Multithreading: multiple threads can be opened in a process, each thread can perform different tasks in parallel (at the same time), process---workshop, thread--workshop workers, Multithreading technology can improve the execution efficiency of the program, such as the simultaneous opening of 3 threads to download 3 files respectively (file a, file b, File c).</p></p><p align="left"><p align="left">The principle of multithreading</p></p><p align="left"><p align="left">At the same time, the CPU can only handle 1 threads, and only 1 threads are working (executing)? multithreading concurrency (at the same time) executes, in fact, the CPU is quickly dispatched between multiple threads (switching)? if the CPU schedules threads Fast enough, It creates the illusion of multithreaded concurrency Execution. think: If the threads are very, very much, What's going to happen?? CPU will be dispatched between N multithreading, the CPU will be exhausted, consumes a lot of CPU resources? the frequency at which each thread is scheduled to execute is reduced (the execution efficiency of the thread is Reduced)</p></p><p align="left"><p align="left">Same: multi-threaded processing at the same time</p></p><p align="left"><p align="left"><strong>Nsthread</strong></p></p><p align="left"><p align="left">Pros: a more lightweight disadvantage than nsoperation GCD: you need to manually manage the cycle of threads and thread synchronization yourself.</p></p><p align="left"><p align="left">Created in two ways:</p></p><p align="left"><p align="left">[nsthread detachnewthreadselector: @selector (updateanimation) totarget:self withobject:nil];</p></p> <ol> <ol> <li>nsthread* myThread = [[nsthread alloc] initwithtarget:self selector: @selector (dosomething:) object:nil];</li> <li>[myThread start];</li> </ol> </ol><p align="left"><p align="left">Methods that do not explicitly create threads:</p></p><p align="left"><p align="left">Using the NSObject class method performselectorinbackground:withobject: Create a thread:? [OBJ performselectorinbackground: @selector (dosomething) withobject:nil];</p></p><p align="left"><p align="left">Back to main thread</p></p><p align="left"><p align="left">[self performselectoronmainthread: @selector (updateUI:) withobject:image waituntildone:yes];</p></p><p align="left"><p align="left"><strong>nsoperation</strong></p></p><p align="left"><p align="left">Pros: no need to care about thread management, data synchronization, You can focus on what you need to do.</p></p><p align="left"><p align="left">Cocoa operation related classes are nsoperation, Nsoperationqueue. Nsoperation is an abstract class that uses its subclasses to implement it or to use the two subclasses it defines: nsinvocationoperation (the operand is executed by default in the main thread, and the new thread is opened only if it is added to the Queue.) ) and Nsblockoperation (the operation is performed asynchronously if the operand of the nsblockoperation package is greater than 1). Creates an object of the Nsoperation subclass and adds the object to the Nsoperationqueue queue for Execution.</p></p><p align="left"><p align="left">One is to use two defined Subclasses:</p></p><p align="left"><p align="left">Nsinvocationoperation and Nsblockoperation.</p></p><p align="left"><p align="left">The other is to inherit Nsoperation</p></p><p align="left"><p align="left">If you are familiar with java,nsoperation, it is similar to the java.lang.Runnable interface. Like Java's runnable, Nsoperation is also designed to be extended by simply inheriting one of the methods of overriding Nsoperation Main. Equivalent to the Runnalbe run method in Java. Then put the object of the Nsoperation subclass into the Nsoperationqueue queue, and the queue will start and process it.</p></p><p align="left"><p align="left">Inherit Nsoperation</p></p><p align="left"><p align="left">Implement the Main method in The. m file, and the Main method writes the code to Execute.</p></p><p align="left"><p align="left">Many nsoperation can be added to the queue, and Nsoperationqueue can be thought of as a thread pool that adds operations (nsoperation) to the Queue. Threads in a thread pool can be considered consumers, taken from a queue, and Executed.</p></p><p align="left"><p align="left">Set by the following code:? [queue setmaxconcurrentoperationcount:5];? The number of threads in the thread pool, which is the number of concurrent Operations. By default, -1,-1 represents no limit, which runs all the operations in the queue at the same time.</p></p><p align="left"><p align="left"><strong>GCD</strong></p></p><p align="left"><p align="left">Grand Central Dispatch (GCD) is a solution for multi-core programming developed by Apple. Can only be used after the iOS4.0 has Started. GCD is an efficient and powerful alternative to technologies such as nsthread, nsoperationqueue, and Nsinvocationoperation.</p></p><p align="left"><p align="left">The Grand Central Dispatch abbreviation (GCD) is the technology developed by Apple to optimize applications that support multi-core processors and other symmetric multi-processing Systems. This is based on the thread pool pattern that the task executes in Parallel. It was first released on Mac OS X 10.6, and IOS 4 and above are also available.</p></p><p align="left"><p align="left">GCD works by allowing programs to queue in parallel to specific tasks, scheduling them to perform tasks on any available processor core, depending on the available processing Resources.</p></p><p align="left"><p align="left">A task can be a function or a block. The bottom of the GCD is still threaded, but this allows the programmer to focus on the details of the Implementation.</p></p><p align="left"><p align="left">The FIFO queue in GCD is called the dispatch queue, which guarantees that advanced tasks are executed First.</p></p><p align="left"><p align="left">Programming with gcd, developers simply define the tasks they want to perform and append them to the appropriate dispatch Queue. Where "define the task you want to perform" is done using block syntax. The tasks in the Dispatch queue are processed in FIFO (first in, first Out) order, which is the first task to be Entered. In addition, the Dispatch queue is divided into two types: serial and parallel.</p></p><p align="left"><p align="left">Serial VS Concurrent</p></p><p align="left"><p align="left">Serial Dispatch queue requires waiting for the executing task to complete before executing the next one, which means that only a thread is created to perform the task</p></p><p align="left"><p align="left">The tasks behind the Concurrent Dispatch queue can begin execution without waiting for the executing task to complete, which means that multiple tasks can be executed at the same time, that is, multiple threads are created concurrently to perform multiple tasks</p></p><p align="left"><p align="left">In general, use the serial Dispatch Queue if you require that you cannot change the order of execution or do not want to perform multiple tasks in Parallel.</p></p><p align="left"><p align="left">In short: Serial Dispatch queue serial code; Concurrent Dispatch queue parallel Code.</p></p><p align="left"><p align="left">Main Dispatch Queue & Global Dispatch queue</p></p><p align="left"><p align="left">The system provides us with two scheduling queues they don't need us to create just to get it when we use it</p></p><p align="left"><p align="left">Dispatch_sync the synchronous add operation, which waits for the operation to complete before continuing.</p></p><p align="left"><p align="left">Dispatch_async is added asynchronously to the task queue and it does not do any waiting</p></p><p><p>Synchronous-commit tasks should be noted: the queue that calls this function and the target queue for the Synchronous-commit task are the current queue sessions that cause deadlocks</p></p><p><p>iOS multithreading (GCD nsoperation nsthread)</p></p></span>
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