如何实现一个好的Android全局对象,将数据从后台线程实时传递到Activity而不死锁?

发布于 2024-11-10 08:03:22 字数 1261 浏览 4 评论 0原文

@Jusit 建议我使用全局对象将数据从后台线程传递到活动,并且它有效。但现在我担心死锁,是否有好的方法来避免/防止它。另外,我不确定我选择 String 作为全局对象是否是一个好的选择。这是我目前所做的解释。

实际上,全局对象只是在 Blueterm.java 中创建的公共静态变量,如下所示:

public class BlueTerm extends Activity {
    // a global object I guess
    public static String strData = "";
    // a couple of booleans to control reads/writes
    public static boolean strWrite = false;
    public static boolean strRead = false;
    ...
}

所以 strData 我想是由后台线程创建和写入的全局对象,如下所示:

while (BlueSentry.strRead == true);
BlueSentry.strWrite = true;
BlueSentry.strData = BlueSentry.strData + Character.toString(printableB);
BlueSentry.strWrite = false;

我的目的是让 strRead 防止 strData 被覆盖当我的绘图活动正在阅读它时。 strWrite 阻止我的绘图活动在写入 strData 时读取它,如下所示:

while (BlueSentry.strWrite == true);
BlueSentry.strRead = true;
String strData = BlueSentry.strData;
BlueSentry.strData = "";
BlueSentry.strRead = false

您可以看到,在绘图活动读取 strData 后,strData 被重置为空字符串。

我在手机未通过 USB 连接调试时经历过强制关闭,所以我不确定是什么导致了强制关闭。从那以后这种事就再也没有发生过。

对于在这里实现并发而没有死锁有什么建议吗?我读过有关互斥(信号量、锁、互斥体)的内容,但我不确定应该使用哪一个。另外,也许我选择 String 作为全局对象是一个糟糕的选择,因为我想要实时数据传输。也就是说,我不希望线程被阻塞等待活动完成读取。而且我不希望活动被阻止等待后台线程完成写入。我可以摆脱 String 并实现/使用 FIFO 缓冲区吗?不是吗?

@Jusit suggested I use a global object to pass data from a background thread to an activity and it worked. But now I am worried about deadlock and if there is a good way to avoid/prevent it. Also I am not sure my choice of String as a global object is such a good one. Here's an explanation what I have currently done.

Actually the global object is just a public static variable created in Blueterm.java as shown below:

public class BlueTerm extends Activity {
    // a global object I guess
    public static String strData = "";
    // a couple of booleans to control reads/writes
    public static boolean strWrite = false;
    public static boolean strRead = false;
    ...
}

So strData I suppose is my global object created and written to by a background thread as follows:

while (BlueSentry.strRead == true);
BlueSentry.strWrite = true;
BlueSentry.strData = BlueSentry.strData + Character.toString(printableB);
BlueSentry.strWrite = false;

My intention is for strRead to prevent strData from being over written while my plotting activity is reading it. strWrite prevents my plotting activity from reading strData while it is being written as follows:

while (BlueSentry.strWrite == true);
BlueSentry.strRead = true;
String strData = BlueSentry.strData;
BlueSentry.strData = "";
BlueSentry.strRead = false

You can see that strData gets reset to an empty string after the plotting activity is through reading it.

I have experienced a force close while my phone was not connected to debugging with the USB, so I am not sure what caused the force close. And it has not happened since.

Any suggestions for achieving concurrency here without deadlock? I have read about mutual-exclusion (semaphores, locks, mutex), but I am not sure which if any I should use. Also maybe my choice of String as a global object is a bad one, since I want real-time data transfer. That is I don't want the thread blocked waiting for the activity finish reading. And I don't want the activity blocked waiting for the background thread to finish writing. May I should get rid of String and implement/use a FIFO buffer instead, no?

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

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

发布评论

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

评论(1

献世佛 2024-11-17 08:03:22

使用处理程序(请参阅 https://developer.android.com/reference/android /os/Handler.html)作为通信技术(如果您要从后台线程转到 Activity)。请参阅 https://web.archive.org/web/20200810154212/http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html 快速教程,但简而言之:

  1. 在您的活动中创建一个处理程序,该处理程序接受消息中的 Character.toString(printableB) 值。
  2. 将 Handler 传递给后台线程。
  3. 每次更新时,后台线程都会向处理程序发送消息。
  4. 处理程序被调用并对活动的线程进行必要的更改。

没有全局共享变量,没有潜在的读/写错误,每个人都高兴地回家。

Use a Handler (see https://developer.android.com/reference/android/os/Handler.html) as the communication technique if you are going from a background thread to an Activity. See https://web.archive.org/web/20200810154212/http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html for a quick tutorial, but in short:

  1. Create a Handler in your activity that accepts the value of Character.toString(printableB) in a Message.
  2. Pass the Handler to the background thread.
  3. The background thread sends messages to the Handler with each update.
  4. The Handler gets called and makes the necessary changes on the Activity's thread.

No globally shared variables, no potential read/write errors, everyone goes home happy.

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