wood burning stoves*
The moose likes JavaFX and the fly likes JavaFX, Scene Builder, and basic issues with threads/tasks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JavaFX
Bookmark "JavaFX, Scene Builder, and basic issues with threads/tasks" Watch "JavaFX, Scene Builder, and basic issues with threads/tasks" New topic
Author

JavaFX, Scene Builder, and basic issues with threads/tasks

L Purcell
Greenhorn

Joined: Feb 19, 2012
Posts: 12
I've read and read, but I'm stumped in learning how to set up/use threads in FX. I set up a Task in the Controller which just sleeps a bit and then does an "updateMessage("Done!"). The Initialize method just displays the class name. In a button-handler I set up a thread (Thread initThread = new Thread(task) ) and a start. I successfully read/display that the thread is "alive" (hooray!), but I CANNOT getMessage. If I can't do THIS simple task, it's hopeless to go further. The rather excellent tutorials on concurrency in FX don't show the OTHER parts of the program, just the TASKs. I know I'm missing something basic, but again, I'm stumped. Help? (Additional question: the Task has a "return"; where does a returned value go (if I were to have one; mine is null for now)?? )
Thanks. Here is the code after the imports:


Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175



You may want to change the type parameter.
For anything else have a look at the javadoc

Edit: It just come to mind that the javadoc is not very clear about the thread the succeeded method is invoked on.


Please correct my English.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

I should have read your question and code more carefully...
I think you got the wrong idea about what your code does.
You are starting your thread. All good.
Then you are building a bomb.
You are sending your thread to rest for 3 seconds and you are sending the application thread to sleep for 5 seconds!
Never sleep the application thread!
The updateMessage method is trying to set the new message inside the application thread which is asleep.
I highly doupt that the developers of javafx made precautions for this kind of thing so your "Done!" String never reaches its property.
In addition to my last answer you could override the done method in your Task class as well.

Edit: Initializable is superceded by the FXML loader. You should read about it in the javadocs.
L Purcell
Greenhorn

Joined: Feb 19, 2012
Posts: 12
Manuel, thanks. And if this msg shows up twice, it's because it disappeared the first time! ?? Anyway, I have had some success, but I still cannot figure out how to use getMessage. Inspired by your suggestions, I switched to a Service instead of just a direct Task, and after a lot of trial and error, it worked -- in the sense that I could use the setOnSucceeded method (based on the Concurrency tutorial)! However, though I studied the FX API on Service, I still don't understand how to use getMessage. Any ideas would be most welcome! (I did remove Initialize - which I had gotten from a template and which is still in some tutorials and examples!)
Here is my modified code: L

Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

To your first question: I really dont know. The javadocs don't say anything to support that.
JavaFx is event driven.
Every event needs to have a stub somewhere unless you want to create an infinite loop or something like that.
The Button click is one event which triggers your handleButtonStartAction.
The success of the service triggers another event namely your handle method.
I am not really certain that you understand that concept.
To understand what you want to do i need to aks what you excpected to do with

Those are called directly after you started the thread and they do not care if the thread finished or not.
For testing purposes you might want to sleep your created thread in your service a while.
To get the message you might want to call wse.getSource().getMessage() in your handle method.
L Purcell
Greenhorn

Joined: Feb 19, 2012
Posts: 12
Manuel, thanks again! Re the getMessage command, I just wanted to test sending info from the Task thread to the app thread. (Eventually I will be dealing with a USB-device, a Phidget, and its API -- which works well, by the way -- by putting the interactions in threads, as I should, and sending info back to the app thread.) So far, I have only gotten to first base with this!
Re the getMessage method, I have a suspicion that maybe it needs a handler, too! And I'll try your suggestion in that regard as well.
I was thinking last night that I really need to dig deeper into handlers and listeners, etc; your note prompts me to do just that! Thanks again! (I'll look at the docs, tutorials, etc, but if you know of any good discussion for FX of handlers et al. I'd appreciate knowing.) L
**Edit: I tried your suggestion of adding wse.getSource().getMessage() in the handle, and it worked! Thanks! Now I'm studying WHY it worked by going through the Handling JavaFX Events tutorial. L
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaFX, Scene Builder, and basic issues with threads/tasks
 
Similar Threads
threads concurrency problem
unable to create a AsyncProvider webservice
Is this code starting multiple threads
Thread suspended ...How to fix it ?
Thread Safe primitive and an Object Array