Search code examples
androidmultithreadingwait

using wait and notify within one thread


I have an app that does the next thing: receives GPS data through DDMS and stores them in a database and while the data are stored in the database I should also start a client thread that reads the new data stored in the database and sends it to a remote server!!!

In order to receive GPS data I do something like this:

 lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


  locationListener = new MyLocationListener();


   lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

And in my LocationChanged method I insert the GPS data in a database:

private class MyLocationListener implements LocationListener 

    {
        @Override

        public void onLocationChanged(Location loc) {

            if (loc != null) {

                latitude=(int)(loc.getLatitude()* 1E6);

                longitude=(int)(loc.getLongitude()* 1E6);

              db.insertData1(longitude,latitude);
           }

        }

And now my problem is:

How/where should I start the client thread....which reads the database?

It first tried to start the client thread right after this line:

m.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

But if I do that I have the risk to get a force close cause the Client thread reads the database which may be empty.

How should I know when to start reading the database?

Should I use the protocol wait/notify for the client thread so while I get GPS update I read the database???? Who should I implement wait/notify within one thread??Thx...I'm here for further questions:)


Solution

  • wait/notify is for synchronizing access to shared data when multiple threads access it concurrently. It does not apply in your case.

    What you need is to simply check if database exists before you start reading it: Query if Android database exists!