iOS开发之多线程的五种方法

来源:互联网 发布:清华外语软件下载 编辑:程序博客网 时间:2024/06/11 02:12

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    //多线程第一种

    NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(ThreadOne:)object:nil];

    [thread start];

    

    //多线程第二种

    [NSThreaddetachNewThreadSelector:@selector(ThreadTwo:)toTarget:selfwithObject:nil];

    

    //多线程第三种

    [selfperformSelectorInBackground:@selector(ThreadThree:)withObject:nil];

    

    //多线程第四种

    NSOperationQueue *myQueue = [[NSOperationQueuealloc]init];

    NSInvocationOperation *invocationOne = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(QueueOne:)object:nil];

    NSInvocationOperation *invocationTwo = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(QueueTwo:)object:nil];

    [myQueue addOperation:invocationOne];

    [myQueue addOperation:invocationTwo];

    invocationOne.queuePriority =NSOperationQueuePriorityVeryHigh;//优先级

    

    //多线程第五种

    dispatch_queue_t queue =dispatch_queue_create("test",NULL);

    dispatch_async(queue, ^{

        NSLog(@"这里进行请求数据操作");

        

        dispatch_sync(dispatch_get_main_queue(), ^{

            NSLog(@"这里加载界面");

            

        });

    });

    

}


-(IBAction)ThreadOne:(id)sender{

    

}

-(IBAction)ThreadTwo:(id)sender{

    

}

-(IBAction)ThreadThree:(id)sender{

    

}

-(IBAction)QueueOne:(id)sender{

    

}

-(IBAction)QueueTwo:(id)sender{

    

}


@end


0 0
原创粉丝点击