I have an application where each process is called by a single method and this process needs to be executed on a thread. The threads code is identical in all, changing only the content of the run () method. I would not want to be copying and pasting the same code dozens of times. Is it possible to do something like 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;
}
}