任务可运行对象,线程对象和时间轴对象是线程安全的吗?

时间:2019-05-06 12:27:37

标签: 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" + 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();
    }
}    

因此,线程属性在这里由主线程启动,并由interruptingThread-attrubute中断。那么,在类似情况下,线程对象似乎是线程安全的,或者?

1 个答案:

答案 0 :(得分:0)

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

}
    还是我错了?