Does a runnable in a service run on the UI thread
Solution 1
Docs:
A services runs in the same process as the application in which it is declared and in the main thread of that application,
Different thread:
Thread t = new Thread(new MyRunnable());
t.start();
UI/Service Thread:
Handler h = new Handler();
h.post(new MyRunnable());
Solution 2
No it is not part of UI thread
, I assume by Runnable
you mean a new thread that you execute by calling start()
.
Regardless if you start a new Thread
in a service
or activity
it will not be part of the UI thread (unless you call something like join()
)
Edit
Since you are running a Runnable
object with Handler
, so it depends on where you initialize your handler
. Service runs in the main thread
, so initializing the handler in a service or activity will make the code be posted to the UI thread
Note, you need a single Handler
object per your thread; so avoid creating a new one with every time e.g. (new Handler()).postDelayed(runnable, 1000);
should be avoided and instead handler.postDelayed(runnable, 1000);
where handler
is an instance variable initialized in your service/activity class
Related videos on Youtube
Johann
Medium Articles Creating responsive layouts with Jetpack Compose https://johannblake.medium.com/creating-responsive-layouts-using-jetpack-compose-7746ba42666c Create Bitmaps From Jetpack Composables https://proandroiddev.com/create-bitmaps-from-jetpack-composables-bdb2c95db51 Navigation with Animated Transitions Using Jetpack Compose https://proandroiddev.com/navigation-with-animated-transitions-using-jetpack-compose-daeb00d4fb45 In-App Testing For Android https://proandroiddev.com/in-app-testing-for-android-6f762bb97387 Github https://github.com/JohannBlake ANDROID DEVELOPMENT 10 years of native Android development with Kotlin, Java & Android Studio Design, develop, test and deploy Android applications Professionally looking UIs with Jetpack Compose & Material Design Develop using continuous integration APIs: Google Cloud Messaging, Google Maps, Google Drive, Gmail, OAuth Frameworks & Patterns: MVVM, RxJava, Dagger, Koin, Retrofit, Sqlite, Room, Realm, Crashlytics Communicate with web services via RESTful APIs Troubleshoot, optimize and performance tune Code versioning using Git and the Gitflow model Project management with Jira Agile development with Scrum WEB DEVELOPMENT 25 years of web development React, Material-UI Javascript, HTML5, CSS3, jQuery, ASP.NET, C#, Java, Servlets Microservices running on the Google Cloud Platform ELECTRON DEVELOPMENT 2 Years of Electron development Javascript, jQuery Node.js HTML5, CSS3 Published Apps Motel One https://play.google.com/store/apps/details?id=com.motelone.m NBC Sports https://play.google.com/store/apps/details?id=air.com.nbcuni.com.nbcsports.liveextra Telemundo Noticias https://play.google.com/store/apps/details?id=com.nbcuni.telemundo.noticiastelemundo TD Mobile Banking https://play.google.com/store/apps/details?id=com.td
Updated on June 04, 2022Comments
-
Johann almost 2 years
In Android, when I create a runnable inside a service and run it, while I realize it runs in its own thread, is this thread somehow part of the UI thread? In other words, if the runnable carried out a long process, would it affect the UI?
EDIT:
private class SomeRunnable implements Runnable { @Override public void run() { try { } } } SomeRunnable runnable = new SomeRunnable(); (new Handler()).postDelayed(runnable, 1000);
-
David Wasser about 11 yearsYour assumption "...while I realize it runs in its own thread..." is not true. A
Runnable
can run anywhere, it depends on where you run it. For example, if you simply create aHandler
in yourService
and then post yourRunnable
using theHandler
, it will run on the main (UI) thread. -
David Wasser about 11 yearsIf all you want to do is run the
Runnable
in a separate Thread, you don't needAsyncTask
. Just do:new Thread(new SomeRunnable()).start();
-
-
Johann about 11 yearsBut that refers to a service. My question is about launching a runnable from a service.
-
Johann about 11 yearsUser117 seems to indicate otherwise. Are you sure?
-
Johann about 11 yearsI see conflicting responses posted here. Some seem to say yes and others say no.
-
S.D. about 11 years@AndroidDev The first piece of code launches a
Runnable
on a separate thread, completely unrelated to UI thread. The second piece of code runs theRunnable
on same thread on which service is running, and it will effect UI thread. -
iTech about 11 yearsYour question needs more clarification, what exactly you mean by
Runnable
. I mentioned it clearly if you start a newThread
by callingstart()
, it will be different from theUI Thread
-
David Wasser about 11 years@AndroidDev Yes, because there are different ways to "create a
Runnable
". If you will update your post with the code you use to "create aRunnable
" then we can probably give you a better answer. -
Johann about 11 yearsSee the code I added above in my original post. There is no start.
-
David Wasser about 11 yearsSee my comment to your question. That
Runnable
runs on the main (UI) thread. -
Johann about 11 yearsI have never heard of running a Runnable with start. I am only familiar with runOnUiThread and using a Handler. I am familiar with starting Threads with start() however.
-
Johann about 11 yearsI am launching it with a Handler, so according to you, it must be running on the UI thread.
-
Johann about 11 yearsI don't use (new Handler()). I only posted that to show I use a Handler. I do in fact only create the handler once and it's a private member.
-
S.D. about 11 years@AndroidDev In case of handler, it will run on thread which executed
new Handler()
like you created handler inonBind()
of service, that handler points to service thread now.