![]()
1 - (void)viewDidLoad {
2 [super viewDidLoad];
3 // Do any additional setup after loading the view, typically from a nib.
4 /**
5 * NSOperation不能直接进行多线程的创建,需要借助:NSOperationQueue
6 */
7
8 // 使用NSOperation的第一个类去创建子线程
9 NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil];
10 // 在单独使用NSOperation的子类创建子线程的时候,一定要启动才行
11 // [operation start];
12
13 // 使用NSOperation的第二个子类创建子线程
14 NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
15 NSLog(@"我是block");
16 NSLog(@"current = %@", [NSThread currentThread]);
17 NSLog(@"main = %@", [NSThread mainThread]);
18 }];
19 // 启动
20 // [blockOperation start];
21
22 // 在使用NSOperation的子类去创建线程的时候,实际上线程没有真正意义上的创建
23 // 需要把上班的两个线程,放到操作列队
24 // 一旦将创建的对象加入到操作队列中,就不能调用start方法,否则会造成崩溃
25 // add 和 start 不能共存
26 NSOperationQueue *queue = [[NSOperationQueue alloc] init];
27 // 最大的并发数量
28 // 当值设置为1的时候,可以叫做串行:即顺序执行
29 // 当设置大于1的时候,叫做并行:多条通道执行
30 queue.maxConcurrentOperationCount = 3;
31
32 [queue addOperation:operation];
33 [queue addOperation:blockOperation];
34 }
35
36 - (void)test {
37 NSLog(@"😊");
38 NSLog(@"current = %@", [NSThread currentThread]);
39 NSLog(@"main = %@", [NSThread mainThread]);
40 }