线程通信

来源:互联网 发布:我的巨星男佣网络剧 编辑:程序博客网 时间:2024/06/14 11:34
// 线程通信, 主-子, 子-主    // 线程之间的相互跳转    // 主线程 - > 子线程    // 子线程 - > 主线程    // 1.    dispatch_async(dispatch_get_main_queue(), ^{        // 回到主线程        // block 任务    });    // 2.    [self performSelectorOnMainThread:@selector(mainThreadAction) withObject:nil waitUntilDone:YES];    // 子线程 - > 子线程    // 创建一个线程对象    NSThread *createThread = [[NSThread alloc]init];    createThread.name = @"other";    // 进入子线程    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        [self performSelector:@selector(otherThreadAction) onThread:createThread withObject:nil waitUntilDone:YES];    });}-  (void) mainThreadAction {    NSLog(@"回到主线程");}// 验证已经跳转到 other 的子线程中的方法- (void)otherThreadAction {    NSLog(@"---%d",[NSThread isMainThread]);}
0 0