Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NSOperationQueue未在iPhone上重用线程_Iphone_Cocoa_Multithreading - Fatal编程技术网

NSOperationQueue未在iPhone上重用线程

NSOperationQueue未在iPhone上重用线程,iphone,cocoa,multithreading,Iphone,Cocoa,Multithreading,我使用的是iPhone SDK 3.1.2,下面的代码显示NSOperationQueue不会为每个任务重用线程 代码在雪豹上没有任何问题 - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after app launch [window addSubview:viewController.view];

我使用的是iPhone SDK 3.1.2,下面的代码显示NSOperationQueue不会为每个任务重用线程

代码在雪豹上没有任何问题

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue setMaxConcurrentOperationCount:1];
    for(int i = 0; i < 100; i++) {
        NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];
        [queue addOperation:op];
        [op release];
    }
}

- (void)run {
    static int tc = 0;
    if([[NSThread currentThread] isMainThread]) {
        NSLog(@"MAIN THREAD");
        return;
    } else if([[NSThread currentThread] name] == nil) {
        [[NSThread currentThread] setName:[NSString stringWithFormat:@"THREAD_%d", tc++]];
    }
    NSLog(@"%@", [[NSThread currentThread] name]);
}

NSOperationQueue旨在以最有效的方式共享和重用线程,在本例中,似乎决定不重用线程是最好的方式

测试代码有它的用途(您可能已经发现了一种特殊情况,其中NSOperationQueue不能做最有效的事情),但这并不意味着NSOperationQueue在现实生活中处理真实代码时总是非常低效;事实上,我自己的经历恰恰相反

因此,我建议在实际代码中使用它,如果您有性能问题,请进一步深入了解它在幕后对线程的操作。否则别担心


另一方面,如果您仍然好奇,您可以尝试将线程的名称记录到一个NSString数组中,然后在测试代码末尾打印出所有内容,这将大大减少每个NSInvocationOperation所完成的工作量,而不是在执行过程中进行日志记录。

雪豹的NSOperation/NSOperationQueue实现现在基于GCD

iPhone仍然使用旧的Leopard实现。因此,您可以在每个平台上预期不同的结果(更不用说完全不同的硬件)


生成新线程可能是完成您赋予NSOperationQueue的任务的最有效方式。

NSOperationQueue异步执行其添加的操作(在单独的线程中)。所以,如果我们打印线程信息,那么我们可能会在大多数时间得到相同的线程对象

添加到NSOperationQueue中的NSOperation对象将不同,但线程对象可以相同

- (void)operaitonqueueTest
{

    _opQueue = [[NSOperationQueue alloc] init];
    [_opQueue setMaxConcurrentOperationCount:5];
    for(int i = 0; i < 50; i++) {
        NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];
        [_opQueue addOperation:op];
    }
}

- (void)run {
    if([[NSThread currentThread] isMainThread]) {
        NSLog(@"MAIN THREAD");
        return;
    }
    NSLog(@"currentThread = %@", [NSThread currentThread]);
}
-(无效)操作队列测试
{
_opQueue=[[NSOperationQueue alloc]init];
[_opqueuesetMaxConcurrentOperationCount:5];
对于(int i=0;i<50;i++){
NSInvocationOperation*op=[[NSInvocationOperation alloc]initWithTarget:自选择器:@selector(run)object:nil];
[_opqueueaddoperation:op];
}
}
-(无效)运行{
如果([[NSThread currentThread]isMainThread]){
NSLog(@“主线程”);
返回;
}
NSLog(@“currentThread=%@,[NSThread currentThread]);
}

我也想知道……为什么op队列是否重用线程很重要?@Peter Hosey-我猜他担心的不是重用,而是最大线程数。太多CPU绑定线程将破坏您的性能。
- (void)operaitonqueueTest
{

    _opQueue = [[NSOperationQueue alloc] init];
    [_opQueue setMaxConcurrentOperationCount:5];
    for(int i = 0; i < 50; i++) {
        NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];
        [_opQueue addOperation:op];
    }
}

- (void)run {
    if([[NSThread currentThread] isMainThread]) {
        NSLog(@"MAIN THREAD");
        return;
    }
    NSLog(@"currentThread = %@", [NSThread currentThread]);
}