android - Java Thread Concurrent Read and Write -
i have arraylist store data received , transmit asynchronously on bluetooth.
i have write thread , read thread access arraylist. trying simulate bluetooth echo (the bluetooth echo 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 aok"); } if (comparebyte(buffer,bufferlist.get(0) == true) // compare data in 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 send data . works fine if transmit slowly. fails when start click rapidly (queue data transmission). once received aok fail message . remaining data aok fail newly add data. why
if threading issue shouldn't resolve after data transmitted ?
this bufferlist variable thread safe object? array blocking list or something? if not may having problems trying add/remove itens simultaneously.
this cause exception in producer thread, nothing else transmitted you'd "nothing aok" log.
Comments
Post a Comment