participate


JavaFX General - Threading in JavaFX 1.2
This question is not answered.

<<   Back to Forum  |   Give us Feedback Topics: « Previous | Next
This topic has 10 replies on 1 page.
lippyjka
Posts:1
Registered: 6/8/09
Threading in JavaFX 1.2   
Jun 8, 2009 3:39 AM
 
 
Hi,

I'm really stumped on how to create separate threads in JavaFX. I've tried everything I've found on the internet including using a java class that extends runnable and using EventQueue.invokeLater(). This just stops the JavaFX thread while running the Java code in invokeLater()

JavaFX 1.2 has included java.async.Task, but I have no idea how to use this, there are no resources on the internet.

At the moment, I'm calling RMI on a java servlet, and the GUI freezes while the methods wait for a return type.

Any help would be appreciated.
 
  raindrop
Posts:464
Registered: 4/11/06
Re: Threading in JavaFX 1.2   
Jun 8, 2009 4:25 AM (reply 1 of 10)  (In reply to original post )
 
 
JavaTaskBase can be used for an asynchronous java code execution.

For example:

MyRunnableFuture.java
import javafx.async.RunnableFuture;
 
public class MyRunnableFuture implements RunnableFuture{
 
    public void run() throws Exception {
        while(true){
            System.out.println("Inifinity Loop!");
        }
    }
 
}


Main.fx
class MyTask extends JavaTaskBase{
 
    override function create():RunnableFuture{
        new MyRunnableFuture();
    }
 
}
 
MyTask{}.start();
 
Surikov
Posts:52
Registered: 5/23/07
Re: Threading in JavaFX 1.2   
Jun 8, 2009 6:41 AM (reply 2 of 10)  (In reply to #1 )
 
 
JavaFX code should not be run from objects that support this interface.

RunnableFuture is useless for JavaFX.
 
  raindrop
Posts:464
Registered: 4/11/06
Re: Threading in JavaFX 1.2   
Jun 9, 2009 4:06 AM (reply 3 of 10)  (In reply to #2 )
 
 
It is also possible to run JavaFX code asynchronously by implemented Task class:

AsyncProgramMoveImpl.java
import javafx.async.*;
import com.sun.javafx.runtime.async.*;
 
 
public class AsyncProgramMoveImpl extends AbstractAsyncOperation{
    RunnableFuture runnable;
 
    AsyncProgramMoveImpl(RunnableFuture runnable, AsyncOperationListener listener) {
        super(listener);
        this.runnable = runnable;
    }
 
    @Override
    public Object call() throws Exception {
        runnable.run();
        return null;
    }
}



JavaFXTaskBase.fx
import javafx.async.Task;
import javafx.async.RunnableFuture;
import com.sun.javafx.runtime.async.AsyncOperationListener;
 
import java.lang.Exception;
 
 
public class JavaFXTaskBase extends Task {
 
    var peer: AsyncProgramMoveImpl;
 
    public var action:function();
 
    public override function start() : Void{
     if (peer == null) {
          peer = new AsyncProgramMoveImpl(RunnableFuture{ override function run(){ action() } }, asyncListener );
          peer.start();
        }
    }
 
    public override function stop() : Void{
    }
 
    var asyncListener = AsyncOperationListener {
        public override function onProgress(progressValue:Integer, progressMax:Integer):Void {}
        public override function onCompletion(value:Object):Void {}
        public override function onCancel():Void {}
        public override function onException(e:Exception):Void{}
   }
}


Main.fx
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
 
Stage {
    title: "Application title"
    scene: Scene {
        content: Button {
            text: "Asynch Operation"
            action: function() {
                JavaFXTaskBase{
                    action: function(){
                        while(true){
                            println("infinty loop!");
                        }
                    }
                }.start();
            }
        }
    }
}
 
Surikov
Posts:52
Registered: 5/23/07
Re: Threading in JavaFX 1.2   
Jun 9, 2009 4:14 AM (reply 4 of 10)  (In reply to #3 )
 
 
 
  raindrop
Posts:464
Registered: 4/11/06
Re: Threading in JavaFX 1.2   
Jun 9, 2009 4:20 AM (reply 5 of 10)  (In reply to #4 )
 
 
import crudfx.util.*;
...
Waiter{
  action:makeQuery
  onDone:updateGUI
  }.serve();
.

How does the Waiter class work?
 
Surikov
Posts:52
Registered: 5/23/07
Re: Threading in JavaFX 1.2   
Jun 9, 2009 4:33 AM (reply 6 of 10)  (In reply to #5 )
 
 
it works fine

8)

Sorry, i can't publish source of CRUDfx SDK yet. I need test compatibility with JavaFX v1.2.
 
  raindrop
Posts:464
Registered: 4/11/06
Re: Threading in JavaFX 1.2   
Jun 9, 2009 4:59 AM (reply 7 of 10)  (In reply to #6 )
 
 
Just show the JavaFX 1.1 code of the Waiter class if it is not a secret.
 
Surikov
Posts:52
Registered: 5/23/07
Re: Threading in JavaFX 1.2   
Jun 9, 2009 5:03 AM (reply 8 of 10)  (In reply to #7 )
 
 
нет смысла. Там ничего интересного. Если в целях самообразования то придётся ждать когда я протестю всё по версию 1.2. Отдельные куски я публиковать не буду.
 
  bestgo
Posts:1
Registered: 6/12/09
Re: Threading in JavaFX 1.2   
Jun 12, 2009 1:46 PM (reply 9 of 10)  (In reply to #8 )
 
 
I wrote a blog about asyn in JavaFX 1.2 at
http://blogs.sun.com/baechul/entry/javafx_1_2_async
Hope this help someone.

Edited by: bestgo on Jun 12, 2009 1:46 PM
 
AndrewHughes
Posts:212
Registered: 10/14/08
Re: Threading in JavaFX 1.2   
Oct 6, 2009 9:33 PM (reply 10 of 10)  (In reply to #9 )
 
 
So what happens if you create a JAVA thread on the JavaFX GUI thread?
    Thread daemonThread = new Thread(){
      @Override
      public void run(){
          //do stuff
      }
    };
    daemonThread.setDaemon(true);
    daemonThread.start();

Can/will the new thread still block/deadlock the gui thread? Or do new JAVA threads need to be created/forked off a RunnableFuture/Task? I've got deadlock, I can see how JavaFX thread/async works - but where JAVA threads belong isn't clear. We have a lot of daemon threaded Java code, I don't want to (and can't) re-write everything to be based on JavaFX's Async.

Cheers.
 
This topic has 10 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 54
  • Guests : 135

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums