Java Reference
In-Depth Information
This is a pretty minimal JavaFX GUI application. Before letting you run this program, we point out several
features of the program. First of all, make a mental note of the
main()
method:
public static void main(String[] args) {
launch(args);
}
You have seen this method several times already. This stylized
main()
method always appears in a class that
extends the
javafx.application.Application
class. There is an overloaded version of the
Application.launch()
method that takes a
Class
object as the first parameter that can be called from other classes:
launch(Class<? Extends Application> appClass, String[] args)
Therefore you can move the
main()
method to another class:
public class Main {
public static void main(String[] args) {
Application.launch(JavaFXThreadsExample.class, args);
}
}
to achieve the same result.
Next, notice that the nested class
Model
builds up its data model, which consists of a list of all live threads and the
stack traces of each thread, in its
update()
method:
public void update() {
threadNames.clear();
stackTraces.clear();
final Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
for (Map.Entry<Thread, StackTraceElement[]> entry : map.entrySet()) {
threadNames.add("\"" + entry.getKey().getName() + "\"");
stackTraces.add(formatStackTrace(entry.getValue()));
}
}
This method is called once in the constructor of
Model
, which is called from the constructor of the
JavaFXThreadsExample
, and once from the event handler of the Update button.
When we run the program in Listing 7-7, the GUI in Figure
7-5
is displayed on the screen. You can explore the
threads in this JavaFX program by clicking on each thread name in the list and seeing the stack trace for that thread in
the text area. Here are some interesting observations:
main
” thread's call stack includes a call to
com.sun.javafx.application.LauncherImpl.
launchApplication()
.
•
The “
JavaFX-Launcher
” thread's call stack includes a call to
com.sun.javafx.application.
PlatformImpl.runAndWait()
. This puts code, including the invocation of the constructor, on
the JavaFX Application Thread.
•
The “
JavaFX Application Thread
” thread's call stack includes the native method
com.sun.
glass.ui.win.WinApplication._runLoop()
on a Windows box, and something similar on
Mac or Linux boxes.
•
The “
QuantumRenderer-0
” thread's call stack includes the method
com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run()
.
•
The “
Search WWH ::
Custom Search