iOS development multithreading-Status of Threads
First, Brief introduction
creation of Threads:
Self.thread=[[nsthread alloc]initwithtarget:self selector: @selector (test) Object:nil];
Description: There are several ways to create threads, and there are not too many introductions here.
Thread- Open :
[Self.thread start];
running and blocking of threads:
(1) Set thread Block 1, block 2 seconds
[Nsthread sleepfortimeinterval:2.0];
(2) The second set of thread Block 2, the current time as a benchmark to block 4 seconds
NSDate *date=[nsdate datewithtimeintervalsincenow:4.0];
[Nsthread Sleepuntildate:date];
Performance in memory when a thread handles a blocking state: (Threads are moved out of the scheduler thread pool, not scheduled at this time)
Thread of death :
When a thread's task ends, an exception occurs, or a forced exit of these three cases causes the thread to die.
After the thread dies, the thread object is removed from memory.
Second, code example
code Example 1:
1 //2 //YYVIEWCONTROLLER.M3 //04-nsthread02-The state of a thread4 //5 //Created by Apple on 14-6-23.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten One @interfaceYyviewcontroller () A@property (nonatomic,strong) Nsthread *thread; - - @end the - @implementationYyviewcontroller - -- (void) Viewdidload + { - [Super Viewdidload]; + A //Creating Threads atSelf.thread=[[nsthread alloc]initwithtarget:self selector: @selector (Test)Object: nil]; - //set the name of the thread -[Self.thread SetName:@"Thread A"]; - } - //when the finger is pressed, the thread is turned on --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event in { - //Open Thread to [Self.thread start]; + } - the-(void) Test * { $ //Get ThreadPanax NotoginsengNsthread *current=[Nsthread CurrentThread]; -NSLog (@"Test---Print thread---%@", self.thread.name); theNSLog (@"Test---Thread starts---%@", current.name); + A //set thread to block 1, block for 2 seconds theNSLog (@"Next, the thread blocks for 2 seconds"); +[Nsthread Sleepfortimeinterval:2.0]; - $ //The second set of threads blocks 2, blocking 4 seconds on the current time basis $NSLog (@"Next, the thread blocks for 4 seconds"); -NSDate *date=[nsdate Datewithtimeintervalsincenow:4.0]; - [Nsthread sleepuntildate:date]; the for(intI=0; i< -; i++) { -NSLog (@"Thread--%d--%@", i,current.name);Wuyi the } -NSLog (@"Test---thread end---%@", current.name); Wu } - About @end
Print View:
code example 2 (Exit thread):
1 //2 //YYVIEWCONTROLLER.M3 //04-nsthread02-The state of a thread4 //5 //Created by Apple on 14-6-23.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten One @interfaceYyviewcontroller () A@property (nonatomic,strong) Nsthread *thread; - - @end the - @implementationYyviewcontroller - -- (void) Viewdidload + { - [Super Viewdidload]; + A //Creating Threads atSelf.thread=[[nsthread alloc]initwithtarget:self selector: @selector (Test)Object: nil]; - //set the name of the thread -[Self.thread SetName:@"Thread A"]; - } - //when the finger is pressed, the thread is turned on --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event in { - //Open Thread to [Self.thread start]; + } - the-(void) Test * { $ //Get ThreadPanax NotoginsengNsthread *current=[Nsthread CurrentThread]; -NSLog (@"Test---Print thread---%@", self.thread.name); theNSLog (@"Test---Thread starts---%@", current.name); + A //set thread to block 1, block for 2 seconds theNSLog (@"Next, the thread blocks for 2 seconds"); +[Nsthread Sleepfortimeinterval:2.0]; - $ //The second set of threads blocks 2, blocking 4 seconds on the current time basis $NSLog (@"Next, the thread blocks for 4 seconds"); -NSDate *date=[nsdate Datewithtimeintervalsincenow:4.0]; - [Nsthread sleepuntildate:date]; the for(intI=0; i< -; i++) { -NSLog (@"Thread--%d--%@", i,current.name);Wuyi if(5==i) { the //End Thread - [Nsthread exit]; Wu } - About } $NSLog (@"Test---thread end---%@", current.name); - } - - @end
Print Example:
Note: the death of a person can not be resurrected, the thread dead can not be resurrected (re-open), if the thread after death, click on the screen again to try to re-open the threads, the program will hang.