今天遇到一个奇怪的问题,使用NSOperationQueue连续添加多个子线程时出现子线程不执行的问题。解决方法如下:
在连续的打开多个子线程时,设置一定的时间间隔。 [NSThread sleepForTimeInterval:0.1];
NSInvocationOperation * theOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getDetailProfile:) object:(void *)0];
[[Func appOperationQueue] addOperation:theOp]; [theOp release]; [NSThread sleepForTimeInterval:0.1]; //不能省略此处代码。联系添加多线程时,应设置一定时间间隔 NSInvocationOperation * theOp1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getDetailProfile:) object:(void *)1]; [[Func appOperationQueue] addOperation:theOp1]; [theOp1 release]; [NSThread sleepForTimeInterval:0.1];//不能省略此处代码。联系添加多线程时,应设置一定时间间隔 NSInvocationOperation * theOp2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getDetailProfile:) object:(void *)2]; [[Func appOperationQueue] addOperation:theOp2]; [theOp2 release]; [NSThread sleepForTimeInterval:0.1];//不能省略此处代码。联系添加多线程时,应设置一定时间间隔 NSInvocationOperation * theOp3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getDetailProfile:) object:(void *)3]; [[Func appOperationQueue] addOperation:theOp3]; [theOp3 release]; [NSThread sleepForTimeInterval:0.1];//不能省略此处代码。联系添加多线程时,应设置一定时间间隔 NSInvocationOperation * theOp4 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getDetailProfile:) object:(void *)4]; [[Func appOperationQueue] addOperation:theOp4]; [theOp4 release];