1
I am trying to perform a task every 1 minute. The task is executed but when it arrives at the right minute to perform the action of this exception:
java.lang.Illegalstateexception: Not on FX application thread; currentThread = Timer-0
In short, I have a method that takes system time and checks if it equals bank time, if the hours are equal an alert is displayed. It was all working before I put in Timertask, The Method alertaGeral();
is called and works since it is not with equal Hours, when the hours are equal the alert should be displayed but the error.
My method:
private void executarTarefa() {
//****INICIA A TAREFA ELE VERIFICA A CADA UM MINUTO****//
Platform.runLater(new Runnable() {
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
System.out.println("Iniciado!");
Timer timer = null;
if (timer == null) {
timer = new Timer();
TimerTask tarefa = new TimerTask() {
@Override
public void run() {
try {
alertaGeral();
System.out.println("Alertado..");
System.out.println(sdf.format(new Date()));
} catch (Exception e) {
e.printStackTrace();
}
}
};
timer.scheduleAtFixedRate(tarefa, TEMPO, TEMPO);
}
}
});
}
Everything you put into Timertask runs in a parallel thread, you’re probably trying to use some object that’s being used by the main thread. You have to stop using this object or create a copy of it and use it.
– Dener
I’m trying to use a
Lista<Tarefas>
. I use the same list to fill my table– DiegoAugusto
So I don’t think you can use the same list, because threads are competitors. Try to make with a copy, but any changes made to this list or its items will not be reflected in the original.
– Dener
where is your method
executarTarefa()
and your table?– Dener
I think I found the error, I’m going to do a test. The error may be on this line:
Alert dialogoAviso = new Alert(Alert.AlertType.INFORMATION);
 dialogoAviso.setTitle("AVISO1");
 dialogoAviso.setHeaderText("Voce tem um compromisso marcado para: " + c.getDataExibicao() + " as " + c.getHoraInicial());
 dialogoAviso.setContentText("Compromisso: " + c.getCompromisso() + "\nNome do Beneficiário: " + c.getNomeBeneficiario());
 dialogoAviso.showAndWait();
– DiegoAugusto
I’m using this Alert within the method
alertaGeral();
and the methodexecutaTarefa
is in the builder– DiegoAugusto