iOS development multithreading-Status of Threads

Source: Internet
Author: User

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.M 3//04-nsthread02-Thread Status 4//5//Created by Apple on 14-6-23. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (nonatomic,strong) Nsthread *thread;1 3 @end15 @implementation YYViewController17-(void) VIEWDIDLOAD19 {[Super viewdidload];21 22//Create line Cheng Self.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, turn on thread-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event29 {30 Open thread [Self.thread start];32}33-(void) Test35 {36//Get thread PNs nsthread *current=[nsthread CurrentThread] ; NSLog (@ "Test---print thread---%@", self.thread.name), NSLog (@ "Test---thread starts---%@", current.name); 40 41//Set thread blocking 1 , blocking 2 seconds NSLog (@ "Next, thread blocked for 2 seconds"); [Nsthread sleepfortimeinterval:2.0];44 45///second setting thread blocking 2, blocking 4 seconds at current time as benchmark 46     NSLog (@ "Next, thread blocked for 4 seconds"), NSDate *date=[nsdate datewithtimeintervalsincenow:4.0];48 [Nsthread Sleepuntildate:da te];49 for (int i=0; i<20; i++) {NSLog (@ "Thread--%d--%@", i,current.name);}53 NSLog (@ "Test---thread end---%@", current.name);}55 @end

Print View:

code example 2 (Exit thread):

 1//2//YYVIEWCONTROLLER.M 3//04-nsthread02-Thread Status 4//5//Created by Apple on 14-6-23. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (nonatomic,strong) Nsthread *thread;1 3 @end15 @implementation YYViewController17-(void) VIEWDIDLOAD19 {[Super viewdidload];21 22//Create line Cheng Self.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, turn on thread-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event29 {30 Open thread [Self.thread start];32}33-(void) Test35 {36//Get thread PNs nsthread *current=[nsthread CurrentThread] ; NSLog (@ "Test---print thread---%@", self.thread.name), NSLog (@ "Test---thread starts---%@", current.name); 40 41//Set thread blocking 1 , blocking 2 seconds NSLog (@ "Next, thread blocked for 2 seconds"); [Nsthread sleepfortimeinterval:2.0];44 45///second setting thread blocking 2, blocking 4 seconds at current time as benchmark 46     NSLog (@ "Next, thread blocked for 4 seconds"), NSDate *date=[nsdate datewithtimeintervalsincenow:4.0];48 [Nsthread Sleepuntildate:da             te];49 for (int i=0; i<20; i++) {NSLog (@ "Thread--%d--%@", i,current.name); if (5==i) {52  End thread [nsthread exit];54}55}57 NSLog (@ "Test---thread end---%@", current.name); 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-Status of Threads

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.