欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS多線程開發(fā)之線程的狀態(tài)

 更新時(shí)間:2015年12月18日 14:33:41   作者:187n  
這篇文章主要介紹了IOS多線程開發(fā)之線程的狀態(tài) 的相關(guān)資料,需要的朋友可以參考下

大家都知道,在開發(fā)過程中應(yīng)該盡可能減少用戶等待時(shí)間,讓程序盡可能快的完成運(yùn)算??墒菬o論是哪種語言開發(fā)的程序最終往往轉(zhuǎn)換成匯編語言進(jìn)而解釋成機(jī)器碼來執(zhí)行。但是機(jī)器碼是按順序執(zhí)行的,一個(gè)復(fù)雜的多步操作只能一步步按順序逐個(gè)執(zhí)行。改變這種狀況可以從兩個(gè)角度出發(fā):對(duì)于單核處理器,可以將多個(gè)步驟放到不同的線程,這樣一來用戶完成UI操作后其他后續(xù)任務(wù)在其他線程中,當(dāng)CPU空閑時(shí)會(huì)繼續(xù)執(zhí)行,而此時(shí)對(duì)于用戶而言可以繼續(xù)進(jìn)行其他操作;對(duì)于多核處理器,如果用戶在UI線程中完成某個(gè)操作之后,其他后續(xù)操作在別的線程中繼續(xù)執(zhí)行,用戶同樣可以繼續(xù)進(jìn)行其他UI操作,與此同時(shí)前一個(gè)操作的后續(xù)任務(wù)可以分散到多個(gè)空閑CPU中繼續(xù)執(zhí)行(當(dāng)然具體調(diào)度順序要根據(jù)程序設(shè)計(jì)而定),及解決了線程阻塞又提高了運(yùn)行效率。蘋果從iPad2 開始使用雙核A5處理器(iPhone中從iPhone 4S開始使用),A7中還加入了協(xié)處理器,如何充分發(fā)揮這些處理器的性能確實(shí)值得思考。今天將重點(diǎn)分析iOS多線程開發(fā):

一、簡(jiǎn)單介紹

線程的創(chuàng)建:

復(fù)制代碼 代碼如下:

self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];

說明:創(chuàng)建線程有多種方式,這里不做過多的介紹。

線程的開啟:

復(fù)制代碼 代碼如下:

[self.thread start];

線程的運(yùn)行和阻塞:

(1)設(shè)置線程阻塞1,阻塞2秒

[NSThread sleepForTimeInterval:2.0];

(2)第二種設(shè)置線程阻塞2,以當(dāng)前時(shí)間為基準(zhǔn)阻塞4秒

復(fù)制代碼 代碼如下:

NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0];
[NSThread sleepUntilDate:date];

線程處理阻塞狀態(tài)時(shí)在內(nèi)存中的表現(xiàn)情況:(線程被移出可調(diào)度線程池,此時(shí)不可調(diào)度)


線程的死亡:

當(dāng)線程的任務(wù)結(jié)束,發(fā)生異常,或者是強(qiáng)制退出這三種情況會(huì)導(dǎo)致線程的死亡。


線程死亡后,線程對(duì)象從內(nèi)存中移除。

二、代碼示例

代碼示例1:

//
// YYViewController.m
// -NSThread-線程的狀態(tài)
//
// Created by apple on --.
// Copyright (c) 年 itcase. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
@property(nonatomic,strong)NSThread *thread;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建線程
self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
//設(shè)置線程的名稱
[self.thread setName:@"線程A"];
}
//當(dāng)手指按下的時(shí)候,開啟線程
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//開啟線程
[self.thread start];
}
-(void)test
{
//獲取線程
NSThread *current=[NSThread currentThread];
NSLog(@"test---打印線程---%@",self.thread.name);
NSLog(@"test---線程開始---%@",current.name);
//設(shè)置線程阻塞,阻塞秒
NSLog(@"接下來,線程阻塞秒");
[NSThread sleepForTimeInterval:.];
//第二種設(shè)置線程阻塞,以當(dāng)前時(shí)間為基準(zhǔn)阻塞秒
NSLog(@"接下來,線程阻塞秒");
NSDate *date=[NSDate dateWithTimeIntervalSinceNow:.];
[NSThread sleepUntilDate:date];
for (int i=; i<; i++) {
NSLog(@"線程--%d--%@",i,current.name);
}
NSLog(@"test---線程結(jié)束---%@",current.name);
}
@end 

打印查看:


代碼示例2(退出線程):

//
// YYViewController.m
// -NSThread-線程的狀態(tài)
//
// Created by apple on --.
// Copyright (c) 年 itcase. All rights reserved.
//
#import "YYViewController.h"
@interface YYViewController ()
@property(nonatomic,strong)NSThread *thread;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建線程
self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
//設(shè)置線程的名稱
[self.thread setName:@"線程A"];
}
//當(dāng)手指按下的時(shí)候,開啟線程
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//開啟線程
[self.thread start];
}
-(void)test
{
//獲取線程
NSThread *current=[NSThread currentThread];
NSLog(@"test---打印線程---%@",self.thread.name);
NSLog(@"test---線程開始---%@",current.name);
//設(shè)置線程阻塞,阻塞秒
NSLog(@"接下來,線程阻塞秒");
[NSThread sleepForTimeInterval:.];
//第二種設(shè)置線程阻塞,以當(dāng)前時(shí)間為基準(zhǔn)阻塞秒
NSLog(@"接下來,線程阻塞秒");
NSDate *date=[NSDate dateWithTimeIntervalSinceNow:.];
[NSThread sleepUntilDate:date];
for (int i=; i<; i++) {
NSLog(@"線程--%d--%@",i,current.name);
if (==i) {
//結(jié)束線程
[NSThread exit];
}
}
NSLog(@"test---線程結(jié)束---%@",current.name);
}
@end 

打印示例:


注意:人死不能復(fù)生,線程死了也不能復(fù)生(重新開啟),如果在線程死亡之后,再次點(diǎn)擊屏幕嘗試重新開啟線程,則程序會(huì)掛。

以上內(nèi)容是小編給大家介紹的IOS多線程開發(fā)之線程的狀態(tài) ,希望大家喜歡。

相關(guān)文章

最新評(píng)論