Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for a thread before continue on Android

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));
like image 936
mesacuadrada Avatar asked May 18 '13 13:05

mesacuadrada


People also ask

How do you wait for a runnable to finish?

Create an Object called lock . Then after runOnUiThread(myRunnable); , you can call lock. wait() .

How do you make a thread wait for some time?

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.

Which function is used to start the next thread only after the previous thread is complete?

The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution.

Which statement instructs the current thread to wait until another thread is finished?

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.


1 Answers

Thread t = new Thread(new Runnable() {                         @Override                         public void run() {                             mensaje = getFilesFromUrl(value);                         }});  t.start(); // spawn thread  t.join();  // wait for thread to finish 
like image 65
xagyg Avatar answered Oct 02 '22 07:10

xagyg