ConcurrentLinkedQueue shared across activities in Android

296 Views Asked by At

I'm working on an app that requires 4 queues of byte[] to be shared across multiple activities. I'm using ConcurrentLinkedQueue because I want them to be non-blocking because they will be communicating with 2 UARTs on a IOIO OTG. It seems that I can only place data in the queues in the same method where they are initialized, and when control is passed to another method, the size is 0. If I declare them as public and initialize them in one activity, other can activities see them, but after add(), the size is still 0 and there are no exceptions and add() returns true. I even tried to put them in a singleton Application object, but the results are the same.

The IOIO service looper will read the output queues and send the bytes to the IOIO, and read bytes from the IOIO and place them in the input queues. Other activities will put data into the output queues and remove data from the input queues.

Here's some simplified code of what I have now:

public class MyApp extends Application {
    //for UARTs
    public static Queue<byte[]> inQueue1   = new ConcurrentLinkedQueue<byte[]>();
    public static Queue<byte[]> outQueue1  = new ConcurrentLinkedQueue<byte[]>();
    public static Queue<byte[]> inQueue2   = new ConcurrentLinkedQueue<byte[]>();
    public static Queue<byte[]> outQueue2  = new ConcurrentLinkedQueue<byte[]>();
}

public class MainActivity extends Activity {
    private ToggleButton toggleButton_;
    private Button btnSend_;
    private EditText etTxData;
.
.
.
    public void btnSendOnClick(View v){
        String s = etTxData.getText().toString(); //i.e. s="ABC"
        byte b[] = s.getBytes(); // i.e. b={65, 66, 67}
        MyApp.outQueue1.add(b);  //size is 0 before and after and add() returns true

        etTxData.setText("");
    }

}

Am I missing some concept? Is there another way to share non-blocking queues across activities?

1

There are 1 best solutions below

0
Kevin S. Miller On

stefs is correct - the problem was in another area. My debugging method was faulty and the code was working correctly. My error is that while stepping through one thread, the other was running full speed and consumed the bytes before I could examine them.