iOS Development multithreaded article-Creating threads

Source: Internet
Author: User

iOS Development multithreaded article-Creating threads

I. Simple instructions for creating and starting a thread

A Nsthread object represents a thread

Creating, starting Threads

(1) nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];

[Thread start];

When a thread is started, the run method of self is executed in threads thread

Main thread related usage

+ (Nsthread *) Mainthread; Get the main thread

-(BOOL) Ismainthread; is the main thread

+ (BOOL) Ismainthread; is the main thread

Other usage

Get current thread

Nsthread *current = [Nsthread CurrentThread];

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

+ (double) threadpriority;

+ (BOOL) SetThreadPriority: (double) p;

Set the name of the thread

-(void) SetName: (NSString *) n;

-(NSString *) name;

Other ways to create threads

(2) automatically start thread after creating thread [Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];

(3) Create and start the thread implicitly [self Performselectorinbackground: @selector (Run) Withobject:nil];

Pros and cons of the above 2 ways to create threads

Advantages: Simple and fast

Cons: Unable to set the thread in more detail

Second, code example

1. Create it in an ancient way

 1//2//YYVIEWCONTROLLER.M 3//4//5//Created by Apple on 14-6-23.6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import <pthread.h>12 @interface Yyviewcontroller (ibaction ) btnclick;16 @end17 @implementation YYViewController20-(void) viewDidLoad23 {[Super viewdidload];25 }26 27 28//button click event (ibaction) Btnclick {30//1. Get current thread Nsthread *current=[nsthread currentthread];32//Master   Thread NSLog (@ "Btnclick----%@", current); 34 35//2. Use the For loop to perform some time-consuming operations. pthread_t thread;37 pthread_create (&thread, NULL, run, NULL);}39 all//c language function *run void (void *data) 43 {44//Gets the current thread, 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 55 @end57 (+) 

iOS Development multithreaded article-Creating threads

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.