11/*
2- * Copyright (c) 2010, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2010, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525
2626package javafx .concurrent ;
2727
28+ import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_CANCELLED ;
29+ import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_FAILED ;
30+ import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_RUNNING ;
31+ import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_SCHEDULED ;
32+ import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_SUCCEEDED ;
33+ import java .util .concurrent .Callable ;
34+ import java .util .concurrent .FutureTask ;
35+ import java .util .concurrent .atomic .AtomicReference ;
2836import javafx .application .Platform ;
2937import javafx .beans .property .BooleanProperty ;
3038import javafx .beans .property .DoubleProperty ;
4351import javafx .event .EventHandler ;
4452import javafx .event .EventTarget ;
4553import javafx .event .EventType ;
46- import java .util .concurrent .Callable ;
47- import java .util .concurrent .FutureTask ;
48- import java .util .concurrent .atomic .AtomicReference ;
49- import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_CANCELLED ;
50- import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_FAILED ;
51- import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_RUNNING ;
52- import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_SCHEDULED ;
53- import static javafx .concurrent .WorkerStateEvent .WORKER_STATE_SUCCEEDED ;
5454
5555/**
5656 * <p>
@@ -998,7 +998,8 @@ protected void failed() { }
998998 return cancel (true );
999999 }
10001000
1001- @ Override public boolean cancel (boolean mayInterruptIfRunning ) {
1001+ @ Override
1002+ public boolean cancel (boolean mayInterruptIfRunning ) {
10021003 // Delegate to the super implementation to actually attempt to cancel this thing
10031004 // Assert the modifyThread permission
10041005 boolean flag = super .cancel (mayInterruptIfRunning );
@@ -1014,12 +1015,30 @@ protected void failed() { }
10141015 // state flag will not be readable immediately after this call. However,
10151016 // that would be the case anyway since these properties are not thread-safe.
10161017 if (isFxApplicationThread ()) {
1018+ switch (getState ()) {
1019+ case FAILED :
1020+ case SUCCEEDED :
1021+ // a finished or failed task retains its state
1022+ return false ;
1023+ }
1024+
10171025 setState (Worker .State .CANCELLED );
10181026 } else {
1019- runLater (() -> setState (Worker .State .CANCELLED ));
1027+ runLater (() -> {
1028+ // the state must be accessed only in the fx application thread
1029+ switch (getState ()) {
1030+ case FAILED :
1031+ case SUCCEEDED :
1032+ // a finished or failed task retains its state
1033+ break ;
1034+ default :
1035+ setState (Worker .State .CANCELLED );
1036+ break ;
1037+ }
1038+ });
1039+ return flag ;
10201040 }
10211041 }
1022- // return the flag
10231042 return flag ;
10241043 }
10251044
0 commit comments