0

I want to automate an online game. It worked while it was only terminal based. But I wanted to add an JavaFX UI and now I don't know where to put Thread.waits until a certain LocalDateTime is reached. Where does JavaFX wait for it's events (like button clicks, ...) and can I overwrite that method? Or is there a way to create custom LocalDateTime events and listeners for javaFX?

If i add a Thread.wait in initialize() the whole scene doesn't appear until that certain LocalDateTime is reached.

  • Never use `LocalDateTime` to track a moment. Read the class doc. For a moment, use `Instant`, `OffsetDateTime`, or `ZonedDateTime`. – Basil Bourque Apr 20 '19 at 18:35

1 Answers1

2

If you want to have an event fire at some predetermined moment, but you want the user-interface of the app to remain responsive to the user’s actions, then you need a background thread. You should almost never sleep the GUI thread of your app, as that makes your app unresponsive and appear to be crashed. For fun, try it the wrong way: Drop a Thread.sleep call into your GUI code to see how it temporarily freezes your app.

Learn about concurrency. Start with the Oracle Tutorial, and work your way up to eventually making an annual read of the Bible, Java Concurrency in Practice by Goetz et al.

The Executors framework was built to make threading easier. Specifically, you will want the ScheduledExecutorService class. This has been covered many many times already on Stack Overflow. Search to learn more.

And learn about how to properly interact with the GUI thread from a background thread.

Never use LocalDateTime to track a moment, as explained in the class doc. For a moment, use Instant, OffsetDateTime, or ZonedDateTime. For a span-of-time on the scale of hours-minutes-seconds, use Duration. The Duration class offers methods such as toMinutes, toSeconds, the results of which you can feed as your delay to a ScheduledExecutorService. All of these classes have been covered many many times on Stack Overflow. Search to learn more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • You might want to qualify `java.time.Duration` as JavaFX has its own `javafx.util.Duration` class and this question is tagged [tag:javafx]—I've seen a decent number of questions which get the two confused. – Slaw Apr 20 '19 at 19:37
  • Okay that's more complex than I thought haha. I think we will learn about multithreading and concurrency at the end of this semester. But thank you – Dardan Kryeziu Apr 20 '19 at 19:47
  • @DardanKryeziu If the delayed action is not too time consuming (executing long running tasks on the UI thread is just as bad as blocking it), then you can use the animation API to do what you want. See https://stackoverflow.com/questions/9966136/javafx-periodic-background-task – Slaw Apr 20 '19 at 19:52
  • @DardanKryeziu Yes, doing concurrency properly is a complicated process, tough to get right for even advanced programmers. But the *Executor* framework is a brilliant piece of work to vastly simplify handling threads. You can get far by searching Stack Overflow for examples. I, and others, have written complete working examples for you to follow. You still need concern yourself with (a) protecting resources or data shared between threads (background & GUI, in your case) if any resources or data are shared, and (b) not accessing the GUI elements from the background thread. – Basil Bourque Apr 20 '19 at 20:07