线程之间的通信

<p>线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信</p><p> </p><p>线程间通信的体现</p><p>1个线程传递数据给另1个线程</p><p>在1个线程中执行完特定任务后,转到另1个线程继续执行任务</p>
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    SEL sel[3] = {
      @selector(nsthread),
        @selector(gcd),
        @selector(operationQueue)
    };
    
    for (NSInteger i = 0; i < 3; i++) {
        NSThread *th = [[NSThread alloc]initWithTarget:self selector:sel[i] object:nil];
        [th start];
    }

}
#pragma mark -线程通信(主线程)-
-(void)nsthread{
    //NSThread 获取主线程的方法
    [self performSelectorOnMainThread:@selector(finish) withObject:self waitUntilDone:NO];
    
    //下面这条语句还是子线程 !!!!
     NSLog(@"nsthread%@",[NSThread currentThread]);
}
-(void)gcd
{
    //GCD获取主线程的方法
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"gcd%@",[NSThread currentThread]);
        [self finish];
    });
}
-(void)operationQueue{
    //NSOperationQueue 获取主队列
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [mainQueue addOperationWithBlock:^{
        NSLog(@"mainQueue%@",[NSThread currentThread]);
        [self finish];
    }];
}

-(void)finish{
    NSLog(@"完成!!!!!!");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(线程,通信,任务)