The correct way to solve this is to extract the anonymous class that implements the thread so that you can reuse.
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 of how this can be implemented depending on the need.