iOS Development multithreading-multithreading Technology 1
Source: Internet
Author: User
<span id="Label3"></p>I. Basic Concepts 1. What is a process<p style="margin-left: 30px;"><p style="margin-left: 30px;">A process is an application that is <span style="color: #ff0000;">running</span> in the system</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Each application is independent of each <span style="color: #ff0000;">other</span></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Each process runs within its <span style="color: #ff0000;">proprietary and protected</span> memory Space.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p>2. What is a thread<p style="margin-left: 30px;"><p style="margin-left: 30px;">A process that wants to execute a program <span style="color: #ff0000;">must</span> need a thread,</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="color: #ff0000;">threads are the basic unit of program execution</span> , and all of the tasks that are applied are executed in the Thread.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">When the program is started, the system automatically creates a thread for the process, called <span style="color: #ff0000;">the "main</span>thread" "U i thread"</p></p><p lang="en-US"><p lang="en-US"></p></p><p lang="en-US"><p lang="en-US"></p></p>3. Thread's serial<p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="color: #ff0000;">a thread executes a task serially</span> , that is, within <span style="color: #ff0000;">a thread</span> , the execution of the task is performed sequentially from the</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">At the same time, the thread can only perform one task----> CPU can only execute one thread at a time</p></p><p><p></p></p>two, multi-threading 1. What is multithreading<p style="margin-left: 30px;"><p style="margin-left: 30px;">Multithreading refers to the creation of multiple threads in the same process, and each thread " <span style="color: #ff0000;">concurrently</span>" performs its own tasks.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Can effectively improve the efficiency of the system</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">As a result, when the CPU quickly switches three threads, it can produce concurrency effects</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p>2. The principle of multithreading<p style="margin-left: 30px;"><p style="margin-left: 30px;">At the same time, the CPU can only execute one thread, and only one thread is executing;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">When multithreading is concurrent, the CPU is quickly <span style="color: #ff0000;">dispatched</span> (switched) between multiple threads, which can produce <span style="color: #ff0000;">concurrent (simultaneous) execution</span> Phenomena.</p></p><p><p><span style="background-color: #ff0000;">note:</span> Concurrent execution is an illusion that appears to be performed concurrently.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Parallel is just, is true simultaneous Execution.</p></p>3. Advantages and disadvantages of multithreading<p style="margin-left: 30px;"><p style="margin-left: 30px;">Advantages:</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Can improve the execution efficiency of the program</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Can improve the utilization of system resources appropriately</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Disadvantages</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Open threads require a certain amount of memory space (default: the main thread 1M sub-thread 512B), If too many threads are turned on, it will consume too much memory space;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The more threads, the more consumption of CPU thread scheduling;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">The more threads, the fewer times each thread is dispatched in the same time;</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Programming is more complex, such as: communication between threads, data sharing between threads, etc.</p></p><p><p><span style="background-color: #ff0000;">note:</span> The thread created by the system is called the "main thread", and the other threads are called "child threads".</p></p>three, multi-threaded Application 1. Main thread<p style="margin-left: 30px;"><p style="margin-left: 30px;">The main thread: when the process is turned on, the system will start by default, which is called: "main thread" or "ui thread";</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Role:</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Display/refresh UI Interface</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Handle UI Interface (E.G. click, scroll, drag, etc.)</p></p>2. Considerations for the main thread<p style="margin-left: 30px;"><p style="margin-left: 30px;">For a better user experience, do not talk about long-time tasks in the main thread, otherwise it will affect the smoothness of the ui, causing the phenomenon of lag</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">For example:</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">If it takes a long time (10s), then the main thread will remain in the task until the task is completed and when 5s clicks, the User's Click event cannot be immediately answered.</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Put long-time tasks behind a child thread, enabling immediate user-clicked events</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p><p></p></p>four, iOS multi-threading Implementation scheme<p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="line-height: 1.5;"> </span></p></p>V. Brief introduction of Pthread and Nsthread<p><p></p></p>A brief introduction to the 1.pthread<p><p>The Created Function:</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">int pthread_create (pthread_t * __restrict, Const pthread_attr_t * __restrict,void * (*) (void *), void * __restrict);</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">pthread_t *restrict: address of the thread identifier</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Const pthread_attr_t *restrict: Setting the properties of a thread</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">void * (*) (void *): a function executed in a thread.</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">void *restrict: The argument passed to the execution Function. Can pass a parameter of any type</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The de void * in the C language is equivalent to the ID in OC</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Return Value: 0 on behalf of thread creation succeeded</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Returns the other numeric value that represents the thread creation Failure. the returned value is the failed state</p></p><p><p><span style="color: #000000; background-color: #ff0000;">Attention:</span></p></p><p><p>Under the Arc environment, C language and OC cannot be passed directly</p></p><p><p>__bridge bridging: C language does not have automatic memory management, need to add the C language code automatic memory operation, only in the ARC environment only need</p></p><p><p>OC: There is no need to manually add automatic memory management (retain/release/autorelease/) in the ARC environment</p></p><p><p>The purpose of adding is to tell the compiler to add automatic memory management code for the OC Code</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">To compile the environment settings for the File:</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Non-arc Mechanism-fno-objc-arc</p></p><p style="margin-left: 30px;" lang="en-US"><p style="margin-left: 30px;" lang="en-US">ARC:-fobjc-arc</p></p><p lang="en-US"><p lang="en-US"></p></p><p><p>Code:</p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">#import "ViewController.h" #import <pthread.h> @interface viewcontroller () @end @implementation viewcontroller-( Void) viewdidload{ [super viewdidload];} -(void) touchesbegan: (nsset*) touches withevent: (uievent*) event{ //create a thread identifier pthread_t myThread; nsstring* str = @ "pthread"; int result = Pthread_create (&mythread, NULL, longtimeoperation, str); NSLog (@ "%d", result); void* (longtimeoperation) (void* data) { NSLog (@ "----%@------%@", data, [nsthread currentthread]); Return NULL;} @end</pre></pre><p><p></p></p><span style="line-height: 1.5;"><span style="line-height: 1.5;">introduction of 2.NSThread</span></span>1> Thread creation and startup<p style="margin-left: 60px;"><p style="margin-left: 60px;">A nsthread represents this Object.</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Create a startup</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">Nsthread *thread = [[nsthread alloc] initwithtarget:self selector: @selector (run) object:nil];</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">[thread start];</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">When a thread starts, it tells the CPU that it is "ready" and executes the appropriate method when the CPU dispatches the Thread.</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Automatically start a thread after creating a thread</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">[nsthread detachnewthreadselector: @selector (run) totarget:self withobject:nil];</p></p><p lang="en-US"><p lang="en-US"></p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">Implicitly creating and starting a thread</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">[self performselectorinbackground: @selector (run) withobject:nil];</p></p><p lang="en-US"><p lang="en-US"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="background-color: #ff0000;">Tip:</span> The latter two ways to create the thread object cannot be set in more Detail.</p></p><p><p></p></p>Common methods of 2><p style="margin-left: 30px;"><p style="margin-left: 30px;">Main Thread-related:</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">+ (nsthread *) mainthread; Get the main thread</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">-(BOOL) ismainthread; is the main thread</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">+ (BOOL) ismainthread; is the main thread</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Get current Thread:</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">Nsthread *current = [nsthread currentthread];</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Scheduling Priority for Threads</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">+ (double) threadpriority;</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">+ (BOOL) setthreadpriority: (double) p;</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">-(double) threadpriority;</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">-(BOOL) setthreadpriority: (double) p;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The scheduling priority value range is 0.0 ~ 1.0, The default 0.5, the higher the value, the higher the priority</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">The name of the thread</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">-(void) setName: (nsstring *) n;</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">-(nsstring *) name;</p></p><p lang="en-US"><p lang="en-US"></p></p>Vi. Status of Threads<p><p></p></p>1, the state of the thread<p style="margin-left: 30px;"><p style="margin-left: 30px;">There are four states in a thread: ready, running, blocking, and Dying.</p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="background-color: #ff0000;">note:</span> When a thread enters a blocking state, it is removed from the scheduler thread pool</p></p><p><p></p></p>2. Thread state related methods<p style="margin-left: 30px;"><p style="margin-left: 30px;">Start thread (enter Ready State)</p></p><p style="margin-left: 60px;"><p style="margin-left: 60px;">-(void) start;</p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Blocking (pausing) a thread (entering blocking State)</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">+ (void) sleepuntildate: (nsdate *) date;</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">+ (void) sleepfortimeinterval: (nstimeinterval) ti;</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US"></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;">Force stop thread (enter death State)</p></p><p style="margin-left: 60px;" lang="en-US"><p style="margin-left: 60px;" lang="en-US">+ (void) exit;</p></p><p lang="en-US"><p lang="en-US"></p></p>seven, the thread lock 1, introduced: thread safety issues (multiple threads access the same piece of Data)<p lang="en-US"><p lang="en-US"></p></p><p><p>。。。。。 Not to be continued</p></p><p><p>iOS Development multithreading-multithreading Technology 1</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