Very simp开发者_如何学JAVAly, is there a one to one connection between a thread(or NSThread) and an NSOperation? Or is it abstracted out an operation is kind of a task that can be picked up and run by multiple threads in the background?
Not a one-to-one connection, no. The advantage of using NSOperation
subclasses is that you're not required to manage the multi-threading yourself. Apple even (confusingly) defines the typical NSOperation
subclass (i.e., one overriding the -main
method) as non-concurrent, not because it doesn't support concurrency, but because the details of concurrency are managed by the superclass:
Don’t be confused by the terminology: just because an operation is non-concurrent, does not mean it cannot be executed concurrently, it simply means that you don't have to handle the concurrency yourself.
In that sense, an NSOperation
subclass is much more like the target object of NSThread's
detachNewThreadSelector:toTarget:withObject:
.
The alternative, if you want control over how the concurrency behaves, is to override -start
and set up concurrency as needed before invoking -main
.
Here's a very good overview: Managing Concurrency with NSOperation
精彩评论