首页 > 代码库 > iOS 关于多线程的一些知识点(不断更新)

iOS 关于多线程的一些知识点(不断更新)

1.NSOperation

对于NSOperation,In OS X v10.6 and later, operation queues ignore the value returned by this method and always start operations on a separate thread.就是一个operation就新建一个thread。但是,这个说法是针对 NSOperationQueue *testQueue = [[NSOperationQueue alloc] init]; 这种queue来说的,如果向mainQueue中加入operation,那么这些operation一定会执行在main thread中,而且由于都在main thread中执行,在operation一次runloop action 完成前,不会去理会其他的operation,也就是说,如果向mainqueue加入多个operation,那么他们会串行执行。

下面看一段例子

 NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    [mainQueue addOperationWithBlock:^{

        for(int i=0;i<200;i++)
        {
            NSLog(@"i is %d",i);
        }

    }];

    [mainQueue addOperationWithBlock:^{

        for(int m=0;m<200;m++)
        {
            NSLog(@"m is %d",m);
        }

    }];

以下是它的部分输出

2014-05-05 10:21:55.000 OperationQueueTest[9876:60b] i is 194
2014-05-05 10:21:55.001 OperationQueueTest[9876:60b] i is 195
2014-05-05 10:21:55.001 OperationQueueTest[9876:60b] i is 196
2014-05-05 10:21:55.002 OperationQueueTest[9876:60b] i is 197
2014-05-05 10:21:55.002 OperationQueueTest[9876:60b] i is 198
2014-05-05 10:21:55.003 OperationQueueTest[9876:60b] i is 199
2014-05-05 10:21:55.004 OperationQueueTest[9876:60b] m is 0
2014-05-05 10:21:55.004 OperationQueueTest[9876:60b] m is 1
2014-05-05 10:21:55.004 OperationQueueTest[9876:60b] m is 2
2014-05-05 10:21:55.005 OperationQueueTest[9876:60b] m is 3
2014-05-05 10:21:55.005 OperationQueueTest[9876:60b] m is 4
2014-05-05 10:21:55.006 OperationQueueTest[9876:60b] m is 5
2014-05-05 10:21:55.006 OperationQueueTest[9876:60b] m is 6
2014-05-05 10:21:55.007 OperationQueueTest[9876:60b] m is 7
2014-05-05 10:21:55.017 OperationQueueTest[9876:60b] m is 8
2014-05-05 10:21:55.017 OperationQueueTest[9876:60b] m is 9
2014-05-05 10:21:55.018 OperationQueueTest[9876:60b] m is 10
2014-05-05 10:21:55.018 OperationQueueTest[9876:60b] m is 11
2014-05-05 10:21:55.019 OperationQueueTest[9876:60b] m is 12

可以看出,加入mainQueue后,两个operation是顺序执行的。

再看下面一段代码

  NSOperationQueue *testQueue = [[NSOperationQueue alloc] init];
    [testQueue addOperationWithBlock:^{

        NSLog(@"111111  thread is %@",[NSThread currentThread]);
        for(int i=0;i<200;i++)
        {
            NSLog(@"i is %d",i);
        }

    }];

    [testQueue addOperationWithBlock:^{
        NSLog(@"222222 thread is %@",[NSThread currentThread]);
        for(int m=0;m<200;m++)
        {
            NSLog(@"m is %d",m);
        }
        
    }];

下面是它的部分输出

2014-05-05 10:27:59.566 OperationQueueTest[9898:1303] i is 186
2014-05-05 10:27:59.566 OperationQueueTest[9898:1303] i is 187
2014-05-05 10:27:59.564 OperationQueueTest[9898:3507] m is 183
2014-05-05 10:27:59.566 OperationQueueTest[9898:1303] i is 188
2014-05-05 10:27:59.566 OperationQueueTest[9898:1303] i is 189
2014-05-05 10:27:59.567 OperationQueueTest[9898:1303] i is 190
2014-05-05 10:27:59.567 OperationQueueTest[9898:1303] i is 191
2014-05-05 10:27:59.568 OperationQueueTest[9898:1303] i is 192
2014-05-05 10:27:59.568 OperationQueueTest[9898:1303] i is 193
2014-05-05 10:27:59.568 OperationQueueTest[9898:1303] i is 194
2014-05-05 10:27:59.569 OperationQueueTest[9898:1303] i is 195
2014-05-05 10:27:59.569 OperationQueueTest[9898:1303] i is 196
2014-05-05 10:27:59.567 OperationQueueTest[9898:3507] m is 184
2014-05-05 10:27:59.570 OperationQueueTest[9898:3507] m is 185
2014-05-05 10:27:59.569 OperationQueueTest[9898:1303] i is 197
2014-05-05 10:27:59.571 OperationQueueTest[9898:1303] i is 198
2014-05-05 10:27:59.572 OperationQueueTest[9898:1303] i is 199
2014-05-05 10:27:59.571 OperationQueueTest[9898:3507] m is 186
2014-05-05 10:27:59.573 OperationQueueTest[9898:3507] m is 187
2014-05-05 10:27:59.573 OperationQueueTest[9898:3507] m is 188
2014-05-05 10:27:59.574 OperationQueueTest[9898:3507] m is 189
2014-05-05 10:27:59.575 OperationQueueTest[9898:3507] m is 190
2014-05-05 10:27:59.575 OperationQueueTest[9898:3507] m is 191
2014-05-05 10:27:59.576 OperationQueueTest[9898:3507] m is 192
2014-05-05 10:27:59.577 OperationQueueTest[9898:3507] m is 193

可以看出Operation是同时在运行的,它们运行在2个不同的thread中!