Chvalov
@Chvalov

ScheduledExecutorService и очередь, как бороться?

Использую ScheduledExecutorService пример кода:
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);
                    }
                }

Начинает цикл свою роботу также с задержкой и притом чем больше времени работает цикл тем дольше приходится ждать запуска ScheduledExecutorService

UPDATE:
Метод send();
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));
    }
И сам цикл
public 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);
    }
  • Вопрос задан
  • 436 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы