Tasks and background updates
Some values change with time: a clock, a message that hides itself, a result that a worker thread computes. A task runs your code on the render thread after a delay or on a period, and a signal set from another thread hands its value to the interface at the next frame.
You will learn
- How to run code on a period with a task
- How to run code once after a delay, and restart that delay
- How long a task lives
- How to set a signal from another thread
Run code on a period
Section titled “Run code on a period”A Task runs its Task.onRun listeners once its delay has passed, and again after each period given to Task.every:
final IntegerSignal seconds = new IntegerSignal(0);Task.create(1000L).every(1000L).onRun(event -> seconds.increment()).attach(this);Task.create(delay) takes the delay in milliseconds, and every(period) the period in milliseconds; without every, the task runs once.
attach(ui) or attach(node) starts the delay, and the owner runs the task when it starts to draw a frame, after the update step, on the render thread, with UI.getCurrent set to its UI.
The time is the clock of JOID, which a snapshot test moves with its wait command.
A frame that comes late runs a due task once, never twice: a period counts from the run before.
Run code once, after a delay
Section titled “Run code once, after a delay”A one-shot task detaches itself after its run. Attaching it again starts its delay over, so one task hides a message three seconds after the last click, however many clicks came before:
final BooleanSignal saved = new BooleanSignal(false);final Task hide = Task.create(3000L).onRun(event -> saved.set(false));RectNode.create(60, 160, 260, 60).color(TimerUI.ACCENT).onClick(event -> { saved.set(true); hide.attach(this);}).label(Text.create("Save", label)).attach(card);Task.detach cancels a task before it runs, and stops a periodic one; a listener stops its own task with event.getTask().detach().
Know how long a task lives
Section titled “Know how long a task lives”A task runs while its owner is drawn:
- attached to a UI, while the UI is open and drawn;
UI.getTaskListlists them; - attached to a node, while the node is in an open UI: it does not run while the node is detached, and its delay starts over when the node joins a UI again.
A task attached in init() lasts one opening: closing the UI or reloading it detaches the task, and the next init() attaches a fresh one.
A task attached to the UI in its constructor comes back at each opening, with its delay started over.
Set a signal from another thread
Section titled “Set a signal from another thread”JOID draws and calls your listeners on the render thread (Threads). A download or a long computation belongs on another thread, and its result comes back through a signal:
RectNode.create(60, 60, 260, 60).color(LoaderUI.ACCENT).onClick(event -> { this.status.set("Loading..."); CompletableFuture.supplyAsync(this::fetch).thenAccept(this.status::set);}).label(Text.create("Load", label)).attach(card);private String fetch() { final long end = BridgeHandler.CLOCK.get().currentTimeMillis() + 1000L; try { while (BridgeHandler.CLOCK.get().currentTimeMillis() < end) { TimeUnit.MILLISECONDS.sleep(10L); } } catch (final InterruptedException exception) { Thread.currentThread().interrupt(); } return "Loaded";}fetch stands for a download: it waits one second of the JOID clock (BridgeHandler.CLOCK).
A set made on another thread does not change the signal at once: JOID applies it on the render thread at the start of the next frame, then runs the listeners there.
The writes of one thread apply in their order, an increment() applies to the value that the signal holds at that time, and the writes inside one batch apply together.
On the worker thread, the signal still holds its old value right after the set:
this.status.set("Loaded");this.status.peek(); // ✗ still "Loading..." on the worker thread, until the next frameHand the value itself to the code that needs it, and let the nodes read the signal.
A write from another thread reaches signals only: changing a node or opening a UI from there goes through ThreadUtils.runOnRenderThread (Threads).
- A
Taskruns itsonRunlisteners after its delay, once, or on the period ofevery, in milliseconds of the JOID clock. - A task runs on the render thread, while its UI or its node is drawn.
- Attaching a task again, or attaching its node to a UI again, starts its delay over;
detach()cancels it. - A task attached in
init()lasts one opening; one attached in the constructor comes back at each opening. - A
setfrom another thread applies at the next frame, on the render thread, in order.
Try it
Section titled “Try it”A countdown. Show “Starts in 3”, “Starts in 2”, “Starts in 1”, then “Go”.
Solution
A periodic task counts an IntegerSignal named left down from 3, and stops itself:
Task.create(1000L).every(1000L).onRun(event -> { if (left.decrement().peek() == 0) { event.getTask().detach(); }}).attach(this);Then show left.map(value -> value > 0 ? "Starts in " + value : "Go").
A status from a thread. A thread of yours parses a file and must show “Parsed 120 lines”. What does the thread call?
Solution
status.set("Parsed " + count + " lines") on a signal that a text node follows: JOID applies it at the next frame.
