Android并发问题

发布于 2024-11-30 09:31:21 字数 465 浏览 0 评论 0原文

我已经严格分离了 Android 应用程序不同部分之间的层。 在执行的某个时刻,我从 Internet 上的 xml 服务更新我的数据。该更新大约需要 10 秒,并且完全在后台完成 - 这意味着应用程序的用户界面运行良好。然而,对负责数据更新(更新已开始但尚未完成后)的我的类(后来的 DataManager)的进一步调用会使我的应用程序崩溃。对于从不为 null 的对象,会引发 NullPointerException。

因此,我假设一次只有一个线程可以使用我的 DataManager,并且从其他线程调用 DataManager 会导致异常。

我尝试了将“同步”关键字放在敏感方法附近的各种组合,但似乎在执行更新时 - 没有任何东西可以使用我的 DataManager 中的任何内容。

顺便说一句,在执行过程中与 DataManager 相关的其他类似乎也持有空对象。

我想我只是缺少某种用于处理并发问题的设计模式,也许有人可以建议我一些东西?

I have strictly separated the layers between different parts of my Android application.
At some point of execution I am updating my data from xml service on Internet. That updating takes up about 10 seconds and is done completely in the background - meaning the user interface of an application works fine. However further calls to my class (later - DataManager) which is responsible for data updating (after update has been started but not yet finished) makes my application crash. NullPointerException is thrown with objects which NEVER are null.

So I assume that only one Thread can use my DataManager at one time and calls to DataManager from other threads ends up in exceptions.

I tried various combinations of putting 'synchronized' keywords near sensitive methods but it seems that when update is executing - NOTHING can use ANYTHING from my DataManager.

BTW other classes which are related to DataManager during the execution also seem to hold null objects.

I guess I am just missing some kind of design pattern which is used to deal with concurrency problems and maybe anyone can suggest me something?

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

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

发布评论

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

评论(1

自演自醉 2024-12-07 09:31:21

由于线程问题,我在使用 Apache http 客户端时遇到了麻烦,我认为您的问题在这方面可能类似。我最终做的是设置一个回调方案。当然,这可能适合你,也可能不适合你。

这对你来说可能有点像鲁布·戈德堡,但它对我有用。

我会让我的 UI 线程使用生成线程来获取数据的方法来调用我的数据管理器对象。该方法的返回值是一个对象,其中最终包含数据。我会让我的活动扩展一个接口,例如“DataCallbackInterface”,并使用线程在获取数据后调用的方法(即 run() 中的最后一行)。由于该调用本质上是在另一个线程中进行的,因此您需要使用 Handler 来运行 DataCallbackInterface 方法的实现中有用的任何内容。当调用该方法时,您将知道数据在那里并且不依赖任何奇怪的同步标志来使其正确。

I had trouble dealing with using the Apache http client because of threading issues and I think your issue could be similar in that respect. What I ended up doing was setting up a callback scheme. This may or may not work for you, of course.

This may seem a bit Rube Goldberg-like to you, but it worked for me.

I would have my UI thread call my data manager object with a method that spawned a thread to go and acquire the data. The method's return value is an object that would EVENTUALLY have the data in it. I would have my activity extend an interface, something like "DataCallbackInterface", with a method that the thread would call after it acquired the data (i.e. the last line in run()). Since that call will inherently be within another thread, you'll need to use a Handler to run anything useful in your implementation of the DataCallbackInterface method. When that method is called, you will know for a fact that the data is there and not rely on any strange synchronization flags to get it right.

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