多线程的四种创建方式--前两种不常用
2015-09-13 21:10
381 查看
- (void)viewDidLoad {
[super viewDidLoad];
//1.Datach 方式,隐式创建
// [NSThread detachNewThreadSelector:@selector(detachMethod) toTarget:self withObject:nil];
//2.NSObject的perform方法,隐式创建
// [self performSelectorInBackground:@selector(detachMethod) withObject:nil];
//3.显示创建
// NSThread *thread =[[NSThread alloc]initWithTarget:self selector:@selector(detachMethod) object:nil];
//给线程name赋值
// thread.name =@"thread_name";
//在线程创建之前设置栈空间才有效
// thread.stackSize =100;
//线程全局数据 readOnly
// [thread.threadDictionary setObject:@"value1" forKey:@"key1"];
//开启线程
// [thread start];
方法4需要子类化一个子类
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument{
if (self =[super initWithTarget:target selector:selector object:argument]) {
_target =target;
_sel =selector;
}
return self;
}
- (void)main{
if (_target) {
//父类的main方法中,_target 会掉用_sel
[_target performSelector:_sel withObject:nil];
}
}
//4.子类NSThread ,重写main方法
Thread *thread1 =[[Thread alloc]initWithTarget:self selector:@selector(detachMethod) object:nil];
[thread1 start];
}
- (void)detachMethod{
//是否是主线程
NSLog(@"%d",[NSThread isMainThread]);
//获取当前线程,打印name
NSLog(@"%@",[[NSThread currentThread]name]);
}
[super viewDidLoad];
//1.Datach 方式,隐式创建
// [NSThread detachNewThreadSelector:@selector(detachMethod) toTarget:self withObject:nil];
//2.NSObject的perform方法,隐式创建
// [self performSelectorInBackground:@selector(detachMethod) withObject:nil];
//3.显示创建
// NSThread *thread =[[NSThread alloc]initWithTarget:self selector:@selector(detachMethod) object:nil];
//给线程name赋值
// thread.name =@"thread_name";
//在线程创建之前设置栈空间才有效
// thread.stackSize =100;
//线程全局数据 readOnly
// [thread.threadDictionary setObject:@"value1" forKey:@"key1"];
//开启线程
// [thread start];
方法4需要子类化一个子类
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument{
if (self =[super initWithTarget:target selector:selector object:argument]) {
_target =target;
_sel =selector;
}
return self;
}
- (void)main{
if (_target) {
//父类的main方法中,_target 会掉用_sel
[_target performSelector:_sel withObject:nil];
}
}
//4.子类NSThread ,重写main方法
Thread *thread1 =[[Thread alloc]initWithTarget:self selector:@selector(detachMethod) object:nil];
[thread1 start];
}
- (void)detachMethod{
//是否是主线程
NSLog(@"%d",[NSThread isMainThread]);
//获取当前线程,打印name
NSLog(@"%@",[[NSThread currentThread]name]);
}
相关文章推荐
- ORA-00257: archiver error. Connect internal only, until freed
- 《哈利波特》名词
- 通过Socket实现TCP编程
- Same Tree
- XTML基础的相关笔记
- 代理模式
- 近期对Java的理解
- 03元祖
- 解决“磁盘空间不足 磁盘上总计 /boot 的空间”问题
- 每日一“结”(6)
- 深入理解Java的接口和抽象类
- poj2217Secretary(后缀数组+两串最长公共子串(可以重叠))
- Next Permutation
- Java面向对象问题汇总
- RabbitMQ安装简单过程
- 防止同一图片重复下载
- 1100. Mars Numbers (20)
- Java内存模型与延迟实例化-关于DCL
- 酷炫的react.js
- 1100. Mars Numbers (20)