?
Solved

interrupt() v/s cancel()

Posted on 2012-03-28
2
Medium Priority
?
499 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 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