Create multiple threads for iosGCD

Source: Internet
Author: User

Create multiple threads for iosGCD

Oh, try to split it into a section. This makes it hard for everyone to see clearly. I have three ways to create a thread. The Code is as follows:

 

 

# Import "ViewController. h"

 

@ Interface ViewController ()

 

@ End

 

@ Implementation ViewController

 

-(Void) viewDidLoad {

[Super viewDidLoad];

}

// When the user clicks the screen, the execution thread

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event

{

[Self testThread];

}

 

-(Void) testThread

{

 

// Obtain the current thread, which is the main thread.

NSThread * curThread = [NSThread currentThread];

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

 

//// Method for obtaining the master thread

// NSThread * mainThread = [NSThread mainThread];

// NSLog (@ "mainThread = % @", mainThread );

//

 

[Self createThread1];

 

}

 

 

-(Void) createThread1

{

// An NSThread object is a thread

// Parameter 1 and 2: Specify parameter 1 in the thread to call the method of parameter 2

// Parameter 3: pass real parameters to the method specified by parameter 2

NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (threadMain :) object: @ "thread creation method 1"];

 

[Thread setName: @ "My name is egg"];

[Thread start];

}

-(Void) createThread2

{

 

[NSThread detachNewThreadSelector: @ selector (threadMain :) toTarget: self withObject: @ "thread creation method 2"];

 

 

}

-(Void) createThread3

{

 

 

[Self defined mselectorinbackground: @ selector (threadMain :) withObject: @ "thread creation method 3"];

}

-(Void) threadMain :( id) obj

{

For (int I = 0; I <1000; I ++ ){

NSLog (@ "I = % d, obj = % @, thread = % @", I, obj, [NSThread currentThread]);

}

 

 

}


Of course, you still need to use more features, just like eating more food. You can pick up the thorns and continue the next article. Haha

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.