What is the difference between Thread.run () and Handler.post () and a service on Android?

As a rule, it is recommended to use Handler.post() in Android when you need to perform some tasks in different threads.
And when I want to do some tasks in the background, I was offered to start Service .

But I feel more comfortable using new Thread (new Runnable(){...} ); as I'm used to.

But I'm afraid that creating new threads manually may behave differently in Android, for example, maybe it will automatically stop when memory is low when using Service , maybe not?

Wanting a clear answer to help me get rid of this confusion. ^ ^

+4
source share
3 answers

When performing certain tasks in android, it is highly recommended to use Handler, because:

In Android, you can only update views in the original stream, i.e. the thread in which they were created, otherwise the application may throw an exception saying

 android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

Handlers in Android are tied to the thread in which they are created. Each Handler instance is associated with one thread and this thread's message queue. When you create a new handler, it is attached to the thread / message queue of the thread that creates it - from this point it will deliver messages and executable files to the message queue and execute them as they exit the message queue. Thus, Handlers are the safest for Android.

While Services, Heres a piece of code from http://developer.android.com/reference/android/app/Service.html

What is a service?

The big confusion regarding the Service class actually revolves around the fact that this is not the case:

Service is not a separate process. The service object itself does not imply that it works in its own process; Unless otherwise specified, it works in the same process as the application in which it is included.

Service is not a thread. This does not mean that you need to do the work of the main thread (to avoid errors associated with application errors). Thus, the service itself is actually very simple, providing two main functions:

Establishing an application to tell the system what it wants to do in the background (even if the user does not interact directly with the application). This corresponds to calls to Context.startService (), which ask the system for a schedule of the service, which must be executed until the service or someone else explicitly stops it.

An application tool for providing some of its functions to other applications. This corresponds to calls to Context.bindService (), which allows you to establish a long connection with the service to interact with it.

And finally Threads,

threads are used to perform some heavy functions not related to viewing, some heavy calculations work like parsing, etc., so that it does not block your user interface and does all the work safely ...

+7
source

Calling a thread from the user interface violates the single-thread model: the Android UI toolkit is not thread safe and should always be controlled by the user interface thread. that is why the following alternatives instead of using the stream directly

  • Activity.runOnUiThread (Runnable)
  • View.post (Runnable)
  • View.postDelayed (Runnable, long)
  • Handler

A handler is used to communicate between a UI thread and a background thread. AsyncTask is used to perform a small heavy task in the background.

If you have the easiest background job, use a handler for heavier background jobs. Use AsyncTask and do the hardest work in the background.

See http://android-developers.blogspot.com/2009/05/painless-threading.html

Example

  • To add or remove data from the list, you can use a handler
  • To get the XML data from the server and reflect it in your view, use AsynTask. AsyncTask is preferred over threads
  • To play a music file, use
+2
source

Handlers are designed to run code fragments in a specific thread. The most common use case is that you are in the context of a workflow and want to run something in the main thread (UI) ... in this case, you will create a handler in your main thread (during initialization, say), and post ( ) Runnable to it from the workflow.

Thread.run () is the base entry point of the Java thread. You implement this when you need a background thread for I / O input or heavy computing, except on Android you usually don't, because it is much easier to use AsyncTask, in which you override doInBackground () for your code workflow (i.e. where you usually implement run ()) and override onPostExecute () with the code that you want to run in the user interface thread after completing the work task.

Services are something completely different ... they are NOT threads. The service code will work in the main thread along with your user interface. You can still use AsyncTask, etc. Or use a special service called IntentService, which will work on the workflow.

0
source

Source: https://habr.com/ru/post/1388581/


All Articles