The correct way to resolve this is to extract the anonymous class that implements the thread so that you can reuse it.
Example:
class MinhaClasse {
//inner class
private Runnable insercao = new Runnable() {
public void run() {
//aqui ficam minhas inserções
}
};
//executa e continua
public void inserirAssincrono() {
new Thread(insercao).start();
}
//executa e espera
public void inserirSincrono() {
Thread t = new Thread(insercao);
t.start();
try {
t.join(); //espera a thread terminar antes de continuar
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
However, there are several possibilities for how this can be implemented depending on the need.