One, Nsthread 1. Introduced
Thread objects in IOS, encapsulating a thread as an OC object, you can set the thread name, priority, and other properties
2. Common Methods
Two, example 1. Creating Threads
//1. Get the main threadNsthread * Mainthread =[Nsthread Mainthread]; NSLog (@"Main---%@", Mainthread); //2. Get the current thread (the main thread)Nsthread * CurrentThread =[Nsthread CurrentThread]; NSLog (@"Current ---%@", CurrentThread);//3. Mode one create thread[self createNSThread1]; //4. Mode two creating Threads//[self createNSThread2]; //5. Mode three creating Threads//[self createNSThread3];
1- (void) CreateNSThread1 {2 3 //1. Create a Thread object and specify how the thread runs4Nsthread * Newthread = [[Nsthread alloc] initwithtarget:self selector: @selector (runnsthread:)Object:@"To open a new thread manually"];5 6 //set the thread name7[Newthread SetName:@"Thread A"];8 9 //Thread is turned on, and the thread runs only when the thread is turned onTen [Newthread start]; One A }
1- (void) Runnsthread: (NSString *) str {2 3 //1. Get the current thread4Nsthread * CurrentThread =[Nsthread CurrentThread];5 6NSLog (@"Current ---%@", CurrentThread);7 8 //2. Time-Consuming execution9 for(inti =0; I <Ten; ++i) {Ten OneNSLog (@"%i---%@---%@", I, CurrentThread, str); A - } - the}
Run results
As you can see, a new Process object (81783) is created with the Nsthread InitWithTarget:selector:objecg: method, and the process is opened by start
1 -(void) createNSThread2 {23 // 1. Automatically create a new thread and turn it on, This method does not need to manually call the Start method to open the process and automatically turns on 4 [nsthread detachnewthreadselector: @selector (runnsthread:) Totarget:self withobject:@ " automatically open new thread 1"]; 5 6 }
The result is similar to the above, except that the newly created process ID and thread ID are not the same, and the diagram is no longer in place.
1 -(void) createNSThread3 {2 3 // 1. Automatically creates a new thread and opens 4 [self performselectorinbackground: @selector (runnsthread:) Withobject:@ " Automatically open new thread 1"]; 5 6 }
The result is similar to the above, except that the newly created process ID and thread ID are not the same, and the diagram is no longer in place.
2. Block the main thread
Drag a Uitextfield control and UIButton control into the xib and create a Ibaction action method UIButton the code as follows
1-(Ibaction) Btn1click: (ID) Sender {2 3 //1. Get the current thread4Nsthread * thread =[Nsthread CurrentThread];5 6 //2. Use for some delay operation7 for(inti =0; I <50000; ++i) {8 9NSLog (@"%i---%@", I, thread);Ten One } - +}
Running the program, you will see that the contents of the For statement are always output in the console, and clicking on the Uitextfield control is not able to eject the keyboard.
The reason is that there is only one thread at this time, the main thread, and the main thread is running serially, because the current for statement takes a lot of time to finish, so it is not possible to perform other tasks until the time-consuming task of the For statement is completed;
Think: If I < 10 of the for statement in the program that created the thread is changed to I < 50000, can I click Uitextfield to respond when I run the program?
As can be seen from the graph, the execution of the time-consuming task is a child thread, not the main thread, and the response control event is the main thread, when the main thread creates a child thread, the time-consuming task is given to it to execute, so the main thread can now respond to the control's events.
ios--Multithreading Nsthread