Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple example for 'ScheduledService' in Javafx

I am a student and learning JavaFX since a month. I am developing a application where I want a service to repeatedly start again after its execution of the task. For this I have come to know that 'ScheduledService' is used. So can anybody please explain the use of scheduledservice with simple example and also how it differs from the 'Service' in JavaFX. Thanks ;)

EDIT : How can I define 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;
            }
        };
    }
}  
like image 630
Anurag Janbandhu Avatar asked Jan 09 '15 04:01

Anurag Janbandhu


People also ask

What is concurrency in JavaFX?

The javafx. concurrent package consists of the Worker interface and two basic classes, Task and Service , both of which implement the Worker interface. The Worker interface provides APIs that are useful for a background worker to communicate with the UI. The Task class is a fully observable implementation of the java.

What does platform runLater do?

runLater. Run the specified Runnable on the JavaFX Application Thread at some unspecified time in the future. This method, which may be called from any thread, will post the Runnable to an event queue and then return immediately to the caller. The Runnables are executed in the order they are posted.

Is JavaFX multithreaded?

JavaFX provides a complete package to deal with the issues of multithreading and concurrency. There is an interface called Worker, an abstract class called Task, and ScheduledService for this purpose. The Task is basically a Worker implementation, ideal for implementing long running computation.

What is JavaFX application thread?

JavaFX uses a single-threaded rendering design, meaning only a single thread can render anything on the screen, and that is the JavaFX application thread. In fact, only the JavaFX application thread is allowed to make any changes to the JavaFX Scene Graph in general.


1 Answers

Considering you have a sound knowledge of Service class. ScheduledService is just a Service with a Scheduling functionality.

From the docs

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

So we can say it as,

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

A very simple example of Scheduled Service is the TimerService, which counts the number of times the Service Task has been called. It is scheduled to call it every 1 second

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();
                }
            };
        }
    }
}
like image 57
ItachiUchiha Avatar answered Sep 23 '22 08:09

ItachiUchiha