本文介绍了在当前线程上调用sleep使我的主GUI停滞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从线程继承的类DataThread.我正在使用两个DataThread对象ReadThreadWriteThread.我有另一个线程正在运行Main_GUI.

I have one class DataThread inherited from Thread.I am using two DataThread objects ReadThread and WriteThread.I have another thread where Main_GUI is running.

现在,当我按下main_GUI上的按钮时,它将调用方法x.method1(),然后此方法使用WriteThread方法WriteThread.sleepForReset().在

Now when I press a button on main_GUI it calls a method x.method1() and then this method uses the WriteThread method WriteThread.sleepForReset(). In

public void sleepForReset(){
    try {
        sleep(28000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

当我按下main_GUI上的按钮时,GUI停滞了28000毫秒.如果我在WriteThread上呼叫睡眠,为什么为什么要停止main_GUI?是因为睡眠是静态方法吗?如果是,那么有人可以建议如何在不影响Main_GUI的情况下使WriteThread入睡吗?

When I press the button on main_GUI the GUI stalls for 28000 miliseconds.If I am calling sleep on WriteThread then why it halts the main_GUI?Is it because the sleep is a static method? If yes can anybody suggest how to sleep the WriteThread without affecting Main_GUI?

推荐答案

感谢大家向我建议其他选项.....但是我使用了TimeTask.schedule()并开始工作.我安排了以后的工作,而不是在当前线程中使用睡眠……

Thanks all of you for suggesting me different options.....But I used TimeTask.schedule() and worked. I scheduled my job for later instead of using sleep in the current thread......

这篇关于在当前线程上调用sleep使我的主GUI停滞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 17:24