如何创建一个线程安全的ContentProvider? [英] How to create a Thread-safe ContentProvider?
问题描述
Android的文件说,
ContentProvider的方法可以从各种ContentResolver的被称为 在不同的进程和线程的目的,它们必须实施 在一个线程安全的方式
和我发现这个职位上#1 <一href="http://stackoverflow.com/questions/6912793/android-sqlite-content-providers-and-multithreading">Android - sqlite的内容提供商和多线程 它说,这是线程安全已??
所以,只是想知道如何创建一个线程安全的的ContentProvider
?它是不够的,如果我做的插入/更新/删除方法 syncronized
公共同步开放的插入(URI URI,ContentValues值){
}
您可以让每一个方法同步
,但要确保它在此之前是绝对必要的。在情况下,底层数据源已经是线程安全的决策同步的方法
可能是昂贵的。请参阅我的<一个href="http://www.androiddesignpatterns.com/2012/10/sqlite-contentprovider-thread-safety.html">blog交 关于此主题的更多信息。
Android documentation says
ContentProvider methods can be called from various ContentResolver objects in different processes and threads, they must be implemented in a thread-safe manner
And I found this post on Stackoverflow Android - sqlite content providers and multithreading which says it's thread safe already ??
So, Just wondering how to create a thread-safe ContentProvider
? Is it enough if I make the insert/update/delete methods syncronized
public synchronized Uri insert (Uri uri, ContentValues values) {
}
You could make every method synchronized
, but make sure it is absolutely necessary before you do. In cases where the underlying data source is already thread-safe making the methods synchronized
could be costly. See my blog post on this topic for more information.
这篇关于如何创建一个线程安全的ContentProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!