Android 在后台线程中启动可运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24823807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:48:48  来源:igfitidea点击:

Starting a runnable in background thread

androidmultithreadingrunnable

提问by Mike Baxter

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.

据我所知,我已经实现了一个在新线程上创建的可运行对象。然而,该线程似乎并没有在后台运行,并且在 runnable 内部采取的操作正在通过大量操作拖延 UI。

See below:

见下文:

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 toastpop up in a non-UI thread should cause an error).

然而,这段代码不在后台运行,它似乎仍然在 UI 线程上运行。我已经放置了一行Gen.popup("TEST");来确保这一点(toast在非 UI 线程中调用弹出窗口应该会导致错误)。

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

关于为什么这个 runnable 不在后台运行的任何想法?

回答by Steve M

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

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

您需要启动线程,而不是在当前线程中调用 run() 方法。

回答by Timo B?hr

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

如果要在不使用 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):

当然之前写的你也可以新建一个线程(独立于UI线程):

    new Thread(runnable).start();

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

在您的示例中,您想要更新 UI 元素,因此最好使用AsyncTask(必须从 UI 线程调用!):

    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();