iOS----------多线程

多线程主要分为三种:NSThread、NSoperationQueue、GCD多线程

NSThread 相比其余两个比较轻量级,建立线程简单,可是须要咱们本身管理该线程,操做线程比较麻烦。不仅是启动,还有该线程使 用完毕后的资源回收;线程

  • 第一种:先建立线程,再启动线程
  •      NSThread * thread = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];资源

  •     thread.name = @"子线程";//能够为开辟的线程起名字get

  •  

     

  •  

        [thread start];//线程开始it

  •     [thread cancel];//线程取消io

     

  • 第二种:
  •  NSThread * thread = [[NSThread alloc]initWithBlock:^{
    •         NSLog(@"initWithBlock");thread

              [self run];object

          }]; select

    • [thread start];opera

  • 第三种:建立线程后自动启动线程

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

相关文章
相关标签/搜索