133 |
|
} |
134 |
|
|
135 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
136 |
< |
if (state != NEW) |
136 |
> |
if (!(state == NEW && |
137 |
> |
UNSAFE.compareAndSwapInt(this, stateOffset, NEW, |
138 |
> |
mayInterruptIfRunning ? INTERRUPTING : CANCELLED))) |
139 |
|
return false; |
140 |
< |
if (mayInterruptIfRunning) { |
141 |
< |
if (!UNSAFE.compareAndSwapInt(this, stateOffset, NEW, INTERRUPTING)) |
142 |
< |
return false; |
143 |
< |
try { // in case call to interrupt throws exception |
144 |
< |
Thread t = runner; |
145 |
< |
if (t != null) |
146 |
< |
t.interrupt(); |
147 |
< |
} finally { // final state |
148 |
< |
UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED); |
140 |
> |
try { // in case call to interrupt throws exception |
141 |
> |
if (mayInterruptIfRunning) { |
142 |
> |
try { |
143 |
> |
Thread t = runner; |
144 |
> |
if (t != null) |
145 |
> |
t.interrupt(); |
146 |
> |
} finally { // final state |
147 |
> |
UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED); |
148 |
> |
} |
149 |
|
} |
150 |
+ |
} finally { |
151 |
+ |
finishCompletion(); |
152 |
|
} |
149 |
– |
else if (!UNSAFE.compareAndSwapInt(this, stateOffset, NEW, CANCELLED)) |
150 |
– |
return false; |
151 |
– |
finishCompletion(); |
153 |
|
return true; |
154 |
|
} |
155 |
|
|