【发布时间】:2016-10-06 17:41:47
【问题描述】:
我正在开发一个需要 4 个字节 [] 队列在多个活动之间共享的应用程序。我使用 ConcurrentLinkedQueue 是因为我希望它们是非阻塞的,因为它们将与 IOIO OTG 上的 2 个 UART 通信。看来我只能将数据放在初始化它们的同一方法中的队列中,当控制权传递给另一个方法时,大小为0。如果我将它们声明为公共并在一个活动中初始化它们,其他可以活动看到他们,但是在 add() 之后,大小仍然是 0 并且没有异常并且 add() 返回 true。我什至尝试将它们放在单例 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("");
}
}
我错过了一些概念吗?还有其他方法可以在活动之间共享非阻塞队列吗?
【问题讨论】:
-
在我看来它应该可以工作,所以我尝试了同样的事情(使用单例)并且它成功了。您的问题可能出在其他地方。
标签: java android queue java.util.concurrent ioio