पर थ्रेड का पुन: उपयोग नहीं कर रहा है, मैं आईफोन एसडीके 3.1.2 का उपयोग कर रहा हूं, और निम्न कोड दिखाता है कि NSOperationQueue प्रत्येक कार्य के लिए थ्रेड का पुन: उपयोग नहीं करता है।एनएसओपरेशन क्यूई आईफोन
कोड Snow Leopard पर कोई समस्या नहीं है।
- (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]);
}
आउटपुट दिखाता है कि यह 100 कार्यों को निष्पादित करने के लिए 100 धागे बनाता है।
2010-01-07 11:46:03.502 OperationQueueTest[7911:4503] THREAD_0
2010-01-07 11:46:03.506 OperationQueueTest[7911:4d03] THREAD_1
2010-01-07 11:46:03.507 OperationQueueTest[7911:4807] THREAD_2
2010-01-07 11:46:03.510 OperationQueueTest[7911:4d07] THREAD_3
2010-01-07 11:46:03.514 OperationQueueTest[7911:5007] THREAD_4
2010-01-07 11:46:03.516 OperationQueueTest[7911:4f0b] THREAD_5
2010-01-07 11:46:03.518 OperationQueueTest[7911:4e0f] THREAD_6
...
2010-01-07 11:46:03.740 OperationQueueTest[7911:4ea7] THREAD_97
2010-01-07 11:46:03.744 OperationQueueTest[7911:4dcf] THREAD_98
2010-01-07 11:46:03.746 OperationQueueTest[7911:460f] THREAD_99
मुझे पता है कि रूप में अच्छी तरह ... – gcamp
क्यों इससे कोई फर्क पड़ता कि सेशन कतार धागे पुन: उपयोग है या नहीं करना चाहते हैं? –
@ पीटर होसे - मुझे लगता है कि यह पुनः उपयोग नहीं है लेकिन अधिकतम थ्रेड गिनती के बारे में चिंतित है। बहुत सारे सीपीयू बाउंड थ्रेड आपके प्रदर्शन को मिटा देंगे। – DougW