Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
导致java.lang.IllegalStateException的新任务:即使使用Platform.runLater()wrap,也不在FX应用程序线程上_Java_Multithreading_Javafx - Fatal编程技术网

导致java.lang.IllegalStateException的新任务:即使使用Platform.runLater()wrap,也不在FX应用程序线程上

导致java.lang.IllegalStateException的新任务:即使使用Platform.runLater()wrap,也不在FX应用程序线程上,java,multithreading,javafx,Java,Multithreading,Javafx,我已尝试在Platform.runLater()中包装ObservableList,但它给了我一个错误: 从内部类引用的局部变量必须是final或final 有效最终 我想不出任何办法来纠正这个错误。这是我的密码: Task task = new Task<Void>() { @Override public Void call() { try {

我已尝试在Platform.runLater()中包装ObservableList,但它给了我一个错误:

从内部类引用的局部变量必须是final或final 有效最终

我想不出任何办法来纠正这个错误。这是我的密码:

    Task task = new Task<Void>() {
            @Override public Void call() {                
                try {

                    for (int i = 0; i < obLstDataset.size(); i++){
                        // **************************                        
                        //doing other stuff here
                        // ************************** 
                        if (pageVLEDEResult.contains("something")) {                            
                            Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                    obLstInvalid.add((String)obLstDataset.get(i));    
                                }
                            });
                        }

                        else if (pageVLEDEResult.contains("something else")){
                            if (i!=0){
                                i--;
                            }
                        }

                        updateProgress (i+1, obLstDataset.size());

                    }

                } catch (IOException | FailingHttpStatusCodeException ex) {
                    Logger.getLogger(VehicleLicenceExpiryDateEnquirer.class.getName()).log(Level.SEVERE, null, ex);
                }
                return null;
            }
        };
Task Task=新任务(){
@重写公共无效调用(){
试一试{
对于(int i=0;i
我认为您需要将runLater中访问的变量设置为final。此外,在for循环中修改for循环变量(在本例中为i)也是非常糟糕的做法。所以我把它重新写进了一个while循环

Task task = new Task<Void>() {
        @Override public Void call() {                
            try {
                int i=0;
                do {
                    // **************************                        
                    //doing other stuff here
                    // ************************** 
                    if (pageVLEDEResult.contains("something")) {                           
                        final List <not sure what class this is> fobLstInvalid = obLstInvalid;
                        final List <not sure what class this is> fobLstDataset = obLstDataset;
                        final int fi = i;

                        Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                                obLstInvalid.add((String)obLstDataset.get(fi));    
                            }
                        });
                    }

                    else if (pageVLEDEResult.contains("something else")){
                        if (i!=0){
                            i--;
                        }
                    }

                    updateProgress (i+1, obLstDataset.size());
                    i++;
                } while (i < obLstDataset.size())

            } catch (IOException | FailingHttpStatusCodeException ex) {
                Logger.getLogger(VehicleLicenceExpiryDateEnquirer.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }
    };
Task Task=新任务(){
@重写公共无效调用(){
试一试{
int i=0;
做{
// **************************                        
//在这里做其他事情
// ************************** 
如果(PageVledResult.contains(“某物”){
最终列表fobLstInvalid=obLstInvalid;
最终列表fobLstDataset=obLstDataset;
最终整数fi=i;
Platform.runLater(新的Runnable(){
@凌驾
公开募捐{
obLstInvalid.add((字符串)obLstDataset.get(fi));
}
});
}
else if(pagevledesult.contains(“其他内容”)){
如果(i!=0){
我--;
}
}
updateProgress(i+1,obLstDataset.size());
i++;
}而(i
是什么阻止您声明字段final?我以为它希望obLstInvalid或obLstDataset是final(它们已经是final了)。事实上,变量“i”才是最终的,完全没有。谢谢:)你能解释一下为什么改变变量i被认为是不好的做法吗?哇,我发现,特别是I变量,需要是final。我的OBERSABLEST VAR已经是决赛了,这就是为什么我如此困惑-我没有想到我!!谢谢修复。关于for循环中i变量的注释不是关于final的。这是关于for循环的含义。for循环的定义是在for循环顶部的循环条件中控制循环变量。如果我能正确格式化我的评论,你会看到在我关于不良实践的问题和我需要var作为最终变量以消除我所得到的错误之间有一段距离。感谢for循环技巧,我想它的可读性使事情变得更容易,但从技术角度看,我不明白为什么更改for循环变量会是一个如此糟糕的主意——不过我会记住:)