What is concurrency?
- Doing multiple things together. Often known as multi-threading
Why bother?
- Keep the UI responsive
- Leverage idle resources
Where to take advantage of concurrency?
- In case of intense computations like parsing or image manipulations concurrency is really suitable. This is needed for good user experience in applications by not blocking the UI or the main thread thereby enhancing the application performance
- During Significant disk I/O like accessing large data sets and importing data
Note: Instrument before using this feature. Do not use for simple operations where you gain nothing much with this approach. Eg: adding a+b in separate thread. Find out the granularity and then decide whether concurrent is really required
Where to avoid Concurrency?
- During networking procedure do not use concurrency as these events consume high latency
- Use asynchronous API's instead of synchronous. In case of asynchronous API's system handles concurrency for you. Therefore avoid blocking (synchronous) API's for network tasks
- Do not use concurrency for idle behaviors. Instead use NSTimers for such processes, where CPU cycles are wasted.
The Challenge
- Traditionally threading is really hard
- Managing shared mutable state is difficult
- IPhone OS ( NSOperation) makes it a lot easier
Concurrency simplified - Thread confinement
- Keep object access confined to one thread
- Forget about locking, signalling, sync points.
- NSOperation and NSOperationQueue provide this
NSOperation:
- Compelling object oriented encapsulation
- here you can know the state (isExecuting,isFinished …) or set dependencies or you can set priority
No comments:
Post a Comment