Wait for the execution of a method to be finished, before being able to re-export it again

Asked

Viewed 861 times

1

I am developing a data synchronizer, basically the synchronizer will every 5 seconds check if there are data to be synchronized.

However, my doubt consists in how I will do for when the synchronization is happening, it does not start a new synchronization, IE, a synchronization process will have to start, finish, wait the 5 seconds to be able to start a new synchronization. Does anyone have an idea how to do?

  • You make it every 5 seconds, open an application instance?

  • No. My application will be running. And every 5 seconds, it will call a method that will run the application. Then I would check if there was a synchronization happening or not, if it exists, I hope it ends to start another, if it does not exist, it starts the method. I could tell?

  • tries to use the class Timer java

  • 2

    this link (http://www.dsc.ufcg.edu.br/~Jacques/cursos/map/html/threads/timer.html) has an example of how to use the classes Timer and TimerTask

  • Thanks @Juliocesar, I’ll take a look and already put a feedback. :)

  • @Juliocesar made using timer and is working, however, I could not find a way to identify if the method is still running.

  • @Joséallison, as well, did not understand..

  • Imagine the scenario: Started the synchronization method (method 01), and the data synchronization process exceeded the 5 seconds that set for my method 01, hence method 01 would start again, and there is already an execution of it. So what I want to avoid is this, that it starts to synchronize if an instance of the method is running. I understood?

  • Try to use flags, when method_01 started activating the flag in true and when it ends it returns to the normal state, at the time of executing the synchronization method asks for the flag, depending on the value executes or does not execute

Show 4 more comments

1 answer

1


If your synchronization operation involves interface updates these tasks must be performed in the FX Thread, otherwise such an error will occur: Not on FX application thread

Assuming this is the case, to perform a task every X time on the main thread we have the Scheduledservice, which works as follows:

The Scheduledservice is a Service which will Automatically Restart itself after a Successful Execution, and under some conditions will Restart Even in case of Failure. A new Scheduledservice Begins in the READY state, just as a normal Service. After Calling start or Restart, the Scheduledservice will enter the SCHEDULED state for the Duration specified by delay.

Executable example:

ScheduledService<Void> sync = new ScheduledService<Void>() {
    @Override
    protected Task<Void> createTask() {
        Task<Void> task = new Task<Void>() {
            @Override
            protected Void call() throws Exception{
                // sua operação de sincronização
                return null;
            }                
        };
        return task;
    }
};
// Thread executa a cada 5 segundos
sync.setPeriod(Duration.seconds(5));

// Início do serviço de sincronização
sync.start();

// Parar a sincronização
sync.cancel();

This way you can put a period that is equivalent to the time of execution of the update code in average + 5 seconds. There would be no need to check if the process is already occurring and to allocate a thread to count the 5 seconds.

Important remarks: If the execution of the task takes longer than the specified period the delay is skipped and the next schedule is executed immediately.

  • Thanks for your attention and help @Gustavofragoso. But what if I don’t know for sure? If it happens the time I set, does the synchronization exceed that time? There would be a dynamic way to set this time for it to start counting only when the synchronization run is over?

  • In the notes I posted it says that when the time exceeds it jumps to the next synchronization. I am seeing if there is a way to synchronize, update my answer if there is.

  • OK @Gustavofragoso. I stand by and once again appreciate the help. I hope you understand my doubt about him jumping to the next update without having finished the previous one.

  • 1

    Really the safest way (Thread Safe) is this, it doesn’t work trying to startar a service or task within another service/task.

  • 1

    He doesn’t jump without finishing no, he always finishes. He jumps if for example the task took 50 sec and the period was 45 then he passes to the next... in case there is failure he restarts alone.

  • So, I get it, and it’s working like this, when the timing of the synchronization takes longer than the time of the Schedule call, it’s working, but even though it takes longer than the call time, It is a requirement here of the project manager that he execute the period of the Schedule call, and then start the synchronization, without creating an execution queue. Can you understand the size of my scolding? Again, I’m grateful!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.