You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by "Jarek Gawor (JIRA)" <ji...@apache.org> on 2008/08/05 18:34:44 UTC

[jira] Created: (HARMONY-5933) FutureTask.cancel() might not actaully cancel the task

FutureTask.cancel() might not actaully cancel the task
------------------------------------------------------

                 Key: HARMONY-5933
                 URL: https://issues.apache.org/jira/browse/HARMONY-5933
             Project: Harmony
          Issue Type: Bug
          Components: Classlib
    Affects Versions: 5.0M6
            Reporter: Jarek Gawor


In some circumstances java.util.concurrent.FutureTask.cancel() might not actaully cancel the task. The problem is in innerCancel() method:

1.        boolean innerCancel(boolean mayInterruptIfRunning) {
2.            int s = getState();
3.            if (ranOrCancelled(s) || !compareAndSetState(s, CANCELLED))
4.                return false;
5.            <cancel logic>

The issue is that the state (s) can change between line 2 and 3 and so compareAndSetState(s, CANCELLED) will return false and the cancel logic will not be performed (cancel() will return false but that task is not complete and it will continue to execute). This problem is more evident with periodic tasks where the state keep changing between 0 -> RUNNING, and RUNNING -> 0 (see innerRunAndReset()).

Looks like FutureTask implementation in Sun's JDK avoids this problem by adding a for (;;) loop around line 2 and 3:

for (;;) {
   int s = getState();
   if (ranOrCancelled(s)) {
     return false;
   }
   if (compareAndSetState(s, CANCELLED)) {
      break;
   }
}
<cancel logic>


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (HARMONY-5933) FutureTask.cancel() might not actaully cancel the task

Posted by "Nathan Beyer (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/HARMONY-5933?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12620112#action_12620112 ] 

Nathan Beyer commented on HARMONY-5933:
---------------------------------------

Without knowing where you got the code snippet mentioned as "Sun's JDK", I need to mention that you should avoid posting possibly copyrighted code. Most of the vendor's maintain copyright of their source code, so please be careful with what you're reading, copying and pasting.

Aside from that, is the FutureTask you're looking at from the Java SE 5, main trunk, or from the Java SE 6 branch. For the most part, we don't do development or maintenance on the code directly, rather we get drops of code from Doug Lea's concurrency site [1]. I'd suggest checking the code there and seeing what the state of this class is.

[1] http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/

> FutureTask.cancel() might not actaully cancel the task
> ------------------------------------------------------
>
>                 Key: HARMONY-5933
>                 URL: https://issues.apache.org/jira/browse/HARMONY-5933
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>    Affects Versions: 5.0M6
>            Reporter: Jarek Gawor
>
> In some circumstances java.util.concurrent.FutureTask.cancel() might not actaully cancel the task. The problem is in innerCancel() method:
> 1.        boolean innerCancel(boolean mayInterruptIfRunning) {
> 2.            int s = getState();
> 3.            if (ranOrCancelled(s) || !compareAndSetState(s, CANCELLED))
> 4.                return false;
> 5.            <cancel logic>
> The issue is that the state (s) can change between line 2 and 3 and so compareAndSetState(s, CANCELLED) will return false and the cancel logic will not be performed (cancel() will return false but that task is not complete and it will continue to execute). This problem is more evident with periodic tasks where the state keep changing between 0 -> RUNNING, and RUNNING -> 0 (see innerRunAndReset()).
> Looks like FutureTask implementation in Sun's JDK avoids this problem by adding a for (;;) loop around line 2 and 3:
> for (;;) {
>    int s = getState();
>    if (ranOrCancelled(s)) {
>      return false;
>    }
>    if (compareAndSetState(s, CANCELLED)) {
>       break;
>    }
> }
> <cancel logic>

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Closed: (HARMONY-5933) FutureTask.cancel() might not actaully cancel the task

Posted by "Tim Ellison (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/HARMONY-5933?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Tim Ellison closed HARMONY-5933.
--------------------------------

    Resolution: Invalid

Please re-open if this is shown to be a problem in the Harmony code.


> FutureTask.cancel() might not actaully cancel the task
> ------------------------------------------------------
>
>                 Key: HARMONY-5933
>                 URL: https://issues.apache.org/jira/browse/HARMONY-5933
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>    Affects Versions: 5.0M6
>            Reporter: Jarek Gawor
>
> In some circumstances java.util.concurrent.FutureTask.cancel() might not actaully cancel the task. The problem is in innerCancel() method:
> 1.        boolean innerCancel(boolean mayInterruptIfRunning) {
> 2.            int s = getState();
> 3.            if (ranOrCancelled(s) || !compareAndSetState(s, CANCELLED))
> 4.                return false;
> 5.            <cancel logic>
> The issue is that the state (s) can change between line 2 and 3 and so compareAndSetState(s, CANCELLED) will return false and the cancel logic will not be performed (cancel() will return false but that task is not complete and it will continue to execute). This problem is more evident with periodic tasks where the state keep changing between 0 -> RUNNING, and RUNNING -> 0 (see innerRunAndReset()).
> Looks like FutureTask implementation in Sun's JDK avoids this problem by adding a for (;;) loop around line 2 and 3:
> for (;;) {
>    int s = getState();
>    if (ranOrCancelled(s)) {
>      return false;
>    }
>    if (compareAndSetState(s, CANCELLED)) {
>       break;
>    }
> }
> <cancel logic>

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.