[英]Java Thread Concurrent Read and Write
I have a arraylist which store data i received and transmit asynchronously over bluetooth. 我有一个arraylist存储我收到的数据并通过蓝牙异步传输。
I have a write thread and a read thread to access the arraylist. 我有一个写线程和一个读取线程来访问arraylist。 I am trying to simulate bluetooth echo (The bluetooth is to echo back everything i send).
我正在尝试模拟蓝牙回声(蓝牙是回应我发送的一切)。
private class ReadThread extends Thread {
@Override
public void run() {
super.run();
while(!isInterrupted()) {
try {
byte[] buffer = new byte[64];
if (mInputStream == null) return;
size = mInputStream.read(buffer);
if (size == 64) {
if (bufferList.isEmpty()){
Log.i("AOK Fail","Nothing to AOK");
}
if (compareByte(buffer,bufferList.get(0) == true) // Compare data in this 2 byte array
bufferList.remove(0);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
private class WriteThread extends Thread {
@Override
public void run() {
super.run();
while(!isInterrupted()) {
try {
count++;
if (bufferList.isEmpty() == false && count < 3){
write(bufferList.get(0));
count = 0;
}else{
// Drop data after 3 fail attempt
bufferList.remove(0);
count=0;
}
Thread.sleep(500);
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
On button pressed i will send data . 按下按钮,我将发送数据。 It works fine if i transmit slowly.
如果我慢慢传输,它工作正常。 However it fails when i start to click rapidly (queue data for transmission).
但是当我开始快速点击(队列数据传输)时它失败了。 Once i received a AOK Fail message .
一旦我收到AOK失败消息。 All remaining data will all be AOK Fail even newly add data.
即使是新添加的数据,所有剩余数据都将是AOK Fail。 Why is that so
为什么会这样
If it is a threading issue shouldn't it be resolve after all data is transmitted ? 如果它是一个线程问题,那么在传输所有数据之后不应该解决它吗?
This bufferList variable is a thread safe object? 这个bufferList变量是一个线程安全的对象? like an array blocking list or something?
像一个数组阻止列表或什么? If not you may be having some problems trying to add/remove itens simultaneously.
如果不是,您可能在尝试同时添加/删除它时遇到一些问题。
This would cause an exception in the producer Thread, nothing else would be transmitted you'd get the "Nothing to AOk" log. 这会导致生产者线程中出现异常,没有其他任何内容会被传输,您将获得“Aothing to AOk”日志。
It's not very clear how this code works because it does not include some details: 目前还不是很清楚这段代码是如何工作的,因为它不包含一些细节:
bufferList
and which is its type? bufferList
,它的类型是什么? Maybe the ArrayList you mentioned in your question? write
method does? write
方法呢? compareByte
method does? compareByte
方法有什么作用? It seems obvious, but it will be good to take a look at the code, you know, just in case (also you are missing a bracket). count
var you are using inside WriteThread class declared? count
var在哪里? Are you incrementing count
value outside this code? count
数值? If that's true, you may be removing all contents from bufferList
variable when count >= 3
(else block). count >= 3
(否则阻塞)时,您可能会从bufferList
变量中删除所有内容。 And if you do this, the first part of your bufferList.isEmpty() == false && count < 3
condition can be false at some time because your bufferList
will be empty; bufferList.isEmpty() == false && count < 3
条件的第一部分在某些时候可能是假的,因为你的bufferList
将为空; then you will execute the else block, trying to remove an element from an empty ArrayList, your code will throw an IndexOutOfBoudsException (see the ArrayList remove method documentation ) and your WriteThread will be aborted.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.