0

A worker thread is used to process a very long-running task. At the middle of task execution, a user interaction is needed to acquire inputs. As the inputs are supplied, the worker thread will resume execution. My problem is, the worker thread needs to be suspended, get inputs (shall we say through a dialog box - I am using the Dialogs API of JDK8u40 which must be facilitated by the JavaFX App Thread), and resume thereafter. The inputs are not supplied at the start due to dependency of certain situations, and inputs might be needed many times.

A typical example is, files are being copied from one directory to another. As the files are copied, a file with the same file name exists in the destination directory, thus a user interaction is needed to rename the file or skip file copying to avoid file name conflict. In this scenario, the worker thread is supposed to execute file copying, and is needed to be suspended to acquire inputs from user (must be facilitated by the JavaFX App Thread) before proceeding. Platform.runlater(() -> {}); can't do this kind of situation, for it would just queued the Runnable object to be ran at some time in the future.

How to facilitate this scenario? I am new to JavaFX concurrency.

Warren Nocos
  • 1,222
  • 1
  • 14
  • 29

1 Answers1

1

You can use the wait notify mechanism as described here: http://www.avajava.com/tutorials/lessons/how-do-i-use-the-wait-and-notify-methods.html. When an interaction is needed a runnable is launched through ui thread to prompt the user. The worker thread calls wait. The ui thread eventually gets the user input and notifies the working thread which continues his job according to the message it receives. The difference here is that you do not need to create two threads as the ui thread already exists.

Tarik
  • 10,810
  • 2
  • 26
  • 40