博客> NSOperationQueue详解
NSOperationQueue详解
1小时前 评论:0 阅读:903 风爵0审判之轮
GCD 多线程

1.

  • (void)blockOperation { NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ // 在主线程 NSLog(@"下载1------%@", [NSThread currentThread]); }];

    // 添加额外的任务(在子线程执行) [op addExecutionBlock:^{ NSLog(@"下载2------%@", [NSThread currentThread]); }];

    [op addExecutionBlock:^{ NSLog(@"下载3------%@", [NSThread currentThread]); }]; [op addExecutionBlock:^{ NSLog(@"下载4------%@", [NSThread currentThread]); }];

    [op start]; }

  • (void)invocationOperation { NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];

    [op start]; }

  • (void)run { NSLog(@"------%@", [NSThread currentThread]); }

2. // 创建队列 NSOperationQueue *queue = [[NSOperationQueue alloc] init];

// 设置最大并发操作数
queue.maxConcurrentOperationCount = 1; // 就变成了串行队列

// 添加操作
[queue addOperationWithBlock:^{

// NSLog(@"download1 --- %@", [NSThread currentThread]); // [NSThread sleepForTimeInterval:1.0]; for (NSInteger i = 0; i<5000; i++) { NSLog(@"download1 -%zd-- %@", i, [NSThread currentThread]); } }]; [queue addOperationWithBlock:^{ NSLog(@"download2 --- %@", [NSThread currentThread]); [NSThread sleepForTimeInterval:1.0]; for (NSInteger i = 0; i<1000; i++) { NSLog(@"download2 --- %@", [NSThread currentThread]); } }]; [queue addOperationWithBlock:^{ NSLog(@"download3 --- %@", [NSThread currentThread]); [NSThread sleepForTimeInterval:1.0]; for (NSInteger i = 0; i<1000; i++) { NSLog(@"download3 --- %@", [NSThread currentThread]); } }];

[queue addOperation:[[XMGOperation alloc] init]];

3.

  • (void)opetationQueue3 { // 创建队列 NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    // 设置最大并发操作数 // queue.maxConcurrentOperationCount = 2; queue.maxConcurrentOperationCount = 1; // 就变成了串行队列

    // 添加操作 [queue addOperationWithBlock:^{ NSLog(@"download1 --- %@", [NSThread currentThread]); [NSThread sleepForTimeInterval:0.01]; }]; [queue addOperationWithBlock:^{ NSLog(@"download2 --- %@", [NSThread currentThread]); [NSThread sleepForTimeInterval:0.01]; }]; [queue addOperationWithBlock:^{ NSLog(@"download3 --- %@", [NSThread currentThread]); [NSThread sleepForTimeInterval:0.01]; }]; [queue addOperationWithBlock:^{ NSLog(@"download4 --- %@", [NSThread currentThread]); [NSThread sleepForTimeInterval:0.01]; }]; [queue addOperationWithBlock:^{ NSLog(@"download5 --- %@", [NSThread currentThread]); [NSThread sleepForTimeInterval:0.01]; }]; [queue addOperationWithBlock:^{ NSLog(@"download6 --- %@", [NSThread currentThread]); [NSThread sleepForTimeInterval:0.01]; }]; }

  • (void)operationQueue2 { // 创建队列 NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    // 创建操作 // NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{ // NSLog(@"download1 --- %@", [NSThread currentThread]); // }];

    // 添加操作到队列中 // [queue addOperation:op1]; [queue addOperationWithBlock:^{ NSLog(@"download1 --- %@", [NSThread currentThread]); }]; [queue addOperationWithBlock:^{ NSLog(@"download2 --- %@", [NSThread currentThread]); }]; }

  • (void)operationQueue1 { // 创建队列 NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    // 创建操作(任务) // 创建NSInvocationOperation NSInvocationOperation op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download1) object:nil]; NSInvocationOperation op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download2) object:nil];

    // 创建NSBlockOperation NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"download3 --- %@", [NSThread currentThread]); }];

    [op3 addExecutionBlock:^{ NSLog(@"download4 --- %@", [NSThread currentThread]); }]; [op3 addExecutionBlock:^{ NSLog(@"download5 --- %@", [NSThread currentThread]); }];

    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"download6 --- %@", [NSThread currentThread]); }];

    // 创建XMGOperation XMGOperation *op5 = [[XMGOperation alloc] init];

    // 添加任务到队列中 [queue addOperation:op1]; // [op1 start] [queue addOperation:op2]; // [op2 start] [queue addOperation:op3]; // [op3 start] [queue addOperation:op4]; // [op4 start] [queue addOperation:op5]; // [op5 start] }

  • (void)download1 { NSLog(@"download1 --- %@", [NSThread currentThread]); }

  • (void)download2 { NSLog(@"download2 --- %@", [NSThread currentThread]); }
收藏
1
sina weixin mail 回到顶部