Solved

interrupt() v/s cancel()

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now