user123321
user123321

Reputation: 12783

When to Thread. When not to Thread

I'm new to the idea of Threading, but not asynchronous behavior. My Android app is taking ~180 millisecond to start up and ~550 milli when I use GoogleAnalytics trackViewPage method and MobFoxView constructor. Coming from Actionscript 3, anything that "took time" was automatically async and I was forced to handle it with listeners which is a bit different in Android it appears. It seems I'M responsible for deciding when something should be asynchronous. So I guess my question is, HOW do I decide what should be async? Is it by milliseconds of executing? But perhaps that changes greatly between devices. Perhaps it should be by ... or is it by ....?

Upvotes: 0

Views: 366

Answers (1)

Olegas
Olegas

Reputation: 10507

You need to know one important thing - by default everything you do without starting separate thread is executed on "main" thread (also knows as UI-thread).

If you do something, which can block - your UI will lag and users will suffer.

If you doing something, which is not about UI but about database query, network call or potentially long blocking operation - you need to start thread directly or use AsyncTask.

Also you must note, if you try to do something with UI (e.g. set value to a TextView) from not-main thread you will fail. UI can be acessed only from UI-Thread.

Upvotes: 4

Related Questions