iOS线程其一NSThread
2016-04-15 09:26
477 查看
之前的文章,我简单分别介绍了四种线程。现在介绍其中一种的NSThread怎么用
//大多数方法默认都在主线程
这是一个触摸模拟器就会响应的方法
第一种《《《《《《《《《《《《
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建线程
//object参数
NSThread *thread = [[NSThread
alloc]initWithTarget:self
selector:@selector(run:)
object:@"jack"];
//启动线程
[thread start];
//线程一启动,就会在线程thread中执行self的run方法
thread.name = @"my - thread";
//是否在主线程
[thread isMainThread];
}
- (void)run:(NSString *)param{
NSLog(@"-------run------%@-----%@",param,[NSThread
currentThread]);
}
打印结果
//这个线程做完就会死掉
接下来,我们来证明线程是否会死掉
第一步.创建一个新的类.h.m
继承NSThread
在新建的XMGThread文件中写
#import "XMGThread.h"
@implementation XMGThread
- (void)dealloc{
NSLog(@"dealloc");
}
@end
第二步.在ViewController里引入头文件
#import "XMGThread.h"
把NSThread 全部改成 XMGThread 运行一下。打印出来的结果
说明线程走完会死掉
第二种《《《《《《《《《《《《
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self
createThread2];
}
- (void)createThread2{
//在主线程直接分出一条线程
创建新的线程
[NSThread
detachNewThreadSelector:@selector(run:)
toTarget:self
withObject:@"rose"];
}
第三种《《《《《《《《《《《《
//大多数方法默认都在主线程
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self
createThread3];
}
- (void)createThread3{
//开一个后台线程
子线程
[self
performSelectorInBackground:@selector(run:)
withObject:@"jack"];
}
//第二,第三种
不用alloc init一个线程
//优点简单快捷
//缺点
无法对线程更详细设置
//大多数方法默认都在主线程
这是一个触摸模拟器就会响应的方法
第一种《《《《《《《《《《《《
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建线程
//object参数
NSThread *thread = [[NSThread
alloc]initWithTarget:self
selector:@selector(run:)
object:@"jack"];
//启动线程
[thread start];
//线程一启动,就会在线程thread中执行self的run方法
thread.name = @"my - thread";
//是否在主线程
[thread isMainThread];
}
- (void)run:(NSString *)param{
NSLog(@"-------run------%@-----%@",param,[NSThread
currentThread]);
}
打印结果
//这个线程做完就会死掉
接下来,我们来证明线程是否会死掉
第一步.创建一个新的类.h.m
继承NSThread
在新建的XMGThread文件中写
#import "XMGThread.h"
@implementation XMGThread
- (void)dealloc{
NSLog(@"dealloc");
}
@end
第二步.在ViewController里引入头文件
#import "XMGThread.h"
把NSThread 全部改成 XMGThread 运行一下。打印出来的结果
说明线程走完会死掉
第二种《《《《《《《《《《《《
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self
createThread2];
}
- (void)createThread2{
//在主线程直接分出一条线程
创建新的线程
[NSThread
detachNewThreadSelector:@selector(run:)
toTarget:self
withObject:@"rose"];
}
第三种《《《《《《《《《《《《
//大多数方法默认都在主线程
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self
createThread3];
}
- (void)createThread3{
//开一个后台线程
子线程
[self
performSelectorInBackground:@selector(run:)
withObject:@"jack"];
}
//第二,第三种
不用alloc init一个线程
//优点简单快捷
//缺点
无法对线程更详细设置
相关文章推荐
- DBGridEh-在指示列显示复选框:gioShowRowselCheckboxesEh、dgMultiSelect
- IOS 开发中判断字符串是否为空字符的方法
- IOS应用程序多语言本地化解决方案
- iOS学习笔记27-摄像头
- iOS开发 ☞ YY_Model
- iOS学习笔记27-摄像头
- OpenCV for Ios 标记检测2
- 【代码笔记】iOS-看图听故事
- ios中三种随机数方法
- iOS之使用AFN进行序列化处理(5)
- iOS 开发 SMSSDK-免费短信获取的实现方法
- iOS GCD, 同步,异步,串行队列,并行队列,dispatch_group
- iOS开发总结之仿qq侧滑功能
- ios开发之Protocol
- iOS 9.3 无法真机测试 Could not find Developer Disk Image
- iOS 8后本地通知需要注意
- 选择蜂窝教育iOS培训 为梦想努力
- iOS中的本地化(设置中文,英文,阿拉伯文等等)
- iOS开发中Xcode编译错误 linker command failed with exit co
- iOS 震动与系统声音