iOS Development multithreaded article-creating threads

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>iOS Development multithreaded article-creating threads</strong></p></p><strong><strong>I. Simple instructions for creating and starting a thread</strong></strong><p><p>A Nsthread object represents a thread</p></p><p><p>creating, Starting Threads</p></p><p><p>(1) nsthread *thread = [[nsthread alloc] initwithtarget:self selector: @selector (run) object:nil];</p></p><p><p>[thread start];</p></p><p><p>When a thread is started, the Run method of self is executed in threads thread</p></p><p><p></p></p><p><p>Main thread related usage</p></p><p><p>+ (nsthread *) mainthread; Get the main thread</p></p><p><p>-(BOOL) ismainthread; is the main thread</p></p><p><p>+ (BOOL) ismainthread; is the main thread</p></p><p><p>Other usage</p></p><p><p>Get current thread</p></p><p><p>Nsthread *current = [nsthread currentthread];</p></p><p><p></p></p><p><p>Scheduling priority for a thread: the value of the scheduling priority is 0.0 ~ 1.0, The default is 0.5, the higher the value, the greater the priority</p></p><p><p>+ (double) threadpriority;</p></p><p><p>+ (BOOL) setthreadpriority: (double) p;</p></p><p><p></p></p><p><p>Set the name of the thread</p></p><p><p>-(void) setName: (nsstring *) n;</p></p><p><p>-(nsstring *) name;</p></p><p><p></p></p><p class="p0"><p class="p0">Other ways to create threads</p></p><p class="p0"><p class="p0">(2) automatically start thread after creating thread [nsthread detachnewthreadselector: @selector (run) totarget:self withobject:nil];</p></p><p class="p0"><p class="p0">(3) Create and start the thread implicitly [self performselectorinbackground: @selector (run) withobject:nil];</p></p><p class="p0"><p class="p0">Pros and cons of the above 2 ways to create threads</p></p><p class="p0"><p class="p0">Advantages: Simple and fast</p></p><p class="p0"><p class="p0">Cons: Unable to set the thread in more detail</p></p><p><p>second, code Example</p></p><p><p>1. Create it in an ancient way</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>9 #import "YYViewController.h" #import <pthread.h>12 @interface yyviewcontroller ()-(ibaction) btnclic k;16 @end17 @implementation YYViewController20 -(void) viewDidLoad23 { [super viewdidload];25 }26 28//button Click Event-(ibaction) btnclick { //1. get current thread nsthread *current=[nsthread currentthread];32 //main Thread NSLog (@ "btnclick----%@", current); //2. use a For loop to perform some time-consuming operations. pthread_t thread;37 pthread_create (&thread, null, run, null); 38}39 40 //c *run (void *data) the current thread, which is the newly created thread nsthread *current=[nsthread currentthread];46 (int i=0; i<10000; i++) { NSLog (@ "btnclick---%d---%@", i,current), }51 return null;52}53 54//multiple threads, Click the button to execute the button call method, the main thread is not blocked @end57 58 </pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Implementation Results:</p></p><p><p></p></p><p class="p0"><p class="p0"></p></p><p class="p0"><p class="p0">Printing results:</p></p><p class="p0"><p class="p0"></p></p><p class="p0"><p class="p0">2. Create a thread using Nsthread</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 8 9 #import "YYViewController.h" #import <pthread.h> @interface yyviewcontroller ()-(ibaction ) btnclick; @end @implementation yyviewcontroller-(void) viewdidload {[super viewdidload]; 23} 24 25 26//button Click event (ibaction) btnclick {28//1. get current thread nsthread *current=[nsthread currentthread]; 30//master Thread NSLog (@ "btnclick----%@", current); 32 33//get The main thread another way Nsthread *main=[nsthread mainthread]; NSLog (@ "main thread-------%@", main); 36 37//2. perform some time-consuming operations [self creatnsthread]; //[self creatNSThread2]; //[self creatNSThread3]; /** * Nsthread Create thread mode 1 * 1> first create initialization thread * 2> start open thread */-(void) creatnsthread 50 { Wuyi nsthread *thread=[[nsthread alloc]initwithtarget:self selector: @selector (run:) object:@ "thread a"]; 52//set a name for the thread [email protected] "thread a"; 54//open Threads [thread start]; Nsthread *thread2=[[Nsthread alloc]initwithtarget:self selector: @selector (run:) object:@ "thread b"]; 59//set a name for the thread [email protected] "thread b"; 61//open Thread [thread2 start]; /** * Nsthread Create thread mode 2 68 * Direct (auto) start of thread creation (automatic) (void) creatNSThread2//nsthread *t Hread=[nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "create thread Direct (auto) start"]; [nsthread D Etachnewthreadselector: @selector (run:) totarget:self withobject:@ "create thread Direct (auto) start"]; /** * Nsthread Create thread mode 3 81 * Implicitly creates thread and directly (automatically) starts A. */-(void) creatNSThread3 85 {86//in Background thread = = = Executes in a child thread [self performselectorinbackground: @selector (run:) withobject:@ "implicitly created"]; the "the" (void) run :(nsstring *) str 93 {94//get Current thread Nsthread *current=[nsthread currentthread]; 96//print output for (int i=0; i<10; I++) {98 NSLog (@ "run---%@---%@", current,str),}100}101 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p class="p0"><p class="p0">Call Thread 1 and print the Result:</p></p><p class="p0"><p class="p0"></p></p><p class="p0"><p class="p0">Calling thread 2</p></p><p class="p0"><p class="p0"></p></p><p class="p0"><p class="p0">Calling thread 3</p></p><p class="p0"><p class="p0"></p></p><p><p>iOS Development multithreaded article-creating threads</p></p></span>
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.