0

I have read a lot of information, but still have some doubts. I have the following code:

import javafx.concurrent.Task; 
...
Task<Void> someTask = new Task<Void>() {
    @Override
    public Void call() throws Exception {
        ....
        while(repeat){
            ...//here some method, for example rmi call
             textArea.setText("This my new text")//LINE #2
        }
        return null;   
    }
};
Thread someThread=new Thread(someTask);
someThread.setDaemon(true);
someThread.start();

Is this code 100% thread safe from position with conflicting with javafx main thread. I mean can I be 100% sure that LINE #2 will never give me any problems? I ask, because I have a serious suspicion that this line gives me the exception below. Maybe the problem is that is called from cycle?:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.sun.javafx.text.PrismTextLayout.addTextRun(PrismTextLayout.java:755)
    at com.sun.javafx.text.GlyphLayout.addTextRun(GlyphLayout.java:140)
    at com.sun.javafx.text.GlyphLayout.breakRuns(GlyphLayout.java:312)
    at com.sun.javafx.text.PrismTextLayout.buildRuns(PrismTextLayout.java:770)
    at com.sun.javafx.text.PrismTextLayout.layout(PrismTextLayout.java:1021)
    at com.sun.javafx.text.PrismTextLayout.ensureLayout(PrismTextLayout.java:223)
    at com.sun.javafx.text.PrismTextLayout.getBounds(PrismTextLayout.java:246)
    at javafx.scene.text.Text.getLogicalBounds(Text.java:358)
    at javafx.scene.text.Text.impl_computeGeomBounds(Text.java:1168)
    at javafx.scene.Node.updateGeomBounds(Node.java:3556)
    at javafx.scene.Node.getGeomBounds(Node.java:3509)
    at javafx.scene.Node.getLocalBounds(Node.java:3457)
    at javafx.scene.Node.updateTxBounds(Node.java:3620)
    at javafx.scene.Node.getTransformedBounds(Node.java:3403)
    at javafx.scene.Node.updateBounds(Node.java:538)
    at javafx.scene.Parent.updateBounds(Parent.java:1709)
    at javafx.scene.Parent.updateBounds(Parent.java:1709)
    at javafx.scene.Parent.updateBounds(Parent.java:1709)
    at javafx.scene.Parent.updateBounds(Parent.java:1709)
    at javafx.scene.Parent.updateBounds(Parent.java:1709)
    at javafx.scene.Parent.updateBounds(Parent.java:1709)
    at javafx.scene.Parent.updateBounds(Parent.java:1709)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2404)
    at com.sun.javafx.tk.Toolkit.lambda$runPulse$31(Toolkit.java:348)
    at com.sun.javafx.tk.Toolkit$$Lambda$212/599905281.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:347)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:374)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$405(QuantumToolkit.java:319)
    at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$51/902578932.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$50(GtkApplication.java:139)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$47/1352483365.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • You can't update the UI from a background thread. (So you can be pretty much certain that that line **will** give you problems.) This [documentation example](http://stackoverflow.com/documentation/javafx/2230/threading/7291/updating-the-ui-using-platform-runlater#t=20160804142846102114) may be helpful. – James_D Aug 04 '16 at 14:29
  • @James_D I've read about Platform.runLater. However, I had used it before and got the same problem. After reading this http://stackoverflow.com/a/31414801/5057736 I understood that there are two ways to work with additional threads: 1) Thread + Plaform.runLater - classic way 2)Use javafx specific classes for myltithreading. That's why I went the second way. – Pavel_K Aug 04 '16 at 14:32
  • @James_D from https://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html Because the Task is designed for use with JavaFX GUI applications, it ensures that every change to its public properties, as well as change notifications for state, errors, and for event handlers, `all occur on the main JavaFX application thread`. Accessing these properties from a background thread (including the call() method) will result in runtime exceptions being raised. – Pavel_K Aug 04 '16 at 14:34
  • But you are still updating the UI from a background thread. The fact you are using a `Task` changes nothing. As is [well-documented](http://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html), you cannot do that. You need to make that change; even if it isnot the cause of the stack trace you posted. – James_D Aug 04 '16 at 14:34
  • "Every change to its public properties". But you are not changing any of the task's properties, you are changing the text in your text field. Again, **you cannot do that from a background thread**. – James_D Aug 04 '16 at 14:35
  • @James_D So, do I understand you right? I need to do the following in LINE#2 `Platform.runLater(()->{textArea.setText("This my new text");});` ? Right? – Pavel_K Aug 04 '16 at 14:37
  • Yes, exactly. Whether or not it solves the problem is another question, but you need to schedule that update on the FX Application Thread. – James_D Aug 04 '16 at 14:38
  • @James_D Thank you very much, I will change and see the result. I know you are expert in JavaFX. – Pavel_K Aug 04 '16 at 14:39
  • @James_D Let me ask you one more question - if I press button then the handler is executed by JavaFx main thread? – Pavel_K Aug 04 '16 at 14:47
  • Yes, events are executed on the JavaFX application thread: the same one that is (effectively) used for rendering the scene, and on which you must make any updates to the UI. – James_D Aug 04 '16 at 14:50
  • @James_D Thank you for your help and time! – Pavel_K Aug 04 '16 at 14:51

0 Answers0