开发者

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:)


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!


It sounds like you are trying to use wat / notify as a general purpose way to pass messages. it's not.

If you truly want B to run only after A is finished, then do that. run A then B on your main thread. no need to mess w/ synchronization / wait / notify.

If A and B can run simultaneously, create a lock object that's shared between the DB thread and the send thread,

Object lock = new Object();

In each of the threads, synchronize your operations on this object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜