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