C
C
Chvalov2015-09-10 13:38:13
Java
Chvalov, 2015-09-10 13:38:13

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) + " ";
                }
            }
        };
    }
}

When I need to stop this cycle and start another one, the commands from the first cycle jump, after which another one already starts, I started digging the log and noticed that it seemed to put everything in a queue, and when I cancel this cycle, there are commands in the queue.
for (ScheduledFuture<?> future : mStartEngineFutures) {
                    if (!future.isDone()) {
                        future.cancel(true);
                    }
                }

It also starts the cycle of its robot with a delay, and moreover, the more time the cycle runs, the longer it takes to start the ScheduledExecutorService
UPDATE :
The send() method;
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 cycle
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);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question