I've one thread, but i'd like to wait for it to finish before continue with the next actions. How could i do it?
new Thread(
new Runnable() {
@Override
public void run() {
mensaje = getFilesFromUrl(value);
}
}).start();
here i'd like to put something (but loops) that knows when thread finishes and evaluate the result (mensaje)
btnOk.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
lblEstado.setText(mensaje);
if(mensaje.equals("Importado"))
startActivity(new Intent(ScanUrl.this, MainActivity.class));
Create an Object called lock . Then after runOnUiThread(myRunnable); , you can call lock. wait() .
In between, we have also put the main thread to sleep by using TimeUnit. sleep() method. So the main thread can wait for some time and in the meantime, T1 will resume and complete its execution.
The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution.
for(int i = 0; i < 10; i++) { thread = new Thread(this); thread. start(); thread. join(); // Wait for it to finish. } This makes the main thread wait until the launched thread is finished.
Thread t = new Thread(new Runnable() { @Override public void run() { mensaje = getFilesFromUrl(value); }}); t.start(); // spawn thread t.join(); // wait for thread to finish
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With