Starting a runnable in background thread

I have, to my knowledge, implemented a runnable which is created on a new thread. However, the thread does not seem to be running in the background, and the actions taken inside the runnable are stalling the UI with heavy actions.

See below:

  • Set FAB (Floating Action Button) above keyboard
  • Android - How Do I Set A Preference In Code
  • Making a multipart post request with compressed jpeg byte array with spring for android
  • How to find a word in large word list (vocabulary) with descent memory consumption and look-up time?
  • Can't build and run an android test project created using “ant create test-project” when tested project has jars in libs directory
  • Always fade in from placeholder with Picasso
  • custListLoadThread = new Thread(loadRunnable);
    custListLoadThread.run();
    
    private Runnable loadRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
    
            Gen.popup("TEST"); // Creates a toast pop-up.
            // This is to know if this runnable is running on UI thread or not!
    
            try
            {               
                customers = Db.BasicArrays.getCustomers(CustomApp.Session.businessCode, empId);
    
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        populate();
                        setCustListVisible(true);
                        loading = false;
                    }
                });
            }
            catch (final Exception ex)
            {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gen.popup(ex.getMessage());
                    }
                });
            }
        }
    };
    

    However this code does not run in the background, it still seems to run on the UI thread. I have placed the line Gen.popup("TEST"); to make sure of this (calling a toast pop up in a non-UI thread should cause an error).

    Any ideas as to why this runnable isn’t running in the background?

    Related posts:

    Why does the spotlight in my three.js scene remain centered in the camera perspective, but only on C...
    Android Dex: UNEXPECTED TOP-LEVEL EXCEPTION: Already added
    Implement two interfaces in an anonymous class
    Disable TabLayout
    Start Activity Using Custom Action
    Is android:exported=“true” really necessary for an authentication service?
  • How to mute camera shutter sound on android phone
  • Error inflating class com.facebook.widget.LoginButton with Facebook SDK 4.0.1
  • How do i dynamically choose which activity to launch when opening an app
  • Android Lollipop Set Status Bar Text Color
  • android:how to set text view as bold by coding not via XML
  • View caching for Headers and footers in a list view
  • 2 Solutions collect form web for “Starting a runnable in background thread”

    custListLoadThread = new Thread(loadRunnable);
    custListLoadThread.start();
    

    You need to start the thread, not call the run() method in the current thread.

    If you want to execute code on a background thread that does not do something with the UI:

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //your action
            }
        };
        AsyncTask.execute(runnable);
    

    Of course as written before you can also create a new thread (that is independent from the UI thread):

        new Thread(runnable).start();
    

    In your example you want to update UI elements, so better use a AsyncTask (must be called from UI thread!):

        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                // your async action
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                // update the UI (this is executed on UI thread)
                super.onPostExecute(aVoid);
            }
        }.execute();
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.