iOS--线程的创建

1.获取当前线程

     NSThread *current=[NSThread currentThread]; 

2.
获取主线程的另外一种方式
     NSThread *main=[NSThread mainThread]; 
3.创建一个新的线程
(1)直接创建
     NSThread  *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程A"];
     //为线程设置一个名称
     thread.name=@"线程A";
      //开启线程
     [thread start];

(2)创建完线程自动启动

 //    NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"];
     [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"];

(3)隐式创建

      //在后台线程中执行===在子线程中执行
     [self performSelectorInBackground:@selector(run:) withObject:@"隐式创建"];

 

4.线程的五种状态:新建、就绪、运行、阻塞、死亡。

 阻塞:一个线程在执行过程中暂停,以等待某个条件的触发。

(1)设置线程阻塞1,阻塞2秒

    [NSThread sleepForTimeInterval:2.0];

   

(2)第二种设置线程阻塞2,以当前时间为基准阻塞4秒

    NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0];

    [NSThread sleepUntilDate:date];

 

线程死亡:    [NSThread exit];


转载于:https://www.cnblogs.com/huadeng/p/7011392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值