14

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

}
Community
  • 1
  • 1
kakopappa
  • 5,023
  • 5
  • 54
  • 73
  • I have multiple threads inserting data into the same database/table – kakopappa Sep 20 '11 at 16:55
  • Please read through what other people wrote in: [What are the best practices for SQLite on Android?](http://stackoverflow.com/questions/2493331/what-are-the-best-practices-for-sqlite-on-android/3689883#3689883). It should clarify things for you. – JJD Nov 11 '12 at 19:14
  • Yeah but what if you have a multiple threads updating and deleting – JPM Nov 13 '12 at 15:47

1 Answers1

14

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • 1
    Even if you make each method synchronized...what would prevent a request to Update and a request to Delete coming in at the same time, thus causing thread safety. Seems to me each of these methods needs to wait for a semaphore to clear before continuing. Or better yet these methods all use a queue... – JPM Nov 13 '12 at 15:46
  • 4
    @JPM Yeah, you're absolutely right. In the case that you are using an `SQLiteDatabase` as the underlying source, this shouldn't matter (`SQLiteDatabase` is thread safe). Other than that, there are no guarantees... you'd have to either synchronize the data source or synchronize access to the `ContentProvider` (I would suggest the former). – Alex Lockwood Nov 13 '12 at 16:13
  • How would you synchronize the Datasource/DataBaseHelper class that ContentProvider uses? Maybe I should ask this as a question... seems something others have not answered on this site. – JPM Nov 13 '12 at 16:49
  • @JPM It depends on the data source, I guess. What is the data source you need to synchronize? – Alex Lockwood Nov 13 '12 at 16:59
  • 1
    I have a SQlite datasource, my issue is multiple threads and multiple applications can access the ContentProvider at the same time. – JPM Nov 13 '12 at 17:04
  • @JPM Yeah, so that's what I'm saying... you don't need to worry about thread safety then since `SQLiteDatabase` provides it for you. See the link to my blog post on this in my answer. – Alex Lockwood Nov 13 '12 at 18:53
  • SQLiteDatabase class implements thread-safety by using ReentrantLock though it can be explicitly switched off if you really need it with setLockingEnabled(). But it is enabled by default. – WindRider Jun 12 '14 at 16:00
  • 2
    @JPM (very late reply to your first comment) If you simply add the `synchronized` keyword to both, the `update` and `delete` methods, both methods will try to acquire a lock for the same object (the ContentProvider) and thus they will not execute simultaneously either. – Markus A. Nov 13 '15 at 18:22