The state of the thread
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//89#import"YYViewController.h"1011@interfaceYyviewcontroller ()@property (nonatomic,strong) Nsthread *Thread1314@end1516@implementationYyviewcontroller1718-(void) Viewdidload19{20[Super Viewdidload];21st22//Creating ThreadsSelf.thread=[[nsthread alloc]initwithtarget:self selector: @selector (Test)Object: nil];24//Set the name of the thread[Self.thread SetName:@"Thread A"];26}27//When the finger is pressed, the thread is turned on28-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event29{30//Open Thread31[Self.thread start];32}3334-(void) test35{36//Get threadPanax Notoginseng Nsthread *current=[Nsthread CurrentThread];NSLog (@"Test---print thread---%@", self.thread.name);NSLog (@"Test---thread starts---%@", current.name);4041//Set thread to block 1, block for 2 secondsNSLog (@"Next, the thread blocks for 2 seconds");[Nsthread Sleepfortimeinterval:2.0];4445//The second set of threads blocks 2, blocking 4 seconds on the current time basisNSLog (@"Next, the thread blocks for 4 seconds");NSDate *date=[nsdate Datewithtimeintervalsincenow:4.0];48[Nsthread Sleepuntildate:date];49for (int i=0; i<< Span style= "color: #800080;" >20; I++@" thread--%d--%@52 }53 NSLog (@ "54 }55 56 @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//89#import"YYViewController.h"1011@interfaceYyviewcontroller ()@property (nonatomic,strong) Nsthread *Thread1314@end1516@implementationYyviewcontroller1718-(void) Viewdidload19{20[Super Viewdidload];21st22//Creating ThreadsSelf.thread=[[nsthread alloc]initwithtarget:self selector: @selector (Test)Object: nil];24//Set the name of the thread[Self.thread SetName:@"Thread A"];26}27//When the finger is pressed, the thread is turned on28-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event29{30//Open Thread31[Self.thread start];32}3334-(void) test35{36//Get threadPanax Notoginseng Nsthread *current=[Nsthread CurrentThread];NSLog (@"Test---print thread---%@", self.thread.name);NSLog (@"Test---thread starts---%@", current.name);4041//Set thread to block 1, block for 2 secondsNSLog (@"Next, the thread blocks for 2 seconds");[Nsthread Sleepfortimeinterval:2.0];4445//The second set of threads blocks 2, blocking 4 seconds on the current time basisNSLog (@"Next, the thread blocks for 4 seconds");NSDate *date=[nsdate Datewithtimeintervalsincenow:4.0];48[Nsthread Sleepuntildate:date];49for (int i=0; i<20; i++) {NSLog (@"Thread--%d--%@", i,current.name);51if (5==i) {52 // end thread 53 [Nsthread exit]; 54 }55 56 }57 NSLog (@ "test---thread ends---%@ "58 }59 60 @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.
iOS Development--Multithreading OC & (v) Multithreading status