0

I have two processes "Loaddata(); Workoffline();", now I want the process Loaddata(); to run until it is completed, the application can switch to the next process Workoffline(); .But I do not know how to do, expect people to help me.

public void test(){    
            runOnUiThread(new Runnable() {

                @Override
                public synchronized void run() {

                    loaddata();
                    //your UI interaction code here

                }
            });
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    workoffline();
                }
            });
}
Takeshi Pham
  • 61
  • 1
  • 7

1 Answers1

0

- The best approach will be to use CountDownLatch in java.util.concurrent package.

- You can use CountDownLatch's method countDown() to notify the completion of the loaddata(), and then use await() method to allow the execution of workoffline().

- Another way is to use join()

Eg:

Thread t1 = new Thread(new Runnable(){

public void run(){

    handler.post(new Runnable(){   // Declare the Handler handler = new Handler();
                                   // in onCreate()
          public void run(){

              loaddata();
        }

  });
}

});

t1.start();

try{
t1.join();
}catch(InterruptedException ex){

}


Thread t2 = new Thread(new Runnable(){

public void run(){

    handler.post(new Runnable(){

          public void run(){

               workoffline();

        }

  });
}

});

t2.start();
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • thank you,when I did as your instructions but still no results.Here Loaddata function is responsible for writing data to the database, longer function Workoffline will read data from the database and displayed on the listview.but in listview remain empty, even though the data in the database – Takeshi Pham Dec 24 '12 at 02:47
  • You can try another way to do this, call `join()` on the thread that is reading data from the DataBase and then after that keep the statement that triggers the filling of listview. – Kumar Vivek Mitra Dec 25 '12 at 02:19
  • problem here is, loaddata call class asynctask (it running in the background), when t1.start it called class asynctask only, so that it runs background, and instantly switch to t2, when passed t2, in the database no data, so the application when retrieve data from the database to the listview display is blank – Takeshi Pham Dec 25 '12 at 04:40