Thursday, June 7, 2012

Example of using ExecutorService

ScheduledExecutorService is an ExecutorService that can schedule commands to run after a given delay, or to execute periodically.

ScheduledExecutorService


package javafx.ex;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class testScheduledExecutorService extends Application {
    
    Text textCounter;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Start ScheduledExecutorService");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                startScheduledExecutorService();
            }
        });
        
        textCounter = new Text();
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(btn, textCounter);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void startScheduledExecutorService(){
        final ScheduledExecutorService scheduler 
            = Executors.newScheduledThreadPool(1);

        scheduler.scheduleAtFixedRate(
                new Runnable(){
                    
                    int counter = 0;
                    
                    @Override
                    public void run() {
                        counter++;
                        if(counter<=10){
                            textCounter.setText("Counting: " 
                                    + String.valueOf(counter));
                        }else{
                            scheduler.shutdown();
                            textCounter.setText("-Finished-");
                        }
                        
                    }
                }, 
                1, 
                1, 
                TimeUnit.SECONDS);        
    }
}



Please note that it's not a good practice to update UI in background thread. Refer to next article to Update UI in JavaFX Application Thread using Platform.runLater().

2 comments:

  1. This is a bad example. You are updating JavaFX controls while not on the JavaFX Thread.

    ReplyDelete
    Replies
    1. Hello Craig Day,

      Thanks your comment.

      Yes, you are right, it's not a good practice. Please refer to next: Update UI in JavaFX Application Thread using Platform.runLater().

      Delete