OC中6种多线程的使用方式

来源:互联网 发布:云狐网络科技园怎么样 编辑:程序博客网 时间:2024/06/05 14:52

NSThread 实例创建;

NSThread类方法创建;

PerformSelectorOnBackground;

NSOperationQueue block添加operation;

 NSOperationQueue直接添加operation;

dispatch_queue_create dispatch_async;

代码实现:

 //one

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

    [t start];


    //two

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


    //three

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


    //four

    NSOperationQueue *queue = [[NSOperationQueuealloc]init];

    [queue addOperationWithBlock:^{

        NSThread *thr = [[NSThreadalloc]init];

        if (![thr isMainThread]) {

            NSLog(@"是多线程");

        }

    }];


    //five

    NSOperationQueue *que = [[NSOperationQueuealloc]init];

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

    [que addOperation:op];


    //six

    dispatch_queue_t  ddph =dispatch_queue_create("hehe",NULL);

    dispatch_async(ddph, ^{

        if (![[NSThreadcurrentThread] isMainThread]) {

            NSLog(@"是多线程");

        }

    });


0 0
原创粉丝点击