1. NSBlockOperation
使用start
#import <Foundation/Foundation.h>
int main(){
NSLog(@"当前线程是%@",[NSThread currentThread]);
NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"这是blockOperation1,线程%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:3];
}];
NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"这是blockOperation2,线程%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:3];
}];
NSBlockOperation *blockOperation3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"这是blockOperation3,线程%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:3];
}];
[blockOperation1 start];
[blockOperation2 start];
[blockOperation3 start];
}
- 向NSBlockOperation中添加ExecutionBlock,是异步的,会开启子线程,可能存在线程复用的情况
- 几个operation之间是同步的,operation和它的几个executionBlock是异步的
#import <Foundation/Foundation.h>
int main(){
NSLog(@"当前线程是%@",[NSThread currentThread]);
NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"这是blockOperation1,线程%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:3];
}];
[blockOperation1 addExecutionBlock:^{
NSLog(@"这是blockOperation1的附加操作,线程%@",[NSThread currentThread]);
}];
NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"这是blockOperation2,线程%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:3];
}];
[blockOperation2 addExecutionBlock:^{
NSLog(@"这是blockOperation2的附加操作,线程%@",[NSThread currentThread]);
}];
[blockOperation2 addExecutionBlock:^{
NSLog(@"这是blockOperation2的附加操作2,线程%@",[NSThread currentThread]);
}];
[blockOperation1 start