Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

interrupt() v/s cancel()

Posted on 2012-03-28
2
Medium Priority
?
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 600 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 28

Accepted Solution

by:
dpearson earned 1400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

618 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