iOS 多线程篇6—线程的状态
来源:互联网 发布:86编程键盘 编辑:程序博客网 时间:2024/06/06 08:44
一、简单介绍
1. 线程的创建:
self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
说明:创建线程有多种方式,这里不做过多的介绍。
2. 线程的开启:
[self.thread start];
3. 线程的运行和阻塞:
(1)设置线程阻塞1,阻塞2秒
[NSThread sleepForTimeInterval:2.0];
(2)第二种设置线程阻塞2,以当前时间为基准阻塞4秒
NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0]; [NSThread sleepUntilDate:date];
线程处理阻塞状态时在内存中的表现情况:(线程被移出可调度线程池,此时不可调度)
4.线程的死亡:
当线程的任务结束,发生异常,或者是强制退出这三种情况会导致线程的死亡。
线程死亡后,线程对象从内存中移除。
二、代码示例
代码示例1:
//// YYViewController.m// 04-NSThread02-线程的状态//// Created by apple on 14-6-23.// Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"@interface YYViewController ()@property(nonatomic,strong)NSThread *thread;@end@implementation YYViewController- (void)viewDidLoad{ [super viewDidLoad]; //创建线程 self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; //设置线程的名称 [self.thread setName:@"线程A"];}//当手指按下的时候,开启线程-(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); //设置线程阻塞1,阻塞2秒 NSLog(@"接下来,线程阻塞2秒"); [NSThread sleepForTimeInterval:2.0]; //第二种设置线程阻塞2,以当前时间为基准阻塞4秒 NSLog(@"接下来,线程阻塞4秒"); NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0]; [NSThread sleepUntilDate:date]; for (int i=0; i<20; i++) { NSLog(@"线程--%d--%@",i,current.name); } NSLog(@"test---线程结束---%@",current.name);}@end
打印查看:
代码示例2(退出线程):
//// YYViewController.m// 04-NSThread02-线程的状态//// Created by apple on 14-6-23.// Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"@interface YYViewController ()@property(nonatomic,strong)NSThread *thread;@end@implementation YYViewController- (void)viewDidLoad{ [super viewDidLoad]; //创建线程 self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; //设置线程的名称 [self.thread setName:@"线程A"];}//当手指按下的时候,开启线程-(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); //设置线程阻塞1,阻塞2秒 NSLog(@"接下来,线程阻塞2秒"); [NSThread sleepForTimeInterval:2.0]; //第二种设置线程阻塞2,以当前时间为基准阻塞4秒 NSLog(@"接下来,线程阻塞4秒"); NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0]; [NSThread sleepUntilDate:date]; for (int i=0; i<20; i++) { NSLog(@"线程--%d--%@",i,current.name); if (5==i) { //结束线程 [NSThread exit]; } } NSLog(@"test---线程结束---%@",current.name);}@end
打印示例:
注意:人死不能复生,线程死了也不能复生(重新开启),如果在线程死亡之后,再次点击屏幕尝试重新开启线程,则程序会挂。
0 0
- iOS 多线程篇6—线程的状态
- ios开发多线程篇——线程的状态
- iOS开发多线程篇—线程的状态
- iOS开发多线程篇—线程的状态
- iOS开发多线程篇—线程的状态
- iOS开发多线程篇—线程的状态
- iOS开发多线程篇—线程的状态
- iOS开发多线程篇—NSThread及线程的状态
- iOS开发多线程篇—线程的状态
- iOS开发多线程篇—线程的状态
- iOS多线程-------线程的状态
- iOS开发多线程—06线程的状态
- iOS开发多线程-线程的状态
- iOS多线程之线程状态
- 多线程:线程的状态
- 多线程篇4 线程状态的改变
- java多线程——线程的状态
- 多线程之——线程的状态
- 第十、十一周项目1:阅读程序,并写出运行结果(4)
- CSS选择器
- 最小的k个数
- 一口一口吃成胖子-JDK
- 第十、十一周项目1:阅读程序,并写出运行结果(5)
- iOS 多线程篇6—线程的状态
- ct04特性批量导入、特性长文本批量导入(BAPI_CHARACT_CREATE、BAPI_CHARACT_CHANGE、BAPI_CHARACT_ADDLONGTEXT)
- CodeForces 638 A Home Numbers(数学,水题)
- linux 连接windows 共享文件夹
- 【Bugly干货分享】“HTTPS”安全在哪里?
- java对数组、List、Map排序及常用方法
- jquery 插件 validate 的常用方法
- Win32游戏制作之---Bizzard
- jQuery基础