1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package hello;
  27 
  28 import java.util.concurrent.atomic.AtomicBoolean;
  29 import javafx.application.Application;
  30 import javafx.scene.Group;
  31 import javafx.scene.Scene;
  32 import javafx.scene.paint.Color;
  33 import javafx.scene.shape.Rectangle;
  34 import javafx.stage.Stage;
  35 
  36 public class HelloLaunchOnNewThread extends Application {
  37 
  38     static long startTime;
  39     static AtomicBoolean mainCalled = new AtomicBoolean(false);
  40 
  41     public HelloLaunchOnNewThread() {
  42         long endTime = System.nanoTime();
  43         long elapsedMsec = (endTime - startTime + 500000) / 1000000;
  44         System.err.println("DONE: elapsed time = " + elapsedMsec + " msec");
  45         System.err.println("Constructor: currentThread="
  46                 + Thread.currentThread().getName());
  47         if (!mainCalled.get()) {
  48             System.err.println("***************************************");
  49             System.err.println("*** ERROR: main() method not called ***");
  50             System.err.println("***************************************");
  51         }
  52     }
  53 
  54     @Override public void init() {
  55         System.err.println("init: currentThread="
  56                 + Thread.currentThread().getName());
  57     }
  58 
  59     @Override public void start(Stage stage) {
  60         System.err.println("start: currentThread="
  61                 + Thread.currentThread().getName());
  62 
  63         stage.setTitle("Launch from New Thread");
  64 
  65         Group root = new Group();
  66         Scene scene = new Scene(root, 600, 450);
  67         scene.setFill(Color.LIGHTGREEN);
  68 
  69         Rectangle rect = new Rectangle();
  70         rect.setX(25);
  71         rect.setY(40);
  72         rect.setWidth(100);
  73         rect.setHeight(50);
  74         rect.setFill(Color.RED);
  75 
  76         root.getChildren().add(rect);
  77         stage.setScene(scene);
  78         stage.show();
  79         System.err.println("You should now see the 'HelloWorld' rectangle in the window");
  80     }
  81 
  82     @Override public void stop() {
  83         System.err.println("stop: currentThread="
  84                 + Thread.currentThread().getName());
  85     }
  86 
  87     /**
  88      * @param args the command line arguments
  89      */
  90     public static void main(final String[] args) {
  91         mainCalled.set(true);
  92         System.err.println("main: currentThread="
  93                 + Thread.currentThread().getName());
  94         new Thread(() -> {
  95             // Sleep for a very short time to ensure main thread exits,
  96             // since that will provoke RT-9824
  97             try {
  98                 Thread.sleep(100);
  99             } catch (InterruptedException ex) {}
 100             System.err.println("Calling Application.launch from currentThread="
 101                     + Thread.currentThread().getName());
 102             System.err.print("LAUNCHING...");
 103             System.err.flush();
 104             startTime = System.nanoTime();
 105             Application.launch(HelloLaunchOnNewThread.class, args);
 106             System.err.println("Application.launch returns");
 107         }).start();
 108         System.err.println("Main thread exiting: currentThread="
 109                 + Thread.currentThread().getName());
 110     }
 111 }