UriMatcher 类的 match(Uri) 是可重入的吗?

发布于 2024-09-27 18:07:26 字数 1088 浏览 2 评论 0原文

我看到的关于如何制作 ContentProvider 的示例 都使用了 < insertqueryupdate中的 code>UriMatcher#match(Uri) 方法delete 方法可以轻松处理内容提供​​程序响应的所有 URI 模式(例如:http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NotePadProvider.html)。这对我来说似乎没问题,直到今天,当我在 ContentProvider API 文档中注意到 insertqueryupdate ,以及delete“可以从多个线程调用[全部]”。此外,UriMatcher 文档没有提及线程安全性或 match 是否可重入。

我是否需要担心在 insert 实现中使用的 UriMatcher 的共享 static 实例上同步对 match 的调用查询更新删除

The examples that I have seen of how to make a ContentProvider have all used the UriMatcher#match(Uri) method within the insert, query, update, and delete methods to easily handle all of the URI patterns that the content provider responds to (e.g.: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NotePadProvider.html). This seemed okay to me until today, when I noticed on the ContentProvider API documentation that insert, query, update, and delete "can [all] be called from multiple threads". Additionally, the UriMatcher documentation says nothing about thread-safety or whether match is reentrant.

Do I need to worry about synchronizing calls to match on a shared, static instance of UriMatcher that is used within my implementations of insert, query, update, and delete?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-10-04 18:07:26

浏览 UriMatcher 的来源,看来多个线程可以同时调用 match 方法,因为match 的实现仅访问每线程变量 uri(参数)、共享 StringArrayList< 的元素;UriMatcher> (通过 ArrayList#get(int),这是线程安全的)。

addURI线程安全的,因为它在结构上修改了ArrayListmatch 读取的内容与 ArrayList 相同,因此当其他线程可能调用 match 时,无法调用 addURI

Looking through the source of UriMatcher, it appears that multiple threads can call the match method simultaneously because the implementation of match only accesses the per-thread variable uri (the parameter), shared Strings, and elements of an ArrayList<UriMatcher> (via ArrayList#get(int), which is thread-safe).

addURI is not thread-safe because it structurally modifies an ArrayList. It is the same ArrayList that match reads from, so addURI cannot be called while other threads are possibly calling match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文