Answer the question
In order to leave comments, you need to log in
ScheduledExecutorService and queue how to deal?
Using ScheduledExecutorService example code:
private final ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor();
public void onClickWrite(View v) {
mStartEngineFutures[0] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 11, 5, 0, 0}), 0, TimeUnit.MILLISECONDS); //10
mStartEngineFutures[1] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 11, 1, 0, 0}), 10000, TimeUnit.MILLISECONDS); //0
mStartEngineFutures[2] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 0, 5, 0, 0}), 11000, TimeUnit.MILLISECONDS); //30
mStartEngineFutures[3] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 1, 5, 0, 0}), 40000, TimeUnit.MILLISECONDS); //5
mStartEngineFutures[4] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 2, 5, 0, 0}), 45000, TimeUnit.MILLISECONDS); //5
mStartEngineFutures[5] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 3, 5, 0, 0}), 50000, TimeUnit.MILLISECONDS); //5
mStartEngineFutures[6] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 4, 5, 0, 0}), 55000, TimeUnit.MILLISECONDS); //5
mStartEngineFutures[7] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 5, 5, 0, 0}), 60000, TimeUnit.MILLISECONDS); //5
mStartEngineFutures[8] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 6, 5, 0, 0}), 65000, TimeUnit.MILLISECONDS); //5
mStartEngineFutures[9] = mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 7, 5, 0, 0}), 70000, TimeUnit.MILLISECONDS); //5
mStartEngineFutures[10]= mExecutor.schedule(getSendRunnable(new byte[]{1, 0x5, 8, 5, 0, 0}), 75000, TimeUnit.MILLISECONDS); //5
}
private Runnable getSendRunnable(final byte[] data){
return new Runnable(){
@Override public void run(){
send(data);
String a = "";
for (byte b : data) {
a += String.valueOf(b) + " ";
}
}
};
}
}
for (ScheduledFuture<?> future : mStartEngineFutures) {
if (!future.isDone()) {
future.cancel(true);
}
}
private void send(byte[] data){
synchronized (mLocker) {
try {
Log.d("OvisLog", "начинаем старт, но ждем своей очереди "+ Arrays.toString(data));
while (mWaitingForResponse) {
mLocker.wait();
}
} catch (InterruptedException e) {
Log.d("OvisLog", "отменяемся "+Arrays.toString(data));
return;
}
}
byte[] bytesToSend = addCRC(data);
mWaitingForResponse = true;
mPhysicaloid.write(bytesToSend, bytesToSend.length);
Log.v("OvisLog", "Отправил - " + Arrays.toString(bytesToSend));
}
And the cyclepublic void startContinuousSending(long period){
mContinuousSendingFuture = mExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
send(new byte[]{1, 3, 0, 0, 0, 0});
}
}, 0, period, TimeUnit.MILLISECONDS);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question