是否有greenDAO线程安全最佳实践? [英] Are there greenDAO thread safety best practices?

查看:510
本文介绍了是否有greenDAO线程安全最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 greenDAO ,到目前为止一切进展顺利.文档或网站(或任何地方(:)似乎没有涉及的一件事)是它如何处理线程安全.

I'm having a go with greenDAO and so far it's going pretty well. One thing that doesn't seem to be covered by the docs or website (or anywhere :( ) is how it handles thread safety.

我知道其他地方提到的基础知识,例如使用单个dao会话"(Android + SQLite的通用做法),并且我对Java内存模型非常了解.库内部甚至看起来是线程安全的,或者至少是出于这种目的而构建的.但是我所见不到的内容涵盖了这一点:

I know the basics mentioned elsewhere, like "use a single dao session" (general practice for Android + SQLite), and I understand the Java memory model quite well. The library internals even appear threadsafe, or at least built with that intention. But nothing I've seen covers this:

greenDAO默认情况下会缓存实体.这对于完全单线程的程序非常有用-透明并且可以在大多数情况下显着提高性能.但是如果我loadAll(),然后修改其中一个元素,我正在整个应用程序中全局修改同一对象.如果我在主线程上使用它(例如,用于显示),并在后台线程上更新数据库(正确且正确),那么除非特别注意,否则就会出现明显的线程问题.

greenDAO caches entities by default. This is excellent for a completely single-threaded program - transparent and a massive performance boost for most uses. But if I e.g. loadAll() and then modify one of the elements, I'm modifying the same object globally across my app. If I'm using it on the main thread (e.g. for display), and updating the DB on a background thread (as is right and proper), there are obvious threading problems unless extra care is taken.

greenDAO是否在后台进行任何操作以防止常见的应用程序级线程问题?例如,在UI线程中修改缓存的实体,同时将其保存在后台线程中(最好是它们不会交织!尤其是在修改列表时!)?除了一般的线程安全性问题(即greenDAO期望并很好地配合使用)以外,是否有任何最佳实践"可以防止这些行为?还是从多线程应用程序的安全角度来看,整个缓存是否存在致命缺陷?

Does greenDAO do anything "under the hood" to protect against common application-level threading problems? For example, modifying a cached entity in the UI thread while saving it in a background thread (better hope they don't interleave! especially when modifying a list!)? Are there any "best practices" to protect against them, beyond general thread safety concerns (i.e. something that greenDAO expects and works well with)? Or is the whole cache fatally flawed from a multithreaded-application safety standpoint?

推荐答案

我对greenDAO没有任何经验,但是这里的文档: http://greendao-orm.com/documentation/queries/

I've no experience with greenDAO but the documentation here: http://greendao-orm.com/documentation/queries/

说:

如果在多个线程中使用查询,则必须在查询上调用forCurrentThread()以获取当前线程的Query实例.从greenDAO 1.3开始,Query的对象实例绑定到它们自己的用于构建查询的线程.这样,您就可以安全地在Query对象上设置参数,而其他线程不能干涉.如果其他线程尝试在查询上设置参数或执行绑定到另一个线程的查询,则将引发异常.这样,您不需要同步的语句.实际上,您应该避免锁定,因为如果并发事务使用相同的Query对象,则可能导致死锁.

If you use queries in multiple threads, you must call forCurrentThread() on the query to get a Query instance for the current thread. Starting with greenDAO 1.3, object instances of Query are bound to their owning thread that build the query. This lets you safely set parameters on the Query object while other threads cannot interfere. If other threads try to set parameters on the query or execute the query bound to another thread, an exception will be thrown. Like this, you don’t need a synchronized statement. In fact you should avoid locking because this may lead to deadlocks if concurrent transactions use the same Query object.

为完全避免那些潜在的死锁,greenDAO 1.3引入了用于CurrentThread()的方法.这将返回查询的线程本地实例,可以在当前线程中安全使用该实例.每次调用forCurrentThread()时,都会使用构建器构建查询时将参数设置为初始参数.

To avoid those potential deadlocks completely, greenDAO 1.3 introduced the method forCurrentThread(). This will return a thread-local instance of the Query, which is safe to use in the current thread. Every time, forCurrentThread() is called, the parameters are set to the initial parameters at the time the query was built using its builder.

尽管据我所知,文档中没有明确提及有关多线程的任何内容,但是这显然已经得到了处理.这是指使用同一Query对象的多个线程,因此显然多个线程可以访问同一数据库.数据库和DAO处理并发访问当然是正常的,并且在这种情况下,有很多行之有效的缓存技术.

While so far as I can see the documentation doesn't explicitly say anything about multi threading other than this this seems pretty clear that it is handled. This is talking about multiple threads using the same Query object, so clearly multiple threads can access the same database. Certainly it's normal for databases and DAO to handle concurrent access and there are a lot of proven techniques for working with caches in this situation.

这篇关于是否有greenDAO线程安全最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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