2 Replies Latest reply: Jan 2, 2013 4:41 AM by jsmith RSS

    Cannot start JavaFX Task using Thread.start()

    982423
      Well I am currently studying JavaFX and as a total beginner(but not in Java) I started reading the official tutorials in Java and I'm currently studying Concurrency in JavaFX. and I tried to create my first JavaFx Task Object and start it. This what I have tried so far
      package concurrency;
      
      import javafx.concurrent.Task;
      
      public class JavaTaskClass {
      
           /**
            * @param args
            */
           public static void main(String[] args) {
                
                //create task object
                Task<Integer> task = new Task<Integer>(){
                     @Override 
                     protected Integer call() throws Exception{
                          System.out.println("Background task started...");
                          int iterations;
                          for(iterations = 0; iterations < 10000; iterations++){
                               if(isCancelled()){
                                    break;
                               }
                               System.out.println("Iteration " + iterations);
                               Thread.sleep(3000);
                          }
                          return iterations;
                     }
                };
                
                //start the background task
                Thread th = new Thread(task);
                th.setDaemon(true);
                System.out.println("Starting background task...");
                th.start();
                
           }
      
      }
      
       
      but the task doesn't start. I don't see any messages in my console. Is there something I missed?

      Edited by: 979420 on Jan 1, 2013 10:02 PM

      Edited by: 979420 on Jan 1, 2013 10:02 PM

      Edited by: 979420 on Jan 1, 2013 10:03 PM

      Edited by: 979420 on Jan 1, 2013 10:49 PM
        • 1. Re: Cannot start JavaFX Task using Thread.start()
          jsmith
          Tasks are meant to be run in the context of a JavaFX application, like in the example below:
          import javafx.application.Application;
          import javafx.concurrent.Task;
          import javafx.scene.Scene;
          import javafx.scene.layout.StackPane;
          import javafx.scene.paint.Color;
          import javafx.stage.Stage;
          
          import java.io.IOException;
          
          public class TaskApp extends Application {
            public static void main(String[] args) { launch(args); }
          
            @Override public void start(final Stage stage) throws IOException {
              //create task object
              Task<Integer> task = new Task<Integer>(){
                @Override
                protected Integer call() throws Exception{
                  System.out.println("Background task started...");
                  int iterations;
                  for(iterations = 0; iterations < 10000; iterations++){
                    if(isCancelled()){
                      break;
                    }
                    System.out.println("Iteration " + iterations);
                    Thread.sleep(3000);
                  }
                  return iterations;
                }
              };
          
              //start the background task
              Thread th = new Thread(task);
              th.setDaemon(true);
              System.out.println("Starting background task...");
              th.start();
          
              stage.setScene(new Scene(new StackPane(), 200, 100, Color.BLANCHEDALMOND));
              stage.show();
            }
          }
          Also your code was setting the task thread as a daemon thread. The Java Virtual Machine exits when the only threads running are all daemon threads. So once your main routine finished (which would be really quickly), the program would just exit without doing anything much. Note that, in your example, even if you made the Task thread a non-daemon thread, it still doesn't work (likely because the Task class has some reliance on the JavaFX system being initialized). For your simple background process you could skip Task and just use a Runnable or a Callable (which doesn't require JavaFX and hence would work in a non-daemon thread invoked from main), but I guess it's just an experiment.
          • 2. Re: Cannot start JavaFX Task using Thread.start()
            982423
            Ah thank you! I was wondering what I did wrong. Well thank you for the information