• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
那一季&花开
博客园    首页    新随笔    联系   管理    订阅  订阅

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

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

 

}

 

posted @ 2015-09-13 21:10  那一季&花开  阅读(395)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3