简体   繁体   English

Java GUI,需要暂停一个方法而不冻结GUI

[英]Java GUI, need to pause a method without freezing GUI aswell

I know that this problem is caused by the sleep or wait calling on the main thread and that the answer on how to solve this will be to put the method into a seperate thread and then make that thread sleep. 我知道这个问题是由主线程上的睡眠或等待调用引起的,关于如何解决这个问题的答案是将方法放入一个单独的线程然后使该线程进入休眠状态。 But the code is a mess and don't really have the time to sort it out and split it up into separate threads and was wondering if there is any other way of doing this? 但代码是一团糟,并没有真正的时间来解决它并将其拆分为单独的线程,并想知道是否有任何其他方式这样做? Even if it is not the cleanest or most common practice for working with GUIs. 即使它不是使用GUI的最干净或最常见的做法。 I only need about a seconds pause from the method. 我只需要从方法中暂停几秒钟。

You can't do it without creating a separate thread. 如果不创建单独的线程,则无法执行此操作。 Creating a thread in Java is easy. 用Java创建线程很容易。 The only thing to pay attention to is that you can only touch the UI through the main thread. 唯一需要注意的是,您只能通过主线程触摸UI。 For this reason you need to use something like SwingUtilities.invokeLater() . 因此,您需要使用SwingUtilities.invokeLater()之类的东西。

It is not possible to sleep on an event thread and not cause a GUI freeze. 无法在事件线程上休眠并且不会导致GUI冻结。 However in Swing, the event thread is created and managed behind the scenes - you main thread (the one originating from main() method) is not the event thread. 但是在Swing中,事件线程是在幕后创建和管理的 - 您的主线程(源自main()方法的线程) 不是事件线程。

So, you can safely sleep on your main thread. 所以,你可以安全地睡在主线程上。

I wrote a super simple delay function for java that doesn't let your GUI freeze . 我为java编写了一个超级简单的延迟函数,它不会让你的GUI冻结。 It has worked everytime i have used it and i guess it will work for you too. 它每次使用它都有效,我想它也适合你。

     void Delay(Long ms){

       Long dietime = System.currentTimeMillis()+ms;
       while(System.currentTimeMillis()<dietime){
           //do nothing
       }
   }

For eg : To delay a thread by 5 millisecods use Delay(5L) 例如:要将线程延迟5毫秒,请使用延迟(5L)

Using a separate thread for the code is your only solution. 使用单独的代码线程是您唯一的解决方案。 Every action started by the Swing thread must be delegated to a separate thread if it would otherwise block the GUI. 必须将Swing线程启动的每个操作委托给一个单独的线程,否则会阻止GUI。

And where would one declare this thread. 一个人会在哪里声明这个帖子。 Please bear in mind any, any reference to a function that contains thread sleep will cause the main thread to pause. 请记住,任何对包含线程休眠的函数的引用都会导致主线程暂停。 Because the main thread will have to wait the the sub thread to pause. 因为主线程必须等待子线程暂停。

The reality is that threads don't realy work as seperate independant thread because a thread must be started from another thread. 实际情况是线程不能作为单独的独立线程工作,因为线程必须从另一个线程启动。 In other words if you are creating desktop application, and even if you don't work with other threads, your application is a one threaded application. 换句话说,如果您正在创建桌面应用程序,即使您不使用其他线程,您的应用程序也是一个单线程应用程序。 Now if you start working with threads & putting them to sleep, you will soon find out that that you won't be able to do anything else in the application. 现在,如果您开始使用线程并让它们进入睡眠状态,您很快就会发现您将无法在应用程序中执行任何其他操作。 No & no the other threads won't even run because they are waiting the first thread to finish sleeping. 否,没有其他线程甚至不会运行,因为他们正在等待第一个线程完成睡眠。 Why is this? 为什么是这样? Cause the thread are sub threads of the main thread and it is the main thread that is waiting for that sleep sub thread to wake up. 导致该线程是主线程的子线程,它是等待该睡眠子线程唤醒的主线程。 You can't design a threadless application either as java is single main threaded application. 您无法设计无线应用程序,因为java是单主线程应用程序。 Any, yes any, thread further defined in your application always runs inside in the main thread. 在您的应用程序中进一步定义的任何,是的任何线程始终在主线程中运行。

Unless somebody can prove me wrong, you obviously whould never pause your main thread as this would lock up your app. 除非有人能证明我错了,否则你显然永远不会暂停你的主线程,因为这会锁定你的应用程序。 However as soon you define another thread and suspend it with sleep() this also locks up your app as the thread was defined in subclass of the main application and therefore the main thread. 但是,一旦你定义了另一个线程并使用sleep()暂停它,这也会锁定你的应用程序,因为线程是在主应用程序的子类中定义的,因此是主线程。

So to put a very very long story to bed, pausing a user defined thread, is almost exactly the same as if your called the Thread.sleep() for anywhere in your app, it pauses the entire application. 因此,暂停一个非常长的故事,暂停用户定义的线程,几乎与您在应用程序中的任何位置调用Thread.sleep()完全相同,它会暂停整个应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM