/* * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. */ package test; import javafx.application.Application; import javafx.application.Platform; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; public class RunLaterProblem extends Application { private static final int RUN_LATERS = 9000; private static final int REGISTERING_SLOWDOWN_LOOP_COUNT = 600000; private static final int RUNNING_SLOWDOWN_LOOP_COUNT = 1000000; public static void main(String[] args) { Application.launch(args); } private void doSomeWork(int count) { for (int i = 0; i < count; i++) { int j = i; j++; } } @Override public void start(Stage stage) { stage.setTitle("RunLaterProblem"); final Text t = new Text("Click the scene now"); t.setFont(new Font(20)); t.setTranslateY(50); Scene scene = new Scene(new Group(t), 600, 450); stage.setScene(scene); stage.show(); scene.setOnMousePressed(new EventHandler() { @Override public void handle(MouseEvent event) { System.out.println("Processing click"); t.setText("Clicked"); } }); Platform.runLater(new Runnable() { public void run() { System.out.println("Registering runLaters.."); for (int i = 0; i < RUN_LATERS; i++) { final int count = i; Platform.runLater(new Runnable() { public void run() { if (count % 1000 == 0) { System.out.println("Processing runLater #" + count); } if (count == 8999) { System.out.println("Processing the last runLater"); } doSomeWork(RUNNING_SLOWDOWN_LOOP_COUNT); } }); doSomeWork(REGISTERING_SLOWDOWN_LOOP_COUNT); } System.out.println("RunLaters registered."); } }); } }