-3

I'm having a trouble with Runnable and Thread implementations. I have this abstract class, that can not be modified:

abstract class Ordenador {

    ...

    protected Ordenador(String nombre, int[] array) {
        ...
    }

    protected void escribir() {
        ...
    }

    protected abstract void ordenar();

}

And this sort algorithm that inherit from the class above and implements the run() method, which call the sorting one.

class Burbuja extends Ordenador implements Runnable {

    protected Burbuja(String nombre, int[] array) {
        super(nombre, array);
    }

    protected void ordenar() {
        ....
    }

    public void esperar() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        this.ordenar();
    }
}

Finally I have my main class that creates a random array and create a new Burbuja object that sort the array. The problem is that when calling b.join() the array stay the same so de ordenar() method doesn't get called.

class Aplicacion {

    public static void main(String[] args) {
        ...

        Burbuja burbuja = new Burbuja("Burbuja", array);
        Thread b = new Thread(burbuja);
        ...

        try {
            b.join();
            s.join();
            ... more sorting algorithms...
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(-1);
        }

        System.out.println("");
        burbuja.escribir();   
    }
}

I tried modificating some parts of the code but doesn't work neither.

allnex
  • 335
  • 3
  • 12

2 Answers2

1

You have to call the start() method on your thread object

https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Camandros
  • 449
  • 6
  • 22
1

Your Thread b = new Thread(burbuja); is right, but you forget to call the start method, b.start();

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97