开发者

How to implement a good Android global object to pass data in real-time from background thread to activity in real-time without deadlock?

开发者 https://www.devze.com 2023-03-08 21:15 出处:网络
@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

@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, si开发者_如何学Gonce 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?


Use a Handler (see http://developer.android.com/reference/android/os/Handler.html) as the communication technique if you are going from a background thread to an Activity. See 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号