您的位置:首页 > 移动开发 > IOS开发

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

2016-01-14 17:39 656 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息