Service or thread or AsyncTask

Sorry my questions, because I'm still very new to programming, so I don't quite understand the concepts of mainthreads and asynchronous tasks, as well as services and threads. I read the documentation for services for Android because I want to perform some tasks from the main thread. It says:

If you need to do work outside the main thread, but only during the time the user interacts with your application, then you should perhaps instead create a new thread, not a service.

1. They say that the "flow" stops immediately after you leave the application (for example, the "Home" button)?

For example, if you want to play some kind of music, but only the activity is running, you can create a stream in onCreate (), start it by running it on onStart (), and then stop it on onStop (). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the document Processes and Threading for more information. streams.

2. If threads are baked in Java, why does android have AsyncTasks?

Remember that if you use the service, it still works in your main application thread by default, so you must create a new thread inside the service if it performs intensive or blocking operations.

3. Does this basically mean that almost every service will basically have a thread created inside it?

4. Is AsyncTask running inside the service incorrectly?

+6
source share
4 answers

1. They say that the "flow" stops immediately after you leave the application (for example, the "Home" button)?

A Thread must be destroyed when the Thread that was launched is destroyed. So, if you run Thread in an Activity , then it should be destroyed when this Activity is destroyed or transferred to Service . For example, you can start music in Thread and update songs there, but if you want it to continue playing when Activity been destroyed, it should be moved to Service

2. If threads are baked in Java, why does android have AsyncTasks?

An AsyncTask allows AsyncTask to run the background and easily update the UI before, during and after the background work is done using any built-in methods except doInBackground() , because this is the only one that does not work on UI Thread

3. Does this basically mean that almost every service will basically have a thread created inside it?

Not necessarily, but you can create a Thread inside it.

4. Is AsyncTask running inside the service incorrectly?

No. Can you do this.

AsyncTask is a great way to do background work. Its methods greatly simplify updating the UI . But you need to read the documentation carefully (maybe even a few times) to make sure you fully understand how to use them. In addition, remember that they are intended for short-term operations, so they can be useful for downloading network data, but should not be used for things that last more than a few seconds (according to the documents).

+10
source
  • The thread does not stop immediately after exiting the application. The proposal to use a separate stream only so that you do not block your GUI application.

  • AsyncTasks actually uses ThreadPool behind the scenes, as thread creation is an expensive process. If you have a lot of short tasks, using AsyncTask is just a quick, easy, and efficient way to do them without locking the applicationโ€™s GUI.

  • Yes, essentially. However, the service is heavier than the stream. Using a service instead of a stream is not a good idea. In addition, services can actually be executed to perform the entire other process. Just FYI.

  • No. It would be nice if you had many short tasks.

If you are trying to complete tasks only from the main thread, you do not need a service. Just create another thread.

AsyncTask behind the scenes just sends your task to the thread pool for execution. If you have many short tasks, such as parsing network traffic, AsyncTask great.

However, if you process a huge number of requests, you may need more control over the thread pool that performs your tasks.

+5
source
  • No

  • Since the main thread controls the user interface, while asynchronous operations can perform more difficult tasks, saving free time in the user interface.

  • No, but if you want your service to do a heavy lifting like downloading things from the Internet, then it should use asynthesis. Most services are used to download data from the Internet, so most of them have asynctasks. Please note that in order for the service to be kept alive after the activity dies, you must specify it. Services by default die with actions if they are not configured correctly

  • No

+1
source

You may be confusing the thread, task, and process. The task is a small process. A process is a program that runs in your system example when you start your task manager, shows the entire process as Internet Explorer, but a thread is a small easy process, you can say that a subprocess that is performed to perform a task, but asynchronous in android just looks like a stream, but it can be long. Take the example in android that you play in the temple on an Android phone, and someone calls you to perform a task with high priority, and the current thread pauses there and there are as many methods as onCreate (), onPause (), you can understand this.

0
source

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


All Articles