Java 任务可运行、线程、时间线对象是线程安全的吗?

Java 任务可运行、线程、时间线对象是线程安全的吗?,java,thread-safety,task,runnable,timeline,Java,Thread Safety,Task,Runnable,Timeline,我的应用程序中有任务、线程和时间线对象作为属性。并且有几个线程可以访问它们。我应该同步访问吗 线程对象的代码: public class CalculationThread { private Thread thread = new Thread("Clct") { public void run() { for (int i = 0; i < 100; i++) { System.out.print("\n"

我的应用程序中有任务、线程和时间线对象作为属性。并且有几个线程可以访问它们。我应该同步访问吗

线程对象的代码:

public class CalculationThread {
    private Thread thread = new Thread("Clct") {
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.print("\n" + i);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    break;

                }
            }
            return;

        }
    };
    private Thread interruptingThread = new Thread("Intrpt") {
        public void run() {
            try {
                Thread.sleep(7000);
            } catch (InterruptedException e) {
                return;
            }
            System.out.println("\nInterrupting thread:" + getName());
            thread.interrupt();
        }
    };


    public static void main(String[] args) {
        CalculationThread cthr= new CalculationThread();
        cthr.thread.start();
        cthr.interruptingThread.start();
    }
}    
公共类计算线程{
私有线程线程=新线程(“Clct”){
公开募捐{
对于(int i=0;i<100;i++){
系统输出打印(“\n”+i);
试一试{
睡眠(5000);
}捕捉(中断异常e){
打破
}
}
返回;
}
};
私有线程中断读取=新线程(“Intrpt”){
公开募捐{
试一试{
睡眠(7000);
}捕捉(中断异常e){
返回;
}
System.out.println(“\n错误启动线程:“+getName()”);
thread.interrupt();
}
};
公共静态void main(字符串[]args){
CalculationThread cthr=新的CalculationThread();
cthr.thread.start();
cthr.read.start();
}
}    

所以,线程属性在这里由主线程启动,并由中断读取属性中断。因此,在类似的情况下,thread对象似乎是线程安全的,或者说?

VGR,但这段代码表明,时间线仍然可以在另一个线程中使用:

public class FillingTimeLine extends Application {
private LongProperty lp = new SimpleLongProperty(0);
private Timeline timeline = new Timeline();

@Override
public void start(Stage primaryStage) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("\nSTARTING THREAD: " + Thread.currentThread().getName());
    StackPane spane = new StackPane();
    ProgressBar pb = new ProgressBar(0);
    pb.setMinSize(160, 21.5);
    pb.setMaxSize(160, 21.5);
    pb.setPrefSize(160, 21.5);

    pb.progressProperty()
            .bind(lp.divide(10000 * 1.0));  
    pb.setStyle("-fx-base:darkgray;-fx-accent:red;");
    spane.getChildren().add(pb);
    Scene scene = new Scene(spane, 300, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
    startFilling();
    stopFilling();


}

public void startFilling() throws InterruptedException {
    new Thread(() -> {
        System.out.println("\nSTARTING THREAD: " + Thread.currentThread().getName());
        timeline = new Timeline(new KeyFrame(Duration.seconds(0), new KeyValue(lp, 0)),
                new KeyFrame(Duration.seconds(10), new KeyValue(lp, 10000)));
        if (Thread.currentThread().isInterrupted()) {
            System.out.println("\n THR WAS INTERRUPTED!");
            return;
        }

        timeline.play();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            System.out.println("\n THR WAS INTERRUPTED!");
            return;
        }

    }).start();

}

public void stopFilling() {
new Thread(() -> {
        System.out.println("\nSTOPPING THREAD: " + Thread.currentThread().getName());
        try {
            Thread.sleep(4000);
            timeline.stop();
        } catch (InterruptedException e) {
            return;
        }

    }).start();
}
public static void main(String[] args) {
    launch(args);
}
}

还是我错了?

取决于您访问的内容以及访问的时间/方式。为什么不在代码中告诉我们您的意思呢?除非您采取步骤在正文中编写线程安全的代码,否则这些代码都不是线程安全的。时间线从来都不是线程安全的;与所有JavaFX类一样,它只能在JavaFX应用程序线程中使用。在任何其他线程中使用它都是错误的。