dazito
dazito

Reputation: 7980

Return or get/assign data from a SwingWorker thread to a variable. Beginner

I'm playing with SwingWorker and learning from using it. So, I've this code:

ArrayList<Train> dataList = new ArrayList<>();
ArrayList<Train> proccessedData = new ArrayList<>();

String query = "my query, not relevant here";

dataList = myApi.getTrainData(connectServer(), query, false);

TestNewThread tpp = new TestNewThread(dataList, threshold, pairs, false);

tpp.execute();

My TestNewThread class is:

public class TestNewThread extends SwingWorker<List, String>{

    private ArrayList<Train> dataList;
    private int threshold
    private int pair
    private boolean processFurther) 

    MyAPI myApi = MyAPI.getInstance();

    public TestNewThread(ArrayList<Train> dataList, int threshold, int pair, boolean processFurther) {
        this.dataList = dataList;
        this.threshold = threshold
        this.pair = pair
        this.iprocessFurther)  = processFurther) 
    }

    @Override
    protected List doInBackground() throws Exception {

        ArrayList<Train> list;

        publish("Starting process");
        list = processingTrains(threshold, pair, dataList);
        publish("Process finished.");

        return list;
    }

    @Override
    protected void process(List<String> chunks) {
        String mostRecentValue = chunks.get(chunks.size() - 1);
        myApi.printPanel("\n"+mostRecentValue);
    }

    @Override
    protected void done() {
//        myApi .printPanel("\nALL DONE... !);
    }

Now what I need and I can't figure out how to do it is: On the first piace of code here, where I've tpp.execute(); I want it to return to an assigned variable. To something like this, note the commented section:

ArrayList resultsList = new ArrayList();
TestNewThread tpp = new TestNewThread(dataList, threshold, pairs, false);
resultsList = tpp.execute(); // I know I can't do this because .execute() is void but I want something like whatever the new thread returns to return to resultsList array.

If there is any typo or something on the code I'm sorry, I just made it now from the top of my head to explain my problem. I think you can get the idea from here.

Upvotes: 0

Views: 1312

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

The whole point of SwingWorker is to be able to do something asynchronously, without blocking the EDT thread. So you can't return anything from the tpp.execute(), because if you could, that would mean that the EDT would be blocked until the background task has finished, and the background task wouldn't be a background task anymore.

What you can do, though, is to pass a reference to the component using the SwingWorker, and invoke a method of this component from the done() method:

public class SomePanel extends JPanel {

    ...
    TestNewThread tpp = new TestNewThread(this, dataList, threshold, pairs, false);
    tpp.execute();

    ...

    public void backgroundTaskHasFinishedHereIsTheResult(List<Train> trains) {
        // TODO do whatever with the list of trains computed by the background task
    }
}

And in the SwingWorker subclass:

   private SomePanel somePanel;

   public TestNewThread(SomePanel somePanel, ArrayList<Train> dataList, int threshold, int pair, boolean processFurther) {
        this.somePanel = somePanel;
        ...
   }

   @Override
    protected void done() {
        List<Train> result = get();
        somePanel.backgroundTaskHasFinishedHereIsTheResult(result);
    }

Upvotes: 1

Related Questions