美文网首页
IOS:OC--多线程NSThread

IOS:OC--多线程NSThread

作者: 任任任任师艳 | 来源:发表于2017-06-13 17:11 被阅读0次

    ViewController.m

    import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    • (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      self.view.backgroundColor = [UIColor redColor];

    // //打印当前的线程
    // NSLog(@"current=%@",[NSThread currentThread]);
    // //打印主线程
    // NSLog(@"main=%@",[NSThread mainThread]);
    // //判断当前线程是否为主线程
    // NSLog(@"isMainThread=%@",[NSThread isMainThread]);

    pragma mark NSThread 手动开启线程

    //开启线程对象
    

    // NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(sayHi) object:nil];
    //线程休眠 放在开启之前
    // [NSThread sleepForTimeInterval:5];

    //开启线程
    

    // [thread start];
    //结束当前线程
    // [thread cancel];
    //直接退出当前线程
    // [NSThread exit];

    pragma mark ----自动开启子线程

    [NSThread detachNewThreadSelector:@selector(sayHi) toTarget:self withObject:nil];
    

    }

    -(void)sayHi{
    //自动释放池,
    @autoreleasepool {
    NSLog(@"IOS->current = %@ %@ %d",[NSThread currentThread],[NSThread mainThread],[NSThread isMainThread]);
    }

    //回到主线程,修改当前背景颜色
    //回到主线程的方法  方法选择器  是否等待子线程完成后进入主线程
    [self performSelectorOnMainThread:@selector(changeCloor) withObject:nil waitUntilDone:YES];
    

    }

    -(void)changeCloor{

    self.view.backgroundColor = [UIColor cyanColor];
    NSLog(@"current=%@   main=%@",[NSThread currentThread],[NSThread mainThread]);
    

    }

    相关文章

      网友评论

          本文标题:IOS:OC--多线程NSThread

          本文链接:https://www.haomeiwen.com/subject/oetjqxtx.html