Why does trying to change a variable inside a TimerTask give me an error?

0

On line 13 ( sleep = true; ) tells me the

  

Syntax error on token "sleep", VariableDeclaratorId expected after this token.

import java.io.IOException;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Comprobacion {
    public static void main(String[] args) throws IOException {
    	
    	boolean dormir = false;
    	
    	TimerTask task = new TimerTask() {
    		dormir = true;
	    	int cont = 0;
    	    @Override
    	    public void run() {
				cont++;
				System.out.println("Contador: "+cont);
    	    }
    	};
    	
        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
        
    	int tiempoDeInicio = 1, tiempoDeIntervalo = 1;
    	
        scheduler.scheduleAtFixedRate(task, tiempoDeInicio, tiempoDeIntervalo, TimeUnit.SECONDS);
    }
}
    
asked by Pablo Contreras 12.05.2018 в 21:46
source

1 answer

1

The problem is that the variable dormir is declared locally and changing the value in the class space TimerTask . Therefore, he asks for a statement.

With cont it does not give you problems, because if you are declaring it. If you want to change that variable within the subclass, you must do it within some method of the same or out of context:

// dormir = true; antes de llamar el TimerTask es valido
TimerTask task = new TimerTask() {
            // dormir = true; lanza error porque asume una declaracion
            int cont = 0; // este es valido porque se declara como parte de TimerTask
            @Override
            public void run() {
                // dormir = true; aqui es valido si es static y global
                cont++;
                System.out.println("Contador: "+cont);
            }
        };

Looking at it as a class you are doing this:

public class TimerTask {
       dormir = true; 
       . . .
}

And obviously doing that in a class will throw a syntax error clarifying that you must specify the type.

Try to do this:

import java.io.IOException;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Comprobacion {

    private static boolean dormir = false;
    public static void main(String[] args) throws IOException {

        TimerTask task = new TimerTask() {
            int cont = 0;
            @Override
            public void run() {
                dormir = true;
                cont++;
                System.out.println("Contador: "+cont);
            }
        };

        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

        int tiempoDeInicio = 1, tiempoDeIntervalo = 1;

        scheduler.scheduleAtFixedRate(task, tiempoDeInicio, tiempoDeIntervalo, TimeUnit.SECONDS);
    }
}
    
answered by 12.05.2018 / 22:01
source