allnex February 2016

run() method doesn't get called from a Runnable implementation

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.

Answers


Camandros February 2016

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

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


Xoce 웃 Пepeúpa February 2016

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

Post Status

Asked in February 2016
Viewed 2,165 times
Voted 14
Answered 2 times

Search




Leave an answer