Simple example for "ScheduledService" in Javafx

I am a student and have been learning JavaFX for a month. I am developing an application in which I want the service to restart again after completing a task. For this, I found out that ScheduledService 'is being used . So can someone explain the use of scheduledservice with a simple example, as well as how it differs from the "Service" in JavaFX. Thank;)

EDIT . How can I determine that this ScheduledService named DataThread should be restarted every 5 seconds?

public class DataThread extends ScheduledService<Void>
{
    @Override
    public Task<Void> createTask() {
        return new Task<Void>() {
            @Override
            public Void call() throws Exception {
             for(i=0;i<10;i++)
             {
                 System.out.println(""+i);
             }
              return null;
            }
        };
    }
}  
+4
source share
1 answer

, Service. ScheduledService - .

ScheduledService - ,

,

Service -> Execute One Task
ScheduledService -> Execute Same Task at regular intervals

- TimerService, . 1

import java.util.concurrent.atomic.AtomicInteger;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TimerServiceApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        TimerService service = new TimerService();
        AtomicInteger count = new AtomicInteger(0);
        service.setCount(count.get());
        service.setPeriod(Duration.seconds(1));
        service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

            @Override
            public void handle(WorkerStateEvent t) {
                System.out.println("Called : " + t.getSource().getValue()
                        + " time(s)");
                count.set((int) t.getSource().getValue());
            }
        });
        service.start();
    }

    public static void main(String[] args) {
        launch();
    }

    private static class TimerService extends ScheduledService<Integer> {
        private IntegerProperty count = new SimpleIntegerProperty();

        public final void setCount(Integer value) {
            count.set(value);
        }

        public final Integer getCount() {
            return count.get();
        }

        public final IntegerProperty countProperty() {
            return count;
        }

        protected Task<Integer> createTask() {
            return new Task<Integer>() {
                protected Integer call() {
                    //Adds 1 to the count
                    count.set(getCount() + 1);
                    return getCount();
                }
            };
        }
    }
}
+3

Source: https://habr.com/ru/post/1570540/


All Articles