码迷,mamicode.com
首页 > 编程语言 > 详细

多线程的四种创建方式--前两种不常用

时间:2015-09-13 21:38:26      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

- (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]);

 

}

 

多线程的四种创建方式--前两种不常用

标签:

原文地址:http://www.cnblogs.com/yxt9322yxt/p/4805623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!