participate


JavaFX General - multithreading in JavaFX
This question is not answered.

<<   Back to Forum  |   Give us Feedback
This topic has 12 replies on 1 page.
Aniketscreen
Posts:14
Registered: 4/7/09
multithreading in JavaFX   
Apr 24, 2009 4:49 AM
 
 
I want to know how multithreading is done in JavaFX by using do and do later? can anybody give me demo source code of how it is done?
 
Fr3nchK1ss
Posts:8
Registered: 4/27/09
Re: multithreading in JavaFX   
Apr 27, 2009 1:00 AM (reply 1 of 12)  (In reply to original post )
 
 
I am wondering the same. I'm using the current version of javaFX SDK (1.1.1) and I don't know how to handle multithreading "by hand". I could read some examples with "do" and "do later" statements which don't exist anymore. We also can't invoke a thread directly, using a class extending Thread and calling the method start().

I have several Timeline variables running in a CustomNode, and when I want to bind a simple Integer updated every second then displayed, my display thread (the Timelines) are totally frozen every seconds for a few milliseconds, then move again. Multithreading seems to be automatically handled or it's well hidden, at least from my sight.

Any idea?

Regards
 
alexfromsarkoland
Posts:158
Registered: 5/5/07
Re: multithreading in JavaFX   
Apr 27, 2009 1:55 AM (reply 2 of 12)  (In reply to #1 )
 
 
There is only one thing for threading in javaFX: AsyncOperation.
There's
 
alexfromsarkoland
Posts:158
Registered: 5/5/07
Re: multithreading in JavaFX   
Apr 27, 2009 2:02 AM (reply 3 of 12)  (In reply to #2 )
 
 
Suite...
The AbstractAsyncOperation use FutureTask
If you want use Thread in your programm:

You have to use Runnable because Thread is not supported
Execute it in a ExecutorService for exemple.
If you handle bound variable in your Thread, make a local copy of your variable for your Runnable to use it.
After task completion, to refreshing your variable update it in a FX.defererAction function
 
alexfromsarkoland
Posts:158
Registered: 5/5/07
Re: multithreading in JavaFX   
Apr 27, 2009 2:12 AM (reply 4 of 12)  (In reply to #3 )
 
 
Here's the code

def pool : ExecutorService = Executors.newCachedThreadPool();
public class MultiThreading {
    public var str: String = "string";
}
function process(multi: MultiThreading) {
    var s = multi.str;
    var task: Runnable = Runnable {
        override function run() {
            s = "{s}->changed by a Thread";
            FX.deferAction(function(): Void { multi.str = s });
        }
    }
    pool.execute(task);
}
function run() {
    var multi = MultiThreading{};
    Stage {
        title : "MyApp"
        scene: Scene {
            width: 200
            height: 200
            content: VBox {
                spacing: 20
                content: [
                    SwingButton {
                        text: "Button"
                        action: function() { process(multi) }
                    }
                    Text {
                        wrappingWidth: 200
                        font : Font {
                            size: 16
                        }
                        textOrigin: TextOrigin.TOP
                        content: bind multi.str
                    }
                ]
            }
        }
    }
}
 
alexfromsarkoland
Posts:158
Registered: 5/5/07
Re: multithreading in JavaFX   
Apr 27, 2009 2:13 AM (reply 5 of 12)  (In reply to #3 )
 
 
Here's the code

def pool : ExecutorService = Executors.newCachedThreadPool();
public class MultiThreading {
    public var str: String = "string";
}
function process(multi: MultiThreading) {
    var s = multi.str;
    var task: Runnable = Runnable {
        override function run() {
            s = "{s}->changed by a Thread";
            FX.deferAction(function(): Void { multi.str = s });
        }
    }
    pool.execute(task);
}
function run() {
    var multi = MultiThreading{};
    Stage {
        title : "MyApp"
        scene: Scene {
            width: 200
            height: 200
            content: VBox {
                spacing: 20
                content: [
                    SwingButton {
                        text: "Button"
                        action: function() { process(multi) }
                    }
                    Text {
                        wrappingWidth: 200
                        font : Font {
                            size: 16
                        }
                        textOrigin: TextOrigin.TOP
                        content: bind multi.str
                    }
                ]
            }
        }
    }
}
 
PhiLho
Posts:1,533
Registered: 17/05/06
Re: multithreading in JavaFX   
Apr 27, 2009 3:49 AM (reply 6 of 12)  (In reply to #3 )
 
 
alexfromsarkoland wrote:
Suite...

FYI, Alex, you have an Edit button (for a limited time) on the top-right corner of each of your answers. :-)
Not sure if you can delete your duplicate post, though (at best you can trim it down).

And thanks for the useful info and example! ;-)
 
alexfromsarkoland
Posts:158
Registered: 5/5/07
Re: multithreading in JavaFX   
Apr 27, 2009 8:01 AM (reply 7 of 12)  (In reply to #6 )
 
 
DSL PhiLo j'avais la tête dans le cul ce matin.
Bye!
 
  raghunair
Posts:6
Registered: 4/27/09
Re: multithreading in JavaFX   
Apr 27, 2009 10:07 AM (reply 8 of 12)  (In reply to #7 )
 
 
You can schedule task using Timeline .
Here is the code.
var timer:Timeline = null;

public function scheduleAction(timeout:Integer,action:function()) {
var timer: Timeline = Timeline {
repeatCount: 1
keyFrames: KeyFrame {
time: timeout
action: action
}
}
timer.play();
}
 
  raghunair
Posts:6
Registered: 4/27/09
Re: multithreading in JavaFX   
Apr 27, 2009 10:14 AM (reply 9 of 12)  (In reply to #8 )
 
 
There is correction instead Integer you have to use Duration.
Here is the code.
var timer:Timeline = null;

public function scheduleAction(timeout:Duration,action:function()) {
var timer: Timeline = Timeline {
repeatCount: 1
keyFrames: KeyFrame {
time: timeout
action: action
}
}
timer.play();
}
You can use this like this

scheduleAction(5s, function () { <<your action here >> }

your action will be executed after 5seconds.
 
Fr3nchK1ss
Posts:8
Registered: 4/27/09
Re: multithreading in JavaFX   
Apr 28, 2009 2:16 AM (reply 10 of 12)  (In reply to #9 )
 
 
I'm currently using the Timeline trick in order to thread a few animations. Everything is fine as long as I don't use any binding operation into another Node of the scene! Here is the wierd behavior:

My scene has two CustomNode, one performing 3 Timeline animations at the same time within a single Timeline called every second, and another one displaying a simple digital watch with the current time updated every second too. I added a draggable behavior to the 2nd CustomNode.

When I start the application, whenever the 2nd node (the digital watch) is refreshed (simple Text component binded on a local attribute), the 1st Node's animations are frozen. It's like the binding is blocking the Timelines from the other Node. But! If I move the 2nd Node over the 1st one, no more freezes!.

Here is some code, I omitted a lot to focus on the threading part only:

The 1st Node animations:

init {
        var timeline = Timeline {
            repeatCount: Timeline.INDEFINITE
            keyFrames : [
                KeyFrame {
                    time : 1s
                    canSkip : true
                    action: function() { (m_layerList[1] as TracksLayer).refreshTracks(); }
                }
            ]
        }
        timeline.play();
    }
 
 
public function refreshTracks(): Void {
 
        [...]
 
        for (track in tracks) {
 
            [...]
 
                // On anime la piste.
                var anim = Timeline {
                    repeatCount: 1
                    keyFrames : [
                        at(1s) {
                            trackNode.track.translateX => tmpPos[0] tween Interpolator.EASEBOTH;
                            trackNode.track.translateY => tmpPos[1] tween Interpolator.EASEBOTH;
                        }
                    ]
                }
                anim.play();
                
            }
        }
    }

The 2nd Node displaying time:

init {
        var timeline = Timeline {
            repeatCount: Timeline.INDEFINITE
            keyFrames : [
                KeyFrame {
                    time : 1s
                    action: function() { nextTick(); }
                }
            ]
        }
        timeline.play();
    }
 
public function nextTick() {
        now = Calendar.getInstance();
        m_time = "{now.get(Calendar.HOUR)}:{now.get(Calendar.MINUTE)}:{now.get(Calendar.SECOND)}";
    }
 
var dateEV = EVNode {
        m_EVModel: EVModel {
            m_width: m_width
            m_height: m_height
            m_title: bind "{m_date} - {m_time}" // The evil binding!
        }
    }


Added {code}
 
Fr3nchK1ss
Posts:8
Registered: 4/27/09
Re: multithreading in JavaFX   
Apr 28, 2009 7:41 AM (reply 11 of 12)  (In reply to #10 )
 
 
Oh my god! I just figured out where it came from. That was not related to any multithread issue or something, it was only the fill property of my CustomNodes. I was using a LinearGradient, and when it's applied to a big area such as my digital watch on the right (500 x 1200), it slows down everything. I simply removed this gradient and the freezes were gone.

In order to get rid of this issue AND to keep my nice LinearGradient, I had to set the "cache" property to "true". If not, it seems that each time the node's content is updated, the whole LinearGradient is drawn again, which leads to the freezes.
 
alexfromsarkoland
Posts:158
Registered: 5/5/07
Re: multithreading in JavaFX   
Apr 28, 2009 12:27 PM (reply 12 of 12)  (In reply to #11 )
 
 
There is a big missunderstanding about this post:
Use of TimeLine is NOT a way to to real Threading, it just allow to handle simply graphic stuff.
Test a very long and intensive task in a TimeLine, you should see the UI less responsive, worst test a blocking task like I/O in a TimeLine, you should be surprised.
If you profile your javaFX app you can see a specific pool wich handle TimeLine, etc.

So the only way to do REAL multithreading in JavaFX is using java Thread API with Runnables and updating UI throw EventQueue.invokeLater() or FX.deferAction()
 
This topic has 12 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 : 28
  • Guests : 129

About Sun forums
  • Oracle 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 Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums