4
I have an application where each process is called by a single method and this process needs to run in a thread.
The code of the threads is identical in all, changing only the content of the method run().
I wouldn’t want to keep copying and pasting dozens of times the same code.
It is possible to do something similar to the example below?
public void metodoComThread(? metodoPassadoComoParametro) //<-- Aqui passei o método!
{ 
    try {
        Thread t = new Thread(new Runnable() {
            public void run()
            {
                metodoPassadoComoParametro(); // <-- executando o método passado como parâmetro
            }
        });
        t.start();
        while (t.isAlive()) {
            try {
                //...
                // faz alguma coisa, atualiza um progressbar, etc
                //...
                Thread.sleep(100);              
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
    catch(Exception e) {
        return false;
    }
}
The kind of behavior you’re trying to get (updating a progressibar while running a background task) suggests that you should try using
AsyncTask.– Piovezan
@Piovezan , was just an example. But thanks for the tip! Another learning!
– AlexQL
Well, in Delphi (which is my area), you can send an anonymous method. I believe that in Java, there is something similar
– Victor Tadashi