pthread
pthread_t pthread;
pthread_create(&pthread, NULL, run, NULL);
void *run(void *data) {
NSLog(@"我在子线程中执行");
for (int i = 0; i < 5; i++) {
NSLog(@"%d", i);
sleep(2);
}
return NULL;
}
NSThread
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
thread.name = @"NSThread";
thread.threadPriority = 1.0;
[thread start];
[NSThread detachNewThreadSelector:@selector(runThread) toTarget:self withObject:nil];
[self performSelectorInBackground:@selector(runThread) withObject:nil];
- (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);
[NSThread sleepForTimeInterval:2];
}
}
NSBlockOperation *blockOper = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"当前线程: %@", [NSThread currentThread]);
}];
[blockOper addExecutionBlock:^{
NSLog(@"当前线程: %@", [NSThread currentThread]);
}];
[blockOper addExecutionBlock:^{
NSLog(@"当前线程: %@", [NSThread currentThread]);
}];
[blockOper start];
GCD
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"start task one");
[NSThread sleepForTimeInterval:5];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"刷新UI");
});
});
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, 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();
}
});
});
}