iOS多线程

本文探讨了在大规模计算任务中使用多线程、并发编程和并行计算技术提高性能的方法,包括线程池、多线程计算、任务分发、同步与互斥等关键概念,以及如何在不同计算环境中实现高效并行计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)updateLabel:(NSNumber *)aObject
{
    self.displayLabel.text = [aObject stringValue];
    NSLog(@"is duo xiancheng = %@",[[NSNumber numberWithBool:[NSThread isMultiThreaded]] stringValue]);
}


int n = 200000000;


- (void)compute:(NSNumber *)aObject
{
    double sum = 0;
    for (int a = aObject.intValue; a < n ; a++)
    {
        sum = sum + a;
//        [self print:a];
    }
    [self performSelectorOnMainThread:@selector(updateLabel:) withObject:[NSNumber numberWithDouble:sum] waitUntilDone:NO];
}


- (void)thread1
{
    [NSThread detachNewThreadSelector:@selector(compute:) toTarget:self withObject:[NSNumber numberWithInt:i]];
}


- (void)thread2
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
//group
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        double sum = 0;
        for (int a = i; a < n  ; a++)
        {
            sum = sum + a;
        }
        NSLog(@"queue 1 = %f",sum);
    });
    
    dispatch_barrier_async(queue, ^{
        double sum = 0;
        for (int a = i; a < n  ; a++)
        {
            sum = sum + a;
        }
        NSLog(@"barrier  = %f",sum);
    });
    
    dispatch_group_async(group, queue, ^{
        double sum = 0;
        for (int a = i; a < n  ; a++)
        {
            sum = sum + a;
        }
        NSLog(@"queue 2 = %f",sum);
    });
    
    
    
    dispatch_group_async(group, queue, ^{
        double sum = 0;
        for (int a = i; a < n  ; a++)
        {
            sum = sum + a;
        }
        NSLog(@"queue 3 = %f",sum);
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
       self.displayLabel.text = @"all done";
    });
    
    
//async
//    dispatch_async(queue, ^{
//        double sum = 0;
//        for (int a = i; a < n  ; a++)
//        {
//            sum = sum + a;
////            [self print:a];
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//            self.displayLabel.text = [NSString stringWithFormat:@"%f",sum];
//            NSLog(@"is duo xiancheng = %@",[[NSNumber numberWithBool:[NSThread isMultiThreaded]] stringValue]);
//        });
//    });
    
//
    dispatch_async(queue, ^{
        double sum = 0;
        for (int a = i; a < n  ; a++)
        {
            sum = sum + a;
        }
        NSLog(@"barrier queue 1 = %f",sum);
    });
    
    dispatch_async(queue, ^{
        double sum = 0;
        for (int a = i; a < n  ; a++)
        {
            sum = sum + a;
        }
        NSLog(@"barrier  = %f",sum);
    });
    
    dispatch_async(queue, ^{
        double sum = 0;
        for (int a = i; a < n  ; a++)
        {
            sum = sum + a;
        }
        NSLog(@"barrier queue 2 = %f",sum);
    });
    
    
    
    dispatch_async(queue, ^{
        double sum = 0;
        for (int a = i; a < n  ; a++)
        {
            sum = sum + a;
        }
        NSLog(@"barrier queue 3 = %f",sum);
    });
}


- (void)thread3
{
    NSMethodSignature *sig = [self methodSignatureForSelector:@selector(compute:)];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
    [invocation setTarget:self];
    [invocation setSelector:@selector(compute:)];
    NSNumber *num = [NSNumber numberWithInt:i];
    [invocation setArgument:&num atIndex:2];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
    //[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(compute:) object:];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
}


- (void)thread4
{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(compute:) object:[NSNumber numberWithInt:i]];
    [thread start];
}


- (void)thread5
{
    [self performSelectorInBackground:@selector(compute:) withObject:[NSNumber numberWithInt:i]];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值