0

I want to visualize my Heap sort algorithm using JavaFX, after each swap of the elements in my array, I also swap the values of the two corresponding bars in a barChart. The algorithm works fine, but when updating the GUI the runnable doesn't seem to block the next runnable for it's execution.

That's why my graph is messed up (values aren't swapped in order). See image below: "Sorted array"

Here is my runnable code snippet:

new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Await");
                try {
                    latch.await();
                } catch (InterruptedException ex) {

                }
                // queuing the done notification into the javafx thread
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        controller.updateChart(parentIndex, childIndex);
                    }
                });
            }
        }).start();

The latch is declared as class variable, and not modified somewhere else:

final CountDownLatch latch = new CountDownLatch(1);

here the code snippet the runnable is doing:

@Override
public void updateChart(int index1, int index2) {

    Object tempObject = series.getData().get(index2);
    series.getData().set(index2, series.getData().get(index1));
    series.getData().set(index1, tempObject);
    barChartCopy.getData().setAll(series);
}

Any idea, why the latch isn't considered?

Alan
  • 589
  • 5
  • 29
  • We have no way of knowing without seeing how you modify the latch elsewhere. – Marv Sep 08 '19 at 14:48
  • I declared the latch as class variable (see updated code) – Alan Sep 08 '19 at 14:54
  • 1
    Please provide a [mre] demonstrating the issue. – Slaw Sep 08 '19 at 15:55
  • You never call `countDown()` form the code you show. Furthermore even if the code inside the `Runnable` passed to `runLater` did call the method on the `CountDownLatch`, the thread would wait forever, since the logic triggering the `countDown` call is not scheduled at the time you start waiting... Assuming multiple of these runnables decrement the same latch, this could also be problematic... – fabian Sep 08 '19 at 16:56
  • So where should I add the countdown @fabian – Alan Sep 08 '19 at 16:57
  • What are you trying to achieve? Pause the sorting algorithm until the GUI was notified of the change? – fabian Sep 08 '19 at 16:58
  • Exactly, swap the array then the bars on the Gui and then continue sorting @fabian – Alan Sep 08 '19 at 17:44
  • It's probably much simpler to calculate all the changes to the GUI before making any changes: Simply create a list where each element stores one set of values the GUI requires. Then use e.g. a `Timeline` to display the changes one by one. This way you get rid of multithreading completely (as your code is concerned) and it shouldn't make a difference in performance for reasonably small inputs... – fabian Sep 08 '19 at 18:12
  • Never done that, sorry for my noobish approaches but how would you do that? – Alan Sep 08 '19 at 18:31
  • possible duplicate: https://stackoverflow.com/questions/53573747/dynamic-bar-chart-in-java-fx/53579013#53579013 – SedJ601 Sep 09 '19 at 15:40

0 Answers0