Solved

interrupt() v/s cancel()

Posted on 2012-03-28
2
473 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 27

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 43
check java version using powershell 13 171
null output 3 35
pagenation logic how it is working in my code 1 54
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 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