0

I am working on a JavaFx project in which there is a sleep in look method like this :

 public void run() {
        while (true) {
                try {
                    method1()
                    Thread.sleep(2000);
                    Runnable1
                    Thread.sleep(10000);
                    if (condition 1){
                       method2
                       Thread.sleep(10000);
                    }else{
                       Runnable3
                       Thread.sleep(20000);
                    }
                    switch ()
                    case 1
                        Runnable 1
                       Thread.sleep(12000);
                    case 2
                      Runnable 4  
                      Thread.sleep(12000);
                    case 3
                     Thread.sleep(5000);
                    }
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                    break;
                }
        }

The problem is that the "case when, and if else" succession is very complex (around 20 branches).
Is there a better way to do that than to use always "Thread.sleep".

Nimantha
  • 6,405
  • 6
  • 28
  • 69
C Morell
  • 81
  • 1
  • 3
  • 10
  • 1
    (As an aside: you don't need to catch `InterruptedException` inside the loop and then break: just catch it outside the loop) – Andy Turner Apr 13 '21 at 08:50
  • as-is, this is unrelated to javafx (except you mentioning it ;) – kleopatra Apr 13 '21 at 09:41
  • Seeing this code snippet in conjunction with JavaFX almost gave me a heart attack ;-( – mipa Apr 13 '21 at 11:51
  • Please see: https://stackoverflow.com/a/60685975/1043824 What you want to achieve is fairly common, BUT unfortunately not easy. If this does not solve your case, update your question with what you tried and what did or did not work. – inquisitive Apr 13 '21 at 12:42

1 Answers1

1

There is almost always a better way than Thread.sleep.

It's very brittle, because there is no guarantee the "thing" happens within that sleep time, unless you crank up the sleep time so much that you are waiting unnecessarily long (and even then, there is no guarantee).

You are presumably waiting for some condition to change; so perhaps look at Condition; or look at something like Future (incl CompletableFuture) to do something when a previous task completes.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • It is a javafx program. The idea is to update data regularly, depending on the screen is currently use. First I do general tasks, and then I do the task that concern the current screen, after general tasks, after current screen task, etc... – C Morell Apr 13 '21 at 09:16
  • Just make sure that you're not using Thread.sleep() inside the JavaFX thread. That will freeze your gui while the thread sleeps. – NomadMaker Apr 13 '21 at 10:22
  • @CMorell I'm not sure what you're telling me: are you saying that the proposed approaches wouldn't work because it's a Javafx program? – Andy Turner Apr 13 '21 at 10:24