문제

After reading Brad Fitzpatrick's article on StrictMode I'm left with a few questions.

Where does File I/O belong in my applications? (I know the UI thread is wrong but could spawning a new thread be worse?)

If I should use a thread for doing disk access then how should my application handle the implementation? Send parameters to an AsyncTask? Shared variables? I want to optimize memory usage as well not only responsiveness (especially since there isn't any noticeable difference on my phones)

@Brad if your reading this: I'd love a blog post with samples (or links to places in the aosp source) where this was done in the frameworks.

Thanks everyone!

도움이 되었습니까?

해결책

Don't worry about threads. Starting a new Thread is faster than the disk. On a Nexus One, IIRC, creating new Threads & starting them in a loop is ~1 ms.

Use whatever's most appropriate for your application: AsyncTask, IntentService, or a new Thread with shared state (with appropriate locks!). An AsyncTask is guaranteed to keep running if the user switches away but is a bit easier to use and pops you back to the UI thread when it's done. An IntentService will keep running and complete, but doesn't help you get back to the UI thread with the result. You'll need to send the result (if any) to a Handler on your UI thread.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top