设置线程队列的优先级无效

hejunbinlan GCD 编程 最后由 极客学院-anglexjq 于2015年06月01日回复

  • 1 回答
  • 3.2k 浏览

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

 NSOperationQueue *operationQueue2 = [[NSOperationQueue alloc] init];

    //设置线程池并发数

    operationQueue2.maxConcurrentOperationCount = 1;

    //线程的创建

    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread1:) object:@"第一个线程"];

    //设置线程的优先级

    [operation1 setQueuePriority:NSOperationQueuePriorityVeryLow];

  

    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread2:) object:@"第二个线程"];

    //设置线程的优先级

    [operation2 setQueuePriority:NSOperationQueuePriorityVeryHigh];

    

    //将创建的线程添加到线程池中

    [operationQueue2 addOperation:operation1];

    [operationQueue2 addOperation:operation2];

}

- (void)thread1:(NSString *)data {

    NSLog(@"%@ is called! %@",NSStringFromSelector(_cmd),data);

}

- (void)thread2:(NSString *)data {

    NSLog(@"%@ is called! %@",NSStringFromSelector(_cmd),data);

}

为何执行时没有先执行operation2,而是先执行了operation1

多次运行程序都是先打印出thread1的内容,再打印出thread2的内容。