Skip to main content

Multi-threading

6 replies [Last post]
cdouglass
Offline
Joined: 2005-08-24
Points: 0

Is the AbstractAsyncOperation supposed be the basis for multi-threading? I'm not thinking of animation (which is what TimeLine seems to be for) but simply running a long task that you don't want to block the event thread?

I tried this:

<br />
public class ChartScan extends AbstractAsyncOperation {</p>
<p>    protected function onCompletion(o: Object): Void {<br />
        logger.debug("Finsihed async op");<br />
    }</p>
<p>    protected function start(): Void {<br />
        logger.debug("Start async op");<br />
    }</p>
<p>    public function cancel(): Void {<br />
        throw new UnsupportedOperationException("Unable to cancel");<br />
    }<br />
}<br />

and then added this to my data model class:

<br />
    postinit {<br />
        var chartScanner = ChartScan {<br />
            onDone: function(dunno: Boolean) {<br />
                logger.debug("onDone called");<br />
            }<br />
        }<br />
    }<br />

When it runs the only output is the "Start async op". None of the other debug statements is ever reached.

What am missing?

Chas Douglass

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
michael_heinrichs
Offline
Joined: 2007-11-15
Points: 0

Btw. what are your typical use-cases when going multi-threaded? If these are common tasks, it's probably a good idea to provide classes for them.

cdouglass
Offline
Joined: 2005-08-24
Points: 0

Thanks for the great replies.

My current project is some navigational charting software. I have a pure Java version using Piccolo, but JavaFX looks like a great way to do it, so I've been experimenting with it.

Building an index of charts on the users computer is a good example, since it is easy to have 100's of them.

Also, loading the chart requires decoding some relatively simple (BSB) and some complicated (ENC) image building. BSB actually builds an RGB image from pixel level data, while ENC is actually a large scenegraph with many geometric figures, small images, text, etc.

Even loading a cached PNG version of the BSB image takes a few seconds to load on a pretty fast computer.

Finally, reading real-time GPS data coming in over a serial port simply cries out for a separate thread.

My plan was to use the current Java classes that do the file scanning/decoding, but I want those loads to run in a separate thread.

Another application I have, also currently started as a pure Java project, is a front-end to a multi-user game. All game "actions" require a message/response to a server over a socket, and I don't want the game UI to lock up while waiting for a response.

Yet another application is very similar, except that a configuration tool front-end communicates with a backend server over RMI. I haven't started looking at how to do that in JavaFX.

I'm not sure there is much there that can be "generalized" (except, perhaps the RMI).

I'll look at using Java threading for all these, I don't have a problem with that.

Chas Douglass

alexsch
Offline
Joined: 2007-07-12
Points: 0

> Can I use the Java classes? I guess I'll go
> experiment with that before I give up on JavaFX.

For example:

[code]
import java.lang.Thread;
import java.lang.Runnable;
import java.lang.System;

var thread1 = new Thread(
Runnable{
public function run () {
while(true){
System.out.println("Thread 1");
}
}
}
);

var thread2 = new Thread(
Runnable{
public function run () {
while(true){
System.out.println("Thread 2");
}
}
}
);

thread1.start();
thread2.start();
[/code]

michael_heinrichs
Offline
Joined: 2007-11-15
Points: 0

JavaFX cannot be used to write multi-threaded applications, at least for now.

Instead there are going to be some classes to do the most common tasks, which need to be done in another thread. There will be specific classes for specific tasks for example RemoteTextDocument allows to load a text-document asynchronously.

AFAIK the whole async-package is still under development and the fact that AbstractAsyncOperation is public is probably a bug.

cdouglass
Offline
Joined: 2005-08-24
Points: 0

That's a major disappointment -- one of the features that would really separate JavaFX from Flex would be support for multi-threading. And it was available in the scripting version.

And, this pretty much pushes JavaFX out of the realm of usefulness to me. I can still write a simple game, I suppose, but I can't remember the last Java app I wrote that wasn't multi-threaded.

Can I use the Java classes? I guess I'll go experiment with that before I give up on JavaFX.

Chas Douglass

michael_heinrichs
Offline
Joined: 2007-11-15
Points: 0

Yes, you can use Java classes to do that (in fact I just finished a project using JavaFX with a multi-threaded Java application). It's quite easy to use both languages together, e.g. using JavaFX for the UI (important part of a great game ;) ) and Java for the more sophisticated processes in the background (game-logic, AI), if JavaFX is not sufficient. Naturally in that case it is up to you to take care of synchronization issues. I used a combination of SwingUtilities.invoke...-calls and the DeferredTask-class from JavaFX.

There are some ideas for the future to come up with a subset of JavaFX, which allows to write safe multi-threaded application in an easy manner, but that is definitely not a 1.0-feature. So even if you give up on JavaFX for now, you should keep it on your radar. :)