IOSGCD multi-thread creation multithreading

Source: Internet
Author: User

Oh, try to break it down into a section of the writing. It's hard for everyone to see clearly. I have three ways to create threads here. The code is as follows


#import "ViewController.h"


@interface viewcontroller ()


@end


@implementation Viewcontroller


-(void) viewdidload {

[superviewdidload];

}

When the user taps the screen, the thread executes

-(void) Touchesbegan: ( Nsset *) touches withevent: (uievent *) Event

{

[self testthread];

}


-(void) testthread

{

// fetch to the current thread , Here is the main path

nsthread *curthread = [nsthreadcurrentthread ];

NSLog(@ "curthread =%@", curthread);

//    // method to fetch the main thread

Nsthread *mainthread = [Nsthread mainthread];

NSLog (@ "mainthread =%@", mainthread);

//

[selfcreateThread1];

}



-(void) createThread1

{

// a nsthread object is a thread

// parameter 1,2: specified in thread by parameter 1 method of calling parameter 2

// parameter 3: pass an argument to the method specified by parameter 2

nsthread *thread = [[nsthreadalloc] Initwithtarget:selfselector:@selector( ThreadMain:)object:@ " thread creation mode 1"];

[Thread setName:@ " My name is two eggs "];

[Thread start];

}

-(void) createThread2

{

[nsthreaddetachnewthreadselector:@selector (threadmain:)totarget:selfwithobject: @ " thread creation mode 2"];

}

-(void) createThread3

{

[selfperformselectorinbackground:@selector (threadmain:)withobject:@ " thread creation mode 3"];

}

-(void) ThreadMain: ( ID ) obj

{

for (int i =0; i<; i+ +) {

NSLog(@ "i =%d,obj =%@,thread =%@", i,obj,[nsthread currentthread]);

}

}


Of course, we still need to use more, like eating, eat more can be picky, continue the next article, this evening 3 more. Ha ha

IOSGCD multi-thread creation multithreading

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.