[英]ConcurrentLinkedQueue shared across activities in Android
我正在開發一個需要4個byte []隊列才能在多個活動之間共享的應用程序。 我使用ConcurrentLinkedQueue是因為我希望它們是非阻塞的,因為它們將與IOIO OTG上的2個UART通信。 似乎我只能將數據放在初始化隊列的同一方法中,並且將控制權傳遞給另一個方法時,大小為0。如果我將它們聲明為public並在一個活動中將其初始化,則其他活動可以看到它們,但是在add()之后,大小仍然為0,沒有異常,並且add()返回true。 我什至試圖將它們放在一個Singleton Application對象中,但是結果是相同的。
IOIO服務循環器將讀取輸出隊列並將字節發送到IOIO,並從IOIO讀取字節並將其放入輸入隊列中。 其他活動會將數據放入輸出隊列,並從輸入隊列中刪除數據。
這是我現在擁有的一些簡化代碼:
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("");
}
}
我想念一些概念嗎? 有沒有其他方法可以跨活動共享非阻塞隊列?
stefs是正確的-問題出在另一個區域。 我的調試方法有問題,代碼正常運行。 我的錯誤是,在單步執行一個線程時,另一個正在全速運行,並在檢查字節之前消耗了字節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.