Java sleep()阻止主UI线程

Java sleep()阻止主UI线程,java,android,multithreading,interrupted-exception,Java,Android,Multithreading,Interrupted Exception,我有一条线索: private class MyThread extends Thread{ public void run(){ try { sleep(10000); Utils.stopTimer(); } catch (InterruptedException e) { Log.d(TAG, "interrupted"); } } } 然后我开始了线程

我有一条线索:

private class MyThread extends Thread{
    public void run(){
        try {
            sleep(10000);
            Utils.stopTimer();
        } catch (InterruptedException e) {
            Log.d(TAG, "interrupted");
        }
    }
}
然后我开始了线程。但线程正在阻止主UI线程,导致它不响应用户交互。

调用而不是myThread.run

执行后一种操作不会导致代码在不同的线程中执行,而只是调用当前线程(例如UI线程)上的run方法,就像任何其他普通方法调用一样

摘自链接文档:

start使该线程开始执行;Java虚拟机调用[在启动的线程中]run方法

调用而不是myThread.run

执行后一种操作不会导致代码在不同的线程中执行,而只是调用当前线程(例如UI线程)上的run方法,就像任何其他普通方法调用一样

摘自链接文档:

start使该线程开始执行;Java虚拟机调用[在启动的线程中]run方法


您需要调用thread.start来启动线程,当您调用该run方法时,该方法将自动执行

  Calling this thread will also blocks the UI thread, you need to call it Async Task or in runonuithread.

您需要调用thread.start来启动线程,当您调用该run方法时,该方法将自动执行

  Calling this thread will also blocks the UI thread, you need to call it Async Task or in runonuithread.

最好使用AsyncTask或Activity.RunOnUIThread执行此类代码。

最好使用AsyncTask或Activity.RunOnUIThread执行此类代码。

那么您的问题是什么?它根据线程概念工作,即Thread.sleep10000@这是因为this.sleep是静态方法Thread.sleep的别名,这将产生等效的语义。但是,我同意在类型上显式调用静态方法是更好的形式?您是调用start方法还是直接调用run方法?我同意。你的问题?那么你的问题是什么?它根据线程概念工作,即Thread.sleep10000@这是因为this.sleep是静态方法Thread.sleep的别名,这将产生等效的语义。但是,我同意在类型上显式调用静态方法是更好的形式?您是调用start方法还是直接调用run方法?我同意。你的问题?