问题:1.
- (BOOL)isConcurrent;有何作用?
测试,没发现有何作用
问题2:
任务队列中,某一个任务是不是在同一个线程中执行的?
self.queue = [[NSOperationQueue alloc] init];
[self.queue setMaxConcurrentOperationCount:3];
for (int i = 0; i < 10; i++)
{
NDTOperation *operation = [[NDTOperation alloc] init];
operation.index = i;
[self.queue addOperation:operation];
}
@implementation NDTOperation
-(void)main
{
NSLog(@"I am %d 线程****%@",self.index,[NSThread currentThread]);
sleep(3);
[self print];
}
-(void)print
{
NSLog(@"Printe********I am %d 线程******%@",self.index,[NSThread currentThread]);
}
@end
结果分析:1、同一个任务中的代码,如果任务中没有创建其他的线程,将在同一线程中执行;
2、各个任务之间没有先后顺序,交叉进行
问题3.当setMaxConcurrentOperationCount = 1的时候,任务的执行顺序问题?
2014-03-21 18:22:08.034 QuenuTest[6767:1403] I am 0 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:11.038 QuenuTest[6767:1403] Printe********I am 0线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:14.040 QuenuTest[6767:1403] Printe1********I am 0线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:17.042 QuenuTest[6767:1403] Printe2********I am 0线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:17.043 QuenuTest[6767:1403] I am 1 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:20.045 QuenuTest[6767:1403] Printe********I am 1线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:23.046 QuenuTest[6767:1403] Printe1********I am 1线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:26.047 QuenuTest[6767:1403] Printe2********I am 1线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:26.048 QuenuTest[6767:1403] I am 2 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:29.050 QuenuTest[6767:1403] Printe********I am 2线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:32.052 QuenuTest[6767:1403] Printe1********I am 2线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:35.053 QuenuTest[6767:1403] Printe2********I am 2线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:35.054 QuenuTest[6767:1403] I am 3 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:38.056 QuenuTest[6767:1403] Printe********I am 3线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:41.057 QuenuTest[6767:1403] Printe1********I am 3线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:44.058 QuenuTest[6767:1403] Printe2********I am 3线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:44.058 QuenuTest[6767:1403] I am 4 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:47.060 QuenuTest[6767:1403] Printe********I am 4线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:50.062 QuenuTest[6767:1403] Printe1********I am 4线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:53.064 QuenuTest[6767:1403] Printe2********I am 4线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:53.065 QuenuTest[6767:1403] I am 5 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:56.067 QuenuTest[6767:1403] Printe********I am 5线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:22:59.069 QuenuTest[6767:1403] Printe1********I am 5线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:02.071 QuenuTest[6767:1403] Printe2********I am 5线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:02.072 QuenuTest[6767:1403] I am 6 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:05.073 QuenuTest[6767:1403] Printe********I am 6线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:08.075 QuenuTest[6767:1403] Printe1********I am 6线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:11.077 QuenuTest[6767:1403] Printe2********I am 6线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:11.078 QuenuTest[6767:1403] I am 7 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:14.080 QuenuTest[6767:1403] Printe********I am 7线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:17.081 QuenuTest[6767:1403] Printe1********I am 7线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:20.082 QuenuTest[6767:1403] Printe2********I am 7线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:20.083 QuenuTest[6767:1403] I am 8 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:23.085 QuenuTest[6767:1403] Printe********I am 8线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:26.087 QuenuTest[6767:1403] Printe1********I am 8线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:29.088 QuenuTest[6767:1403] Printe2********I am 8线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:29.088 QuenuTest[6767:1403] I am 9 线程****<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:32.090 QuenuTest[6767:1403] Printe********I am 9线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:35.092 QuenuTest[6767:1403] Printe1********I am 9线程******<NSThread: 0x89235b0>{name = (null), num = 2}
2014-03-21 18:23:38.093 QuenuTest[6767:1403] Printe2********I am 9线程******<NSThread: 0x89235b0>{name = (null), num = 2}
结果解析:多个任务,按顺序执行;