java 如何让应用主线程进入睡眠状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6092093/
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-10-30 14:17:47  来源:igfitidea点击:

How to put an app main thread to sleep?

javaandroidmultithreading

提问by pharaoh

I've a current problem : I've a app and want to put the main thread to sleep for 1500ms show a I can show a ProgressDialog, and I don't know how to do it. It's possible to stop an app main thread? I can achieve the same goal using another thread right?

我目前有一个问题:我有一个应用程序,想让主线程休眠 1500 毫秒,显示我可以显示 ProgressDialog,但我不知道该怎么做。可以停止应用程序主线程吗?我可以使用另一个线程实现相同的目标,对吗?

回答by Carsten

Stopping the main thread is usually a bad idea as it would also stop all UI interaction processing.

停止主线程通常是一个坏主意,因为它还会停止所有 UI 交互处理。

Try making the progress dialog modal. This will prevent the user from doing anything on your UI until the dialog is gone. I think this will achieve what you want to do and is compatible with how Android works

尝试使进度对话框模式化。这将阻止用户在您的 UI 上做任何事情,直到对话框消失。我认为这将实现您想要做的事情并且与 Android 的工作方式兼容

回答by Eshaan

not a good idea to do this, but if you have to use this

这样做不是一个好主意,但如果你必须使用它

try {
    Thread.sleep(1500);
} catch (InterruptedException e) {
    //handle
}

回答by Alex Gitelman

Don't stop main UI thread! It will freeze UI. I can imagine that you show progress dialog during some background operation. Once this operation is complete just post something to update UI via handler.

不要停止主 UI 线程!它会冻结用户界面。我可以想象您在某些后台操作期间显示进度对话框。完成此操作后,只需发布​​一些内容即可通过处理程序更新 UI。

The point of progress dialog is to interact with user while something long is executing. Otherwise you would not even need background operation and progress dialog. You would just do you operation in main UI thread and wait for UI to unfreeze.

进度对话框的重点是在执行较长的操作时与用户进行交互。否则你甚至不需要后台操作和进度对话框。您只需在主 UI 线程中进行操作并等待 UI 解冻。

回答by Maximus

What you're asking for should be unnecessary and as Carsten said, is a bad idea. It's a cardinal rule that you both never interrupt the UI thread and also only update elements of the UI on that thread. While a Dialog and any of it's subclasses are shown, nothing in the background will receive any input, so while it's up... though the main thread is running, nothing should be happening besides what is going on in the Dialog.

你所要求的应该是不必要的,正如卡斯滕所说,这是一个坏主意。一条基本规则是,您永远不要中断 UI 线程,并且只更新该线程上的 UI 元素。虽然显示了 Dialog 及其任何子类,但后台中的任何内容都不会收到任何输入,因此当它启动时……尽管主线程正在运行,但除了 Dialog 中正在发生的事情之外,什么都不应该发生。

What you're probably looking to do is use something like an AsyncTask() to show your ProgressDialog, do some work, then dismiss it when that work is done.

您可能想要做的是使用 AsyncTask() 之类的东西来显示您的 ProgressDialog,做一些工作,然后在工作完成后关闭它。

回答by Maximus

To do something like this, you'll want to use "Runnables" along with a "Handler." As others mentioned, you don't want to stop the main UI thread. Since there is only one, you won't be showing ANY updates if you make it sleep or wait. See a small sample below of running code on another thread.

要执行此类操作,您需要将“Runnables”与“Handler”一起使用。正如其他人提到的,您不想停止主 UI 线程。由于只有一个,如果您让它休眠或等待,您将不会显示任何更新。请参阅下面在另一个线程上运行代码的小示例。

The main thing to take out of this is that sleeping the main thread means stopping any and all visual updates.

要解决的主要问题是休眠主线程意味着停止任何和所有视觉更新。

// Class Variables
private Handler mHandler;

@Override
public void onCreate(){
    // Create a Handler Object
    // In Layman's terms, Handlers can run processes off the main
    //    User interface thread. 
    mHandler = new Handler();

    // Post a "Runnable" after a delay in milliseconds
    // On a separate thread, the progressDialog_Runnable
    //    will call the function "createProgressDialog"
    mHandler.postDelayed(progressDialog_Runnable, 250); 
}

// Runnables (Process to run on separate thread)
private Runnable progressDialog_Runnable;
{
    progressDialog_Runnable = new Runnable(){
        @Override
        public void run() {
            createProgressDialog();
        }
    };  
}

// Method you create to make the progress dialog
private void createProgressDialog(){
    //... Run this code on a separate thread via Runnable
}

回答by Adobels

Sleep 10s on a thread in Kotlin

在 Kotlin 的线程上睡眠 10 秒

try {
    Thread.sleep(10000)
} catch (e: InterruptedException) {
    //handle
}