Android 异步同步
我有一个通过许多 AsyncTasks 访问的 DBApdater 类。 DB 上定义的每个操作都必须调用 DBAdappter 类中编写的函数(称为 open、insert 或 delete),然后关闭 DB。如果一个异步对象通过 DbApater 的对象调用了 open,我如何防止第二个 AsyncTasks 调用 open,直到第一个 AsyncTask 调用关闭数据库。我可以使用像 public static Object myLock =new Object();
这样的锁吗?
并且在打开的mentod write中
public void open()
{
synchronizaed(myLock.getClass)
{///Open the DB
}
}
public void close()
{
synchronizaed(myLock.getClass)
{///close the DB
}
notify();
}
这段代码可以工作吗?基本上,调用类将获得 open 方法的锁,并仅在调用 close 时释放它。
亲切的问候,
穆罕默德·马丁
I have a DBApdater
class that is accessed through many AsyncTasks
. Each operation defined on the DB has to call a function written in the DBAdappter
class called open, insert or delete from the db, and then close the DB. If one Async object has called open through an object of the DbApater
how can i prevent a second AsyncTasks
to call open until the first AsyncTask
has called close on the DB. Could i use a lock like public static Object myLock =new Object();
and in the open mentod write
public void open()
{
synchronizaed(myLock.getClass)
{///Open the DB
}
}
public void close()
{
synchronizaed(myLock.getClass)
{///close the DB
}
notify();
}
would this code work. Basically the calling class would obtain a lock on the open menthod and release it only when close is called.
Kind Regards,
Muhammad Mateen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,通过 open() 和 close() 方法同步对数据库的访问是正确的方法。这应该有效。
Synchronizing the access to database via
open()
andclose()
methods is the right approach in my mind. This should work.我不熟悉 Android 开发中线程安全通信的具体细节,但从 C# 开发人员的角度来看您的建议是有道理的。
I am not familiar with the specifics of thread safe communication in an android development, but looking at your suggestion from a C# developers point of view it makes sense.