Skip to content

Instantly share code, notes, and snippets.

@nskvortsov
Created December 21, 2011 10:57
Show Gist options
  • Save nskvortsov/1505610 to your computer and use it in GitHub Desktop.
Save nskvortsov/1505610 to your computer and use it in GitHub Desktop.
senderThread = new Thread() {
run () {
while (!runSender) {
int id = nextIdFromQueue();
if (id == null) {
Thread.currentThread().sleep(50)
continue;
} else {
byte[] data = RMS.read(id);
RMS.remove(id); // данные прочитали, убили из хранилища.
for (int i=0; i<data.length) {
reading.add(new Point(data[i]);
if (readyToSend(reading) {
try {
sendReading(reading); // синхронная отправка
clearAndReinit(reading);
} catch (Exception e) {
// stop() всей работы с девайсом: мы не осилили отправить сообщение совсем.
}
}
}
}
}
};
senderThread.start();
public static final List QUEUE = new LinkedList();
public volatile booleand runSender = true;
public Integer nextIdFromQueue() {
if (QUEUE.size() > 0) {
synchronized(QUEUE) {
final Integer first = (Integer)QUEUE.get(0);
QUEUE.remove(0);
return first;
}
} else {
return null;
}
}
public void putIdToQueue(Integer nextId) {
synchronized(QUEUE) {
QUEUE.add(nextId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment