Solved

interrupt() v/s cancel()

Posted on 2012-03-28
2
468 Views
Last Modified: 2012-04-11
hi guys

what happens when Thread.currentthread.interrupt() is executed?
How is it different from Future.cancel() ?

"Threads created by Executor framework impliment an Interruption
policy that lets tasks be cancelled using interruption
."- Brian Goetz.

Can anyone provide internal code for this? Does it mean something like
Public class Thread implements InterruptionPolicy ..?
0
Comment
Question by:royjayd
2 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 150 total points
ID: 37777851
did you see this discussion:
http://stackoverflow.com/questions/2109288/java-thread-weird-thread-interrupted-and-future-canceltrue-behaviour

It looks like neither of them guarantees interruption

>Does it mean something like
>Public class Thread implements InterruptionPolicy ..?

I don't think so, it is more in figure of speech manner
this is source code of ThreadPoolExecutor:
http://fuseyism.com/classpath/doc/java/util/concurrent/ThreadPoolExecutor-source.html
and there is nothing there that says "inplements Interr..."
0
 
LVL 26

Accepted Solution

by:
dpearson earned 350 total points
ID: 37780243
This is the code that Future.cancel() ultimately calls:

        boolean innerCancel(boolean mayInterruptIfRunning) {
          for (;;) {
            int s = getState();
            if (ranOrCancelled(s))
                return false;
            if (compareAndSetState(s, CANCELLED))
                break;
          }
            if (mayInterruptIfRunning) {
                Thread r = runner;
                if (r != null)
                    r.interrupt();
            }
            releaseShared(0);
            done();
            return true;
        }

This means if you pass in the "mayInterruptIfRunning" as true then it will call interrupt() on the thread being used by the Executor.

Then the threads will shutdown when they next check to see if they've been interrupted.

That's what Goetz means by saying the Executor threads support being interrupted.

Doug
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
simple java question 3 55
difference of if loops 23 49
servlet web applications   metadata-complete="true" or false 3 37
hibernate example using maven 12 41
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question