How to use executorservice

1

I have an arrayList, where I store 12 values [1 or 0], depending on whether the error is activated or not (0 being deactivated and 1 activated).

public void rellenar() {
    cont = 0;
    list = new ArrayList<String>();
    list.add(0, getVibrador1Reponer());//vibrador 1 reponer
    list.add(1, getVibrador1Atasco());//vibrador 1 atasco
    list.add(2, getVibrador2Reponer());//vibrador 2 reponer
    list.add(3, getVibrador2Atasco());//vibrador 2 atasco
    list.add(4, getVibrador3Reponer());//vibrador 3 reponer
    list.add(5, getVibrador3Atasco());//vibrador 3 atasco
    list.add(6, getVibrador4Reponer());//vibrador 4 reponer
    list.add(7, getVibrador4Atasco());//vibrador 4 atasco
    list.add(8, getVibrador4Reponer());//vibrador 5 reponer
    list.add(9, getVibrador4Atasco());//vibrador 5 atasco
    list.add(10, getVibrador4Reponer());//vibrador 6 reponer
    list.add(11, getVibrador4Atasco());//vibrador 6 atasco
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).equals("1")) {
            cont++;

        }
    }
    System.out.println("total de Hilos a crear son: " + cont);
    zonaFallos();//ejecuta los audios segun la zona del error 
    System.out.println("____________________________________________________");
}

Well now check if the error is activated or not (all this I'm checking in real time)

private void zonaFallos() {
    if (list.get(0).equals("1")) {
        audioVibrador.playFalloVibrador1Reponer();
    }
    if (list.get(1).equals("1")) {
        audioVibrador.playFalloVibrador1Atasco();
    }
    if (list.get(2).equals("1")) {
        audioVibrador.playFalloVibrador2Reponer();
    }
    if (list.get(3).equals("1")) {
        audioVibrador.playFalloVibrador2Atasco();
    }
    if (list.get(4).equals("1")) {
        audioVibrador.playFalloVibrador3Reponer();
    }
    if (list.get(5).equals("1")) {
        audioVibrador.playFalloVibrador3Atasco();
    }
    if (list.get(6).equals("1")) {
        audioVibrador.playFalloVibrador4Reponer();
    }
    if (list.get(7).equals("1")) {
        audioVibrador.playFalloVibrador4Atasco();
    }
    if (list.get(8).equals("1")) {
        audioVibrador.playFalloVibrador5Reponer();
    }
    if (list.get(9).equals("1")) {
        audioVibrador.playFalloVibrador5Atasco();
    }
    if (list.get(10).equals("1")) {
        audioVibrador.playFalloVibrador6Reponer();
    }
    if (list.get(11).equals("1")) {
        audioVibrador.playFalloVibrador6Atasco();
    }
}

An audio is played giving the warning sequentially.

I need to create a task for each 30-second warning, so that it is not so repetitive (and that the task is not executed at the same time, since the audio overlaps) The task will be executed each time you issue a warning (to 1), and the task will be canceled when the fault is solved (to 0). I have been told to spend "executorservice", but I do not know how to start ... I would appreciate your help. Thanks.

    
asked by R.Priego 09.11.2016 в 17:14
source

1 answer

2

You should use ScheduledExecutorService . You need to do 3 things:

  • Implement Runnable in the class you invoked from ScheduledExecutorService.
  • Obtain an instance of the ScheduledExecutorService.
  • Launch the task
  • Something like this:

    // la clase
    class MiClase implements Runnable {
    
        void run() {
             zonaFallos();
        }
    
        // declaracion de zonaFallos()
    }
    
    // donde 1 es la cantidad maxima de tareas simultaneas
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    
    MiClase target = new MiClase();
    
    // esto lo lanza cada 30 segundos exactos
    scheduler.scheduleAtFixedRate(target, 0, 30, TimeUnit.SECONDS);
    
    // esto lo lanza con 30 segundos de diferencia entre ejecuciones.
    scheduler.scheduleWithFixedDelay(target, 0, 30, TimeUnit.SECONDS);
    

    The first option, execute the task exactly every 30 seconds, if it does not finish in those 30 seconds, it will launch the task again (if there are enough Threads in the pool of ScheduledExecutorService ).

    The second option, avoids that the task is executed more than once in simultaneous since it calculates the 30 seconds after the previous execution ended.

    Cancel the task

    There are 2 ways:

    One is invoking shutdown of the Scheduler.

     scheduler.shutdown();
    

    The other is taking a ScheduledFuture<?> task when you create the task and canceling it manually.

    ScheduledFuture<?> scheduledFuture = scheduler.scheduleWithFixedDelay(target, 0, 30, TimeUnit.SECONDS);
    
    // luego cuando la quieres cancelar
    scheduledFuture.cancel(false); // false evita que lanze una interrupcion.
    

    Salu2

        
    answered by 09.11.2016 / 17:36
    source