How to Check If Running on UI Thread in Android

How to check if running on UI thread in Android?

Doesn't look like there is a method for that in the SDK. The check is in the ViewRoot class and is done by comparing Thread.currentThread() to a class member which is assigned in the constructor but never exposed.

If you really need this check you have several options to implement it:

  1. catch the android.view.ViewRoot$CalledFromWrongThreadException
  2. post a Runnable to a view and check Thread.currentThread()
  3. use a Handler to do the same

In general I think instead of checking whether you're on the correct thread, you should just make sure the code is always executed on the UI thread (using 2. or 3.).

How to check if current thread is not main thread

Looper.myLooper() == Looper.getMainLooper()

if this returns true, then you're on the UI thread!

check if a method was called on UI-Thread with robolectric

Answer refered from the following links :

How to check if current thread is not main thread

How to know if this thread is a UI Thread

1) Looper.myLooper() == Looper.getMainLooper()

2) Android app has only one UI thread, so you could somewhere in the Activity callback like onCreate() check and store its ID and later just compare that thread's ID to the stored one.

 mMainThreadId = Thread.currentThread().getId();

3) if you want to do something on the UI thread and have any reference to Activity by using

mActivity.runOnUiThread( new Runnable() {
@Override
public void run() {
...
}
});

Hope it helps

How can I find out which parts of my code are using the UI Thread the most?

First thing you can do is enable StrictMode. That will detect non-UI, potentially long operations running on the main thread.

Second thing you can do is profile the CPU, as stated above. You'll be able to see the activity per thread, so you can determine what is running where. In order to get accurate readings, don't run it from Android Studio. Run it directly from your code. The easiest way to do that is to use the Debug class, and start the sampling at the Application.onCreate && finish it when the app closes.

The third thing you can do is to observe the messages sent to the main thread looper and evaluate them. Pierre-Yves Ricau has a nice guide about this that you can adapt to your own processes.

After that, you can use custom approaches to evaluate critical parts of your code, like generating your own traces. Perfetto uses JSON files, so you can print logcat messages from the parts you suspect are causing trouble and parse those messages into a perfetto trace.

How to tell if code needs to be run on the UI thread

In general you should take guidance from the API documentation. For example the Activity.onCreate() explicitly states that:

This method must be called from the main thread of your app.

For the example you gave Activity.setRequestedOrientation() there is no explicit statement that the method should be called on a particular thread. Usually if threading is of concern the documentation will state that.

If you would prefer certainty then you called also call upon Activity.runOnUiThread()

Android basics: running code in the UI thread

None of those are precisely the same, though they will all have the same net effect.

The difference between the first and the second is that if you happen to be on the main application thread when executing the code, the first one (runOnUiThread()) will execute the Runnable immediately. The second one (post()) always puts the Runnable at the end of the event queue, even if you are already on the main application thread.

The third one, assuming you create and execute an instance of BackgroundTask, will waste a lot of time grabbing a thread out of the thread pool, to execute a default no-op doInBackground(), before eventually doing what amounts to a post(). This is by far the least efficient of the three. Use AsyncTask if you actually have work to do in a background thread, not just for the use of onPostExecute().

How to check if callback is executed in GUI Thread: Android

Simple way would be to just see what thread the ui is on with http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#currentThread()
Then check it again later in the callback.

Running a method on UI thread

If you're calling the same UI method repeatedly, you could streamline the client code by creating the Runnable in a method:

private void updateUi(final String message) {
runOnUiThread(new Runnable() {
public void run() {
ui.myMethod(message);
}
});
}

Then your client code would simply call updateUi("changetext"). (This code assumes that ui is final. If not, you could pass in a final reference.)

If you're calling a different UI method every time, this doesn't gain you anything as you'd need a separate update method for each UI method. Your existing code is as elegant as it gets.



Related Topics



Leave a reply



Submit