[ Will it cause any issues if I have a repeating thread going until the user quits the application? ]
Just as I wrote in the title: Right after launching the application if I start a thread which calls a usual method then waits for 10 seconds and this gets repeated till the user quits the app, will it cause any performance issues? Or will it have a negative impact on other Threads and AsyncTasks, which might have higher priority? Like, lets say I have another thread which is triggered by a button, and it has to be executed instantly even if the main repeating thread is going. Could someone introduce me a safe solution?
Thank you for your answers, I am going to provide you an example: I have a button which is expected to be pressed once, so I examine if the user has already pressed it or not every 10 seconds. But beside this ongoing thread the user is able to trigger other short threads like download a 100KB file in the background or load some data from an external database. So as I understand there will be no mess up and I can have that ongoing thread as long as I wish. (this is just an example, I intentionally do not use the listener of the button)
Answer 1
It won't have a negative impact on the user experience per se, since you're running it off the UI thread so it shouldn't block that (this is assuming your method does not do anything to mess up the experience).
As for making sure other threads run before it, you could set thread priority, but multi threading is a finicky thing to deal with, and there is pretty much nothing you can do to always have some other thread cut ahead in front of this one.
A lot of factors decide which thread gets executed at a particular instance, many of which are outside your control. The VM will try to keep execution as close as possible to your intentions, but it is not a guarantee. You should design your app in a way that it runs fine even if your threads are slightly out of sync.
Answer 2
Using an IntentService is the correct approach for what you want to do. It may or may not have a performance impact depending on the nature of your task.
From the docs:
The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask
Be weary of concurrent access and make sure the processes you're using are thread-safe.