Is a UriMatcher match (Uri) reentrant?

In the examples I saw how to make a ContentProvider , everyone used UriMatcher#match(Uri) in the insert , query , update and delete methods to easily process all the URI patterns that the content provider responds to (for example: http: // developer .android.com / resources / samples / NotePad / src / com / example / android / notepad / NotePadProvider.html ). It seemed to me until today, when I noticed in the ContentProvider API documentation that insert , query , update and delete "you can call [all] from several threads". In addition, the UriMatcher documentation UriMatcher nothing about thread safety or that match is reentrant.

I need to worry about synchronizing calls with match in a shared instance of static UriMatcher , which is used in my implementations of insert , query , update and delete

+4
source share
1 answer

Looking through the UriMatcher source , it seems that several threads can call the match method at the same time, since the match implementation only accesses the uri stream variable (parameter), common String s and ArrayList<UriMatcher> elements (via ArrayList#get(int) , which is thread safe )

addURI not thread safe because it structurally modifies the ArrayList . This is the same ArrayList that match is read, so addURI cannot be called, and other threads may call match .

+5
source

All Articles