iOS开发之多线程

pthread
// 创建方法
pthread_t pthread;
pthread_create(&pthread, NULL, run, NULL);
// 使用的run方法 (子线程中)
void *run(void *data) {
    NSLog(@"我在子线程中执行");
    for (int i = 0; i < 5; i++) {
        NSLog(@"%d", i);
        sleep(2); // 睡 2s
    }
    return NULL;
}
NSThread
// 通过 alloc init 创建 
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
thread.name = @"NSThread"; // 给线程起名字, 方便定位Bug
thread.threadPriority = 1.0; // 设置线程的优先级
[thread start]; // 线程就绪

// 通过detachNewThreadSelector 方式创建并执行线程
[NSThread detachNewThreadSelector:@selector(runThread) toTarget:self withObject:nil];

// 通过performSelectorInBackground 方式创建线程
[self performSelectorInBackground:@selector(runThread) withObject:nil];

// 执行的runThread
- (void)runThread {
    NSLog(@"子线程 %@", [NSThread currentThread].name);
    for (int i = 0; i <= 5; i++) {
        NSLog(@"i = %d", i);
        sleep(2);
        if (i == 5) {
            [self performSelectorOnMainThread:@selector(runMainThread) withObject:nil waitUntilDone:YES];
        }
    }
}

- (void)runMainThread {
    NSLog(@"回到主线程中执行");
}
NSInvocationOperation
NSInvocationOperation *invocationOper = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocationAction) object:nil];
[invocationOper start];
- (void)invocationAction {
    for (int i = 0; i < 3; i++) {
        NSLog(@"---%d---", i);
        // 睡 2s
        [NSThread sleepForTimeInterval:2];
    }
}

NSBlockOperation *blockOper = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    }];

[blockOper addExecutionBlock:^{
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    }];
[blockOper addExecutionBlock:^{
        NSLog(@"当前线程: %@", [NSThread currentThread]);
    }];
[blockOper start];
GCD
 // DISPATCH_QUEUE_PRIORITY_LOW
 // DISPATCH_QUEUE_PRIORITY_DEFAULT
 // DISPATCH_QUEUE_PRIORITY_HIGH
 // DISPATCH_QUEUE_PRIORITY_BACKGROUND
 dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 执行耗时操作
        NSLog(@"start task one");
        [NSThread sleepForTimeInterval:5]; // 睡眠5s           
        dispatch_async(dispatch_get_main_queue(), ^{
            // 回到主线程刷新UI
            NSLog(@"刷新UI");
        });
    });

// DISPATCH_QUEUE_CONCURRENT 并行
// DISPATCH_QUEUE_SERIAL NULL 串行
dispatch_queue_t queue = dispatch_queue_create("com.xm.gcd.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
    NSLog(@"start task one");
    [NSThread sleepForTimeInterval:2];
    NSLog(@"end tast one");
});
dispatch_async(queue, ^{
    NSLog(@"start task two");
    [NSThread sleepForTimeInterval:2];
    NSLog(@"end tast one");
});
dispatch_async(queue, ^{
    NSLog(@"start task three");
    [NSThread sleepForTimeInterval:2];
    NSLog(@"end tast three");
});

dispatch_queue_t queue = dispatch_queue_create("com.xm.gcd.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
    NSLog(@"start task one");
    [NSThread sleepForTimeInterval:2];
    NSLog(@"end tast one");
});
dispatch_group_async(group, queue, ^{
    NSLog(@"start task two");
    [NSThread sleepForTimeInterval:2];
    NSLog(@"end task two");
}); 
dispatch_group_async(group, queue, ^{
    NSLog(@"start task three");
    [NSThread sleepForTimeInterval:2];
    NSLog(@"start task three");
});

dispatch_queue_t queue = dispatch_queue_create("com.xm.gcd.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();

dispatch_group_enter(group);
[self sendRueqestOne:^{
    NSLog(@"request one done");
    dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[self sendRueqestTwo:^{
    NSLog(@"request two done");
    dispatch_group_leave(group);
}];
// dispatch_group_notify会简体关联的任务组group中的所有任务是否都已完成(dispatch_group_enter和dispatch_group_leave匹配)完成了就会执行block 刷新UI操作
dispatch_group_notify(group, queue, ^{
    NSLog(@"All tasks over");
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"回到主线程刷新UI");
      });
});

- (void)sendRueqestOne:(void(^)())block {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"start task one");
        [NSThread sleepForTimeInterval:2];
        NSLog(@"end tast one");
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block();
            }
        });
    });
}

- (void)sendRueqestTwo:(void(^)())block {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"start task two");
        [NSThread sleepForTimeInterval:2];
        NSLog(@"end task two");
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block();
            }
        });
    });
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值