zoukankan      html  css  js  c++  java
  • 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];

    
    
  • 相关阅读:
    POJ 3252 Round Numbers
    HDU 1024 Max Sum Plus
    HDU 1024 Max Sum Plus Plus
    HDU 1698 Just a Hook
    HDU 1049 Climbing Worm
    HDU 3386 Reversi
    树状数组总结
    HDU 1556 Color the ball
    树形数组
    HDU 1188 敌兵布阵
  • 原文地址:https://www.cnblogs.com/huadeng/p/7011392.html
Copyright © 2011-2022 走看看