在当前线程上调用 sleep 会停止我的主 GUI

发布于 2024-08-17 07:37:27 字数 744 浏览 6 评论 0原文

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

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

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

我按下 main_GUI 上的按钮时,GUI 停滞了 28000 毫秒。 如果我在 WriteThread 上调用 sleep,那么为什么它会停止 main_GUI? 是因为 sleep 是静态方法吗?如果是的话,有人可以建议如何在不影响 Main_GUI 的情况下休眠 WriteThread 吗?

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.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆离笙 2024-08-24 07:37:27

如果您从 actionPerformed 方法中调用 WriteThread.sleepForReset(),那么您实际上是从 EventDispatch Thread 中调用它。即使您有一个代表 WriteThread 的对象,从 EventDispatch Thread 内调用该对象上的方法也会导致该过程在 EDT 内执行。

查看本教程,了解如何在爪哇。请注意,仅仅拥有一个继承自 Thread 的类是不够的,您需要启动这些 Thread 的实例才能拥有多线程应用程序。提供的教程将为您提供一种入门方法的好主意。

If you are calling WriteThread.sleepForReset() from within your actionPerformed method, you are actually calling it from within the EventDispatch Thread. Even though you have an object that represents your WriteThread, calling a method on that object from within the EventDispatch Thread will cause the process to be performed within the EDT.

Take a look at this tutorial on using worker threads in Java. Note that it isnt enough to just have a class that inherits from Thread, you need to have the instances of these Threads to be started for you to have a multithreaded app. The tutorial provided will give you a good idea on one way to get started.

顾铮苏瑾 2024-08-24 07:37:27

我认为称人们为极客不一定是获得帮助的好方法,但要回答您的问题,您会看到睡眠,因为您在事件调度线程上调用 Thread.sleep 。 Swing 的 GUI 操作运行在这个单线程上。如果您需要执行长时间运行的后台工作,则应将其委托给 SwingWorker。

此外,中断异常的处理也不是很稳健。至少,您应该使用 Thread.currentThread().interrupt() 重新中断线程。

I think calling people Geeks is not necessarily a good way to get help, but to answer your question, you're seeing the sleep because you're calling Thread.sleep on the event dispatch thread. Swing's GUI operations run on this single thread. If you need to do long running background work, you should delegate it to a SwingWorker.

Also, the handling of interrupted exception is not very robust. At the very least, you should be reinterrupting the thread with Thread.currentThread().interrupt().

幸福还没到 2024-08-24 07:37:27

谢谢大家给我建议不同的选择......
但我使用了 TimeTask.schedule() 并工作了。我安排了稍后的工作,而不是在当前线程中使用 sleep ......

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......

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文