I'm using android aidl. I have a method to put data into a queue, and a read thread to take data from it.
private static LinkedBlockingQueue<int[]> mWriteQueue = new LinkedBlockingQueue<int[]>();
private final ISendService.Stub mBinder = new ISendService.Stub() {
@Override
public void writeData(int[] data) throws RemoteException {
try {
mWriteQueue.put(data);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private class WriteThread extends Thread {
public void run() {
while(true){
try {
int[] data = mWriteQueue.take();
Log.e(TAG, "mWriteQueue take:" + ByteUtils.toHexString(data));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
In the UI, a button is clicked to execute the code:
for(int i = 0; i < 10; i++) {
try {
mSendService.writeData(data);
Log.e(TAG, "write data:" + ByteUtils.toHexString(data));
} catch (Exception e) {
e.printStackTrace();
}
}
the result:
write data:0x51 0x51 0x01 0x08
write data:0x51 0x51 0x02 0x08
write data:0x51 0x51 0x03 0x08
write data:0x51 0x51 0x04 0x08
write data:0x51 0x51 0x05 0x08
write data:0x51 0x51 0x06 0x08
write data:0x51 0x51 0x07 0x08
write data:0x51 0x51 0x08 0x08
write data:0x51 0x51 0x09 0x08
write data:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x01 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
mWriteQueue take:0x51 0x51 0x0a 0x08
The data from take
is wrong, I see in the java doc, that take
should remove the item. I print the data before mWriteQueue.put(data);
to make sure data is correct.