线程之间的通信-(子线程获得主线程的方式)

/**
 ios 的线程库模型
    1.pthread(底层C)线程库 (一般的情况下不用)
    2.NSThread(OC线程库)
    3.NSOperationQueue(线程池,线程队列)
    4.GCD(Block 模式的线程)
 */
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

#pragma mark -线程之间的通信(主线程)-
/**
 *  主要讲解 主线程和子线程之间的通信,子线程如何获得子线程
 */


-(void)nsThread
{
    [self performSelectorOnMainThread:@selector(finished) withObject:self waitUntilDone:YES];
    
    //注意下面这句代码是由子线程来完成的 上面仅仅是获得主线程执行所调用的方法
    NSLog(@"nsThread %@",[NSThread currentThread]);
}

-(void)gcd
{
    //GCD 获取主线程的方法
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"gcd%@",[NSThread currentThread]);
        [self finished];
    });
}

-(void)operationQueue
{
    //线程池线程队列
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [mainQueue addOperationWithBlock:^{
        NSLog(@"mainQueue%@",[NSThread currentThread]);
        [self finished];
    }];
}

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

@end

-(void)NSthread
{
    int i =0;
    while (i ++ <3) {
        
    [self performSelectorOnMainThread:@selector(finished) withObject:self waitUntilDone:YES];
    
    NSLog(@"%@当前线程NSthread",[NSThread currentThread]);
    }
}


-(void)GCD
{
    dispatch_async(dispatch_get_main_queue(),^{

        NSLog(@"当前线程GCD%@",[NSThread currentThread]);
        int i = 0;
        while (i++ < 3) {
            [NSThread sleepForTimeInterval:1];
            [self finished];
        }
        
    });
}


-(void)operationQueue2
{
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    
    [mainQueue addOperationWithBlock:^{
       
        int i = 0;
        while (i++ < 3) {
            [NSThread sleepForTimeInterval:1];
             [self finished];
        }
    
        NSLog(@"当前线程OPerationQueue2----%@",[NSThread currentThread]);
    }];
    
}


-(void)test
{
    //初始化必须给定数组的大小
    SEL sel[3] = {
        @selector(NSthread),
        @selector(GCD),
        @selector(operationQueue2)
    };
    
    for (NSInteger i = 0; i < 3; i++) {
        
        NSThread *thread = [[NSThread alloc]initWithTarget:self selector:sel[i] object:nil];
        [thread start];//用NSThread 创建线程 注意要调用start 方法
        
    }
    
    

}

-(IBAction)testBtn:(id)sender{
    [self test];
}


@end

2015-08-28 20:47:24.621 线程通信-06练习[3106:202306] 当前线程GCD<NSThread: 0x7fbc22d17880>{number = 1, name = main}
2015-08-28 20:47:25.623 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:26.624 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:27.625 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:27.625 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:27.626 线程通信-06练习[3106:202443] <NSThread: 0x7fbc22d91560>{number = 5, name = (null)}当前线程NSthread
2015-08-28 20:47:28.627 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:29.628 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:30.628 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:30.629 线程通信-06练习[3106:202306] 当前线程OPerationQueue2----<NSThread: 0x7fbc22d17880>{number = 1, name = main}
2015-08-28 20:47:30.629 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:30.630 线程通信-06练习[3106:202443] <NSThread: 0x7fbc22d91560>{number = 5, name = (null)}当前线程NSthread
2015-08-28 20:47:30.632 线程通信-06练习[3106:202306] 完成!!!!
2015-08-28 20:47:30.634 线程通信-06练习[3106:202443] <NSThread: 0x7fbc22d91560>{number = 5, name = (null)}当前线程NSthread


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值