用等待和一个线程内通知 [英] using wait and notify within one thread

查看:136
本文介绍了用等待和一个线程内通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,并在接下来的事情:通过DDMS,并将它们存储在数据库中接收GPS数据,并在数据被存储在数据库中,我也应该启动一个客户线程读出存储在数据库中的新的数据和它发送到远程服务器!!!

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!!!

为了接收GPS数据我做这样的事情:

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

在我LocationChanged方法我在一个数据库中插入GPS数据:

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?

我应该使用的协议等待/通知客户端线程因此,虽然我得到的GPS更新我读
数据库????
我应该和谁执行的等待/一个线程?? THX内通知......我在这里作进一步的问题:)

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.

您需要的是简单地检查,如果你开始阅读之前存在的数据库:查询,如果Android版数据库存在!

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

这篇关于用等待和一个线程内通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆