JavaFX concurrent task design pattern for contunually returning ArrayLists
<p>I'm learning how to use <code>Task</code>s in JavaFX. I am trying to figure out the design pattern for a <code>Task</code> to continually update an <code>ArrayList</code> which is used by the Application thread.</p>

<p>Below is the code I have so far. It works but I want to make sure that I'm following best practices. Is this an appropriate design pattern? If not, can you suggest a better way? </p>

<p>Ultimately I am aiming to update a <code>Canvas</code> with pixel data generated by a long running <code>Task</code> but want to get the design pattern right on a simple case first.</p>

<pre><code>import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
import java.util.List;

interface Updateable {
    public void update(List&lt;Integer&gt; data);
}

public class TaskExample extends Application implements Updateable {

    private ListView&lt;Integer&gt; list_view = new ListView&lt;&gt;();

    public void update(List&lt;Integer&gt; data) {
        list_view.getItems().clear();
        list_view.getItems().addAll(data);  
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        MyTask task = new MyTask(this);

        Thread thread = new Thread(task);
        thread.setDaemon(true);     
        thread.start();

        primaryStage.setScene(new Scene(list_view));
        primaryStage.show();    

    }

    public static void main(String[] args) {
        launch(args);
    }
}

class MyTask extends Task&lt;Void&gt; {

    private ObservableList&lt;Integer&gt; data = FXCollections.observableArrayList();
    private Updateable ui;

    public MyTask(Updateable ui) {
        this.ui = ui;
        addListener();      
    }

    @Override
    protected Void call() throws InterruptedException {
        for (int i=0; i&lt;10; i++) {
            Thread.sleep(1000);
            data.add(i);
        }
        return null;
    }

    private void addListener() {
        data.addListener((ListChangeListener.Change&lt;? extends Integer&gt; c) -&gt; {
            Platform.runLater(() -&gt; {
                ui.update(data);
            });
        });
    };

}
</code></pre>

<h1>Updated</h1>

<p>Updated <code>MyTask</code> according to @James_D's comment</p>

<pre><code>class MyTask extends Task&lt;Void&gt; {

    private ObservableList&lt;Integer&gt; data = FXCollections.observableArrayList();
    private Updateable ui;

    public MyTask(Updateable ui) {
        this.ui = ui;
        addListener();      
    }

    @Override
    protected Void call() throws InterruptedException {
        for (int i=0; i&lt;10; i++) {
            Thread.sleep(1000);
            final int j = i;
            Platform.runLater(() -&gt; {
                data.add(j);
            });
        }
        return null;
    }

    private void addListener() {
        data.addListener((ListChangeListener.Change&lt;? extends Integer&gt; c) -&gt; {
            ui.update(data);
        });
    };

}
</code></pre>