java - How do I start one thread for my code and one for a JavaFX Application? -


i'm trying run program using javafx. if using swing, have 1 class started main method, , have build gui class. give me 2 threads, normal thread application, , eventqueue. prevent blocking ui work.

so, tried have class created in static main method construct application class, , launch it. got runtimeexception because program calling launch method not subclass of application.

is there way have separation of threads, or have work inside thread given application class?

in javafx, i'm guessing in order prevent common threading errors present in many swing applications, startup process constrained (more or less) way things forces execute ui code on fx application thread:

public class myappstartclass extends application {      @override     public void start(stage primarystage) {         // method executed on fx application thread          // load ui , display here....     }  } 

in oracle jre, executing java myappstartclass (in contrast regular java application), cause instance of myappstartclass created, fx application thread started, , start method of created instance executed on fx application thread. (there bit more that, that's basic gist.)

if want support environments aren't aware of how execute javafx application (including many ides), can add main method forces happen, calling static application.launch() method:

public class myappstartclass extends application {      @override     public void start(stage primarystage) {         // method executed on fx application thread          // load ui , display here....     }      // support non-javafx-aware environments:     public static void main(string[] args) {         launch(args);     }  } 

note there's overloaded form of launch, can specify application subclass, have different main class (the use cases few):

public class mainclass {     public static void main(string[] args) {         application.launch(myappstartclass.class, args);     } } 

there 2 important features of launch method aware of:

  1. it can called once per jvm lifetime. calling second time throw exception.
  2. it block until javafx platform exits.

if want have thread doing work besides fx application thread, simplest way start directly start method:

public class myappstartclass extends application {      @override     public void start(stage primarystage) {          // start background thread background stuff:          new thread(() -> {             // background work...         }).start();           // ui work...     }  } 

if compare standard startup swing application:

public class myswingapp {      public static void main(string[] args) {          swingutilities.invokelater(() -> {             // ui work...         });          // background work...     } } 

the process kind of inverted compared swing. in swing (are supposed to) explicitly have startup method (main) specify ui run on awt event dispatch thread, , can other stuff in "current" thread in main executed. in javafx startup method (start) executed on ui thread, , if want stuff on thread explicitly launch one.

other rules pretty same: ui elements part of scene graph can modified on ui thread, etc. note javafx has specific concurrency api managing tasks in background thread , scheduling ui updates on fx application thread.

aside:

in theory suppose like:

public class mainclass {     public static void main(string[] args) {         new thread(() -> application.launch(myappstartclass.class, args)).start();          // "background thread" work here in (now free) main thread     } } 

but since idiom far usual setup, not recommend it. note should not have main method directly in application subclass, (i think) oracle jre (or environment knows how run javafx applications) might ignore main method , boot start method described @ top of post.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo