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

相關文章
相關標籤/搜索