Massimo Bührer
Massimo Bührer

Reputation: 3

Pausing a For-Loop/better Ideas how to solve this

I have following problem: I need to start many processes and let it calculate as background threads. If one Process has finished I want to update a BubbleChart. I started a for loop which creates a new thread which then starts my Process. My problem is that I can't seem to stop or pause the loop because I either can start one process or every proccess at once (which is very hard for my system to handle)

Here's some code I have

for (int i = 0; i < getAllVariantsCount(); i++ ) {
        CalculatingFaRAO calculatingFaRAO = new CalculatingFaRAO(
                choiceBoxSelectShip.getValue(),
                tempPathFolder,
                coreConfiguredFilesArray.get(i),
                waterwayNamesArray.get(i),
                getAllVariantsCount(),
                i);
        Thread calculatingFaRAOThread = new Thread(calculatingFaRAO);
        calculatingFaRAOThread.start();



        calculatingFaRAO.setOnFailed(value -> {
            showErrorMessage(calculatingFaRAO.getException());
        });

        calculatingFaRAO.setOnSucceeded(event -> {});

        progressBar.progressProperty().bind(calculatingFaRAO.progressProperty());



        cancelButton.setOnAction(event -> {
            calculatingFaRAO.cancel(true);
            primaryStage.setMinHeight(415);
            primaryStage.setMinWidth(500);
            primaryStage.centerOnScreen();
            start(primaryStage);
        });

        stopButton.setOnAction(event -> {
            calculatingFaRAO.cancel(true);
        });


    }

In my CalculatingFaRAO Class I just pass the arguments to a ProcessBuilder

I hope someone can help me out with this. I don't have to stick to this code design it was just my only solution for this.

Just ask if you need to know anything else.

Upvotes: 0

Views: 71

Answers (2)

mdrg89
mdrg89

Reputation: 61

I will answer you from my own experiece, if I'm mistake, please, correct me.

First, I would recomended you to use Service Class instead Thread Class, because Service will give you more control over the process, its more safe and allow you stop, start, restart, pause, etc.

Second, on your code you are trying set events on a Task, and I think that should been set over a Thread or Service. And the bind on progressBar must be with a Thread o Service too.

Third, those events must be declared before the start event, if not, it will no have effect on the execution.

And finaly, you use a only one progressBar for all process, I dont think that will work good. You can try declare a new progressBar for each process, or, use a While loop and any flag/signal to launch each process one by one, like a stack.

Upvotes: 1

Thomas
Thomas

Reputation: 88707

I'm not quite sure I understand your requirements/problem but I'll try, so let me rephrase:

You want to run a number of (independent?) processes concurrently but want to limit that number to a reasonable amount. When a process finishes you want to update the chart and start another process if there's one left.

If that's true then you might consider using a threadpool (e.g. ThreadPoolExecutor) instead: create tasks/runnables for all the processes you need and feed them to the pool. The pool would then run as many tasks in parallel as it has threads while the others are sitting in a queue waiting to get executed.

The updates to the chart could then be triggered by the tasks themselves.

Upvotes: 3

Related Questions