简体   繁体   中英

Appropriate use of threads in Android

In android apps, we mostly use threads,other than main UI thread,when we are performing network operations . I have seen core Java applications using several threads at same time. So will it be a good practice to use multiple threads in Android other than network operations ? One of the reasons I feel that if I am using several threads and they are directly impacting the flow of my application then it may cause data mismatch as one of the thread may not have executed by the time Ui updates or user traverses to next activity. So all the data operations should happen synchronously. So basically is it a good practice to use multiple threads in android like pure java applications ? Also why this conditions does not occur in java applications ? Is is because of lower ability of the mobile to handle multiple processes as compared to a proper machine ? Thank you.

You are asking the wrong question: the real question should be: how do I design my Android app to give the user the best user experience?

As a programmer, you don't use concepts because they exist , but because they add value to the things you are doing.

For example, you look into threads to ensure that your app doesn't block or becomes unresponsive. If your app needs to do many different things, then it makes a lot of sense that these things can happen in "parallel" to a certain degree.

Of course, threads add complexity to your logic; and of course, when you start using threads, then you need to know what you are doing. Using multiple threads that cause data mismatch per se.

What causes problems is when you do not know what you are doing!

Long story shorts: your question sounds more like I am not sure how to do multi-threaded programming on Android ; and thus you should study that topic; starting here for example!

Edit: In the end, this is about balancing and being pragmatic. On the one hand, yes, (correct!) multi-threading adds value to your application; but of or course, there is also cost in terms of additional complexity. So the essential answer is probably: do what you feel comfortable with. If you have the feeling that multi-threading makes it harder for you to deliver a product that works correctly , then that answers your question - you should not use things that you are not comfortable with!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM