JavaFX:更改javafx.concurrent.Task中的文本区域

时间:2016-08-04 14:24:12

标签: java multithreading javafx

我已经阅读了很多信息,但仍有一些疑问。我有以下代码:

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();

此代码是否100%线程安全,与javafx主线程冲突的位置。我的意思是,我能100%确定LINE#2永远不会给我任何问题吗?我问,因为我严重怀疑这条线给了我下面的例外。也许这个问题是从周期调用的?:

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)

0 个答案:

没有答案