Is it possible to change/set name or title of javafx.concurrent.Task just like in Thread thread.setName("name") ?
2 Answers
I am guessing you haven't understood the concept of javafx.concurrent.Task
Task
and Threads
have the same difference, as a Runnable
and Thread
has.
Can you name a Runnable
object ?
Still confused ? Explanation on the background..
From the Docs
public abstract class Task<V>
extends java.util.concurrent.FutureTask<V>
implements Worker<V>, EventTarget
FutureTask
A FutureTask can be used to wrap a Callable or Runnable object. Because FutureTask implements Runnable, a FutureTask can be submitted to an Executor for execution.
Task
Likewise, since Task extends from FutureTask, it is very easy and natural to use a Task with the java concurrency Executor API. Since a Task is Runnable, you can also call it directly (by invoking the FutureTask.run() method) from another background thread. This allows for composition of work, or pass it to a new Thread constructed and executed manually.
So basically you will need a Thread
or Executor
to execute your Task
and I hope you already know how to name a Thread
or Threads spawned from Executors
;)
If not ..

- 1
- 1

- 36,135
- 10
- 122
- 176
javafx.concurrent.Task
has a title property which you can update from within the Task with the protected method updateTitle(String newTitle)
.
In response to Edv Beq's comment: from the [docs|https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html#updateTitle-java.lang.String-]:
updateTitle protected void updateTitle(String title)
Updates the title property. Calls to updateTitle are coalesced and run later on the FX application thread, so calls to updateTitle, even from the FX Application thread, may not necessarily result in immediate updates to this property, and intermediate title values may be coalesced to save on event notifications.
This method is safe to be called from any thread.
My hunch would be that the title update is being queued to the jfx thread, but you are trying to access it immediately, and not getting the result you expect. I know this wasn't the example you asked for, but I hope it helps.

- 164
- 1
- 8
-
Can you please show an example? I am calling "updateTitle("someTitle")" from withing the Task such as in the "succeeded()" method but when I try to print "t.getTitle().toString()" or "t.titleProperty().getValue()" - I don't get anything. This is after running the task. – Edv Beq Sep 19 '17 at 02:38
-
1my reply to @EdvBeq was too long so I added it to my answer – Jonathan Millman Sep 25 '17 at 10:52
-
I called updateTitle("title") from inside the @Override call of the Task and it worked great. Thank you Jonathan. – Edv Beq Sep 25 '17 at 10:54