Why do Android services not run in separate threads? - android

Why does a service not run in it's own thread under Android? And what were the design considerations?

It is all about Flexibility: I assume you are familiar with so many Frameworks like for example e.g. Volley, Retrofit etc. These Frameworks has their own Thread implementation behind scene so if Service by default is executed in a separate Thread then it is overkill. Why? because the threading is handled by the framework already so why the need to have another thread?
See also Why is creating a Thread said to be expensive?. And put also into consideration that you are doing Threading in an Android device and not into a powerful Desktop.

Take a look at IntentService I think this is that you are looking for.

It gives you more flexibility. For example you could run each request in a seperate thread with special priorities or you could store the requests in a queue and only start the next thread if the previous one has finished. A IntentService provides a default configuration that in the most cases fits your needs from scratch.(http://developer.android.com/guide/components/services.html). Such decisions depend on your use case.

Related

Can we use Log in Android to know the sequential flow of thread?

How can I know the flow if more than two threads are in use , as in case of any networking library like VOLLEY , ASYSNCTASK etc .?
You can see the sequence of threads by logging, but by definition you can't possibly know when exactly threads will execute since it's not deterministic. Who decides this is the kernel based on some internal rules and Android has some "layers" built on top of the Linux Kernel that allow you to somehow control the priority of your threads. What this means is: everytime you run your application the order of the methods inside threads will change.
I recommend you to watch this presentation where this is explained in detail: https://www.youtube.com/watch?v=xQoc1nSvvMw
There is one tool that can help you to debug threads: Dalvik Debug Monitor Server (DDMS) https://developer.android.com/studio/profile/ddms.html
Here is a post explaining how to use it https://www.toptal.com/android/android-ddms-ultimate-power-console

Export File-I/O-Operations into seperate threads recommended or maybe a must?

At the moment I'm writing tests and think about to export all File-I/O-Operations into separate threads within my GUI-App, because I fear that a large file can block the main-thread. This wouldn't be user-friendly.
Its common to export File-I/O-Operations into separate worker-threads?
Well, it depends, but generally it is a very good idea. If your main thread is your event pump, then yes; it is highly recommended to execute lengthy operations in separate threads. Particularly if the app is using a slow GPRS/3G connection, you typically don't want the app to block when using the network. Local file operations can occasionally be quite slow as well, depending on how busy the device is.
The Java Swing Worker model is an example of how to do this in a modular and thread-safe way. I suggest you study it for inspiration as it is well documented. Then again, I am sure Android has similar facilities for executing code outside the main thread.
If you are writing (unit) tests for it, I suggest establishing a pattern where you can inject your worker code as part of the test and keeping your I/O code strictly outside of context from the main thread. That way, you can simulate how the app behaves during slow connection without actually having to use the network at all.

Android convention to denote method UI thread safety

Android documentation, tutorials, videos and the like are filled with best practice on how to avoid non-essential processing on the UI thread. However, when creating a big app or using external components it can be hard to remember which methods block the UI thread and which do not. I've often seen threading mentioned in comments, but that seems error prone for lazy/rushed situations, and not everything gets an ideal code review.
What naming, annotation, or other conventions exist to help developers quickly determine if the methods they are considering are UI thread optimized or not?

Android thread priority

How is thread priority managed in Android? (Or what is the threading policy in Android?) GUI threads would be assigned higher priority, right?
I've seen applications that create separate thread to clean up (native) resources since finalize() is discouraged. However, there is still memory leak (or memory inefficiency) since the cleanup thread is not running frequently enough.
Some other apps, those ignoring the advice on not to use finalize(), rely on the finalizer to clean up resources. So I'm wondering how is the finalizer thread scheduled? Is it a reliable way to release resources, either managed or native?
I would strongly recommend you to use AsyncTask for doing something in different thread. AsyncTask is very easy to use and I would say that it is one of the biggest advantages of java. I really miss it in obj-c.
Async task got method
onPostExecute(){}
in which you can clean whatever you want after thread done it's job.
http://labs.makemachine.net/2010/05/android-asynctask-example/
http://marakana.com/s/video_tutorial_android_application_development_asynctask_preferences_and_options_menu,257/index.html
Links with tuts about AsyncTask.
About priority: threads in android are threads in java, and they have priority from 0 to 10.
You can set them for each thread as you would like.
And about
finalize();
you should avoid using this method. As I already said u should better use AsyncTask class instead of Thread.

AsyncTask also in flash?

i have develop one game size of 27mb,i load it in device & its performance are going to lower and some time its hang the device.there are much Media resource are used in this game.if i make it in android then i will handle with Asynctask and make some process in background and also make memory management but here i have develop game in flash with AS3 and use Adobe Air. so my question is :: is there any method like asynTask are stay in flash or any performance related thing by which i take precaution against poor performance?
Thanks
nik
You should consider using Flash 11 and AIR 3 - the new version allow the execution of native code, so that you can use Flash and AIR for the UI and the animations while at the same time you can execute asynchronously some native threaded code that otherwise would have of been occupying the time in between frames. In the native code you can have all of the calculations that are not related to the UI - e.g. the AI logic.
A detailed tutorial is provided by Lee Brimelow on his blog - Two-Part Tutorial on Android Native Extensions
As far as I know there's only two threads network thread and thread doing all other actions.
Asynctask is about optimal using UI thread, so because of there's no separate UI thread in flash/AIR there could be no analogous tricks. As for other methods, I'm affraid there's no general answer.
If you want to load assets asynchronously, you should use the URLLoader and Loader classes to load resources. They will load stuff in the background and throw an Event (Event.COMPLETE) when they are done.
Here are some links on how to use PixelBender to do stuff like number crunching in a separate thread, I'm not sure it is supported on Android though, so you would have to look in to that further:
http://blogs.adobe.com/aharui/2008/01/threads_in_actionscript_3.html
Stack Overflow question, the answer by Ascension Systems:
Flash parallel programming
You should read Optmizing performance for the Flash Platform
help.adobe.com/en_US/as3/mobile/flashplatform_optimizing_content.pdf

Categories

Resources