OC 线程操作

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
//    GCD 开几条线程并不是我们可以控制的,而是系统来决定的,
    
//    [self asyncConcurrent];
//    [self asyncSerial];
//    [self syncConcurrent];
//    [self syncSerial];
//    [self test_get_global_queue];
//   [self syncMain];//主线程操作直接死锁

    [NSThread detachNewThreadSelector:@selector(syncMain) toTarget:self withObject:nil];//在子线程地调用,会直接执行,不会死锁
  
}


/**
 异步函数 + 并发队列  :会开启多条线程,队列中的任务是异步执行的(并发执行)(无序执行的)
 */
- (void)asyncConcurrent{
    
    //1.创建任务
    /*
     参数1:const char * _Nullable label  , C语言参数 字符串 ,标识符,为了区分队列的 推荐写法:反着写
     参数2:dispatch_queue_attr_t  _Nullable attr,队列类型
     DISPATCH_QUEUE_CONCURRENT 并发,
     DISPATCH_QUEUE_SERIAL 串行
     */
    dispatch_queue_t queue = dispatch_queue_create("com.520it.www", DISPATCH_QUEUE_CONCURRENT);
    
    //2  2.1封装任务 2.2添加到任务队列中
    /**
     dispatch_async(<#dispatch_queue_t  _Nonnull queue#>, <#^(void)block#>)
     参数1:dispatch_queue_t  _Nonnull queue 队列
     参数2:<#^(void)block#>  要执行的任务
     */
    dispatch_async(queue, ^{
        NSLog(@"打印一下111 --- %@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"打印一下222 --- %@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"打印一下333 --- %@", [NSThread currentThread]);
    });
}


/**
 异步函数 + 串行队列:可以开启线程,但是串行队列只会在同一个子线程执行
 */
- (void)asyncSerial{
    dispatch_queue_t queue = dispatch_queue_create("asyncSerial", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        NSLog(@"打印一下111 --- %@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"打印一下222 --- %@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"打印一下333 --- %@", [NSThread currentThread]);
    });
}


/**
 同步函数 + 并发队列  :不会会开启多条线程,不管后面是并发还是串行,任务是串行执行的
 */
- (void)syncConcurrent{
    
    //1.创建任务

    dispatch_queue_t queue = dispatch_queue_create("com.520it.www", DISPATCH_QUEUE_CONCURRENT);

    dispatch_sync(queue, ^{
        NSLog(@"打印一下111 --- %@", [NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"打印一下222 --- %@", [NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"打印一下333 --- %@", [NSThread currentThread]);
    });
}

/**
 同步函数 + 串行队列  :不会会开启多条线程,不管后面是并发还是串行,任务是串行执行的
 */
- (void)syncSerial{
    dispatch_queue_t queue = dispatch_queue_create("syncSerial", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue, ^{
        NSLog(@"打印一下111 --- %@", [NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"打印一下222 --- %@", [NSThread currentThread]);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"打印一下333 --- %@", [NSThread currentThread]);
    });
}



/**
 获得全局队列
 */
-(void)test_get_global_queue{
    
    /**
     获得全局并发队列 : 这个队列已经存在的, 我们只是获得
     参数1 : long identifier 优先级,
      DISPATCH_QUEUE_PRIORITY_HIGH 2
      DISPATCH_QUEUE_PRIORITY_DEFAULT 0
      DISPATCH_QUEUE_PRIORITY_LOW (-2)
      DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN    这是最低的优先级
     
     参数2 : unsigned long flags 给未来使用, 总是给个0
     */
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        NSLog(@"打印一下111 --- %@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"打印一下222 --- %@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"打印一下333 --- %@", [NSThread currentThread]);
    });
}

  

1.

2

 
3.

 
4.

5.

 
8. 

原文地址:https://www.cnblogs.com/qingzZ/p/9227965.html