java - 我在使用 sleep 时遇到问题?

标签 java android multithreading

为什么在我的代码中,TextView 除了最后一次计数之外不进行计数。 结果是:counter= 4

int i =0;

while (i< 5) {
    try {
        Thread.sleep((i*1000));
        mText.setText("counter"+ i);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // mText does not show
    mText.setText("counter= "+ i);
    i = i +1;
} 

最佳答案

当应用程序 hibernate 时,您会阻塞 UI 线程,因此屏幕不会重新绘制,因此在循环结束之前您不会看到文本更改。

The UI thread

When an application is launched, the system creates a thread called "main" for the application. The main thread, also called the UI thread, is very important because it is in charge of dispatching the events to the appropriate widgets, including drawing events. It is also the thread where your application interacts with running components of the Android UI toolkit.

For instance, if you touch the a button on screen, the UI thread dispatches the touch event to the widget, which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread dequeues the request and notifies the widget to redraw itself.

This single-thread model can yield poor performance unless your application is implemented properly. Specifically, if everything is happening in a single thread, performing long operations such as network access or database queries on the UI thread will block the whole user interface. No event can be dispatched, including drawing events, while the long operation is underway. From the user's perspective, the application appears hung. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.

If you want to see how bad this can look, write a simple application with a button that invokes Thread.sleep(2000) in its OnClickListener. The button will remain in its pressed state for about 2 seconds before going back to its normal state. When this happens, it is very easy for the user to perceive the application as slow.

To summarize, it's vital to the responsiveness of your application's UI to keep the UI thread unblocked. If you have long operations to perform, you should make sure to do them in extra threads (background or worker threads).

更多信息:
http://developer.android.com/resources/articles/painless-threading.html

这就是问题所在。 AsyncTask 是(一个)解决方案:

AsyncTask

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread.

http://developer.android.com/reference/android/os/AsyncTask.html

关于java - 我在使用 sleep 时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7175304/

相关文章:

android - 运行 Android Studio Default **Map Application** 时出错

c - 有信号和无信号的事件状态

java - run() 方法未执行

java - 将仿射变换应用于 opencv2 中的图像时出错

java - 如何在 Android 应用程序中从 Schoology 的 REST API 访问信息?

java - Gradle 测试从 XML 文件读取

java - 使用 java 线程间在控制台中进行文件传输

java - 是否可以在java中的if else语句中更新变量

android - 如何使用 Crosswalk XWalkView 以编程方式设置 cookie?

android - 在 fragment 中请求位置权限