Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 162577 - [Jobs] Jobs cannot be canceled if in a blocking call because Thread.interrupt() is not called
Summary: [Jobs] Jobs cannot be canceled if in a blocking call because Thread.interrupt...
Status: RESOLVED DUPLICATE of bug 122986
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Runtime (show other bugs)
Version: 3.2   Edit
Hardware: PC All
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: platform-runtime-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 162585
  Show dependency tree
 
Reported: 2006-10-27 12:02 EDT by Michael Scharf CLA
Modified: 2006-10-27 14:53 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Scharf CLA 2006-10-27 12:02:34 EDT
I have a job that makes a blocking call. The only way to cancel the job is to call the Thread.interrupt() of the thread the job is running in:

    class BlockingJob extends Job {
        public BlockingJob() {
            super("Blocking Job");
        }
        protected IStatus run(IProgressMonitor monitor) {
            try {
                // the only way to get out is to call 
                // Thread.interrupt();
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                return Status.CANCEL_STATUS;
            }
            return Status.OK_STATUS;
        }

Unfortunately, Job.cancel() does *not* call Thread.interrupt();
The simple solution would be to override cancel and call Thread.interrupt()


    class BlockingJob extends Job {
        private Thread fCurrentThread;
        public BlockingJob() {
            super("Blocking Job");
        }
        protected IStatus run(IProgressMonitor monitor) {
            synchronized(this) {
                fCurrentThread=Thread.currentThread();
            }
            try {
                // the only way to get out is to call 
                // Thread.interrupt();
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                return Status.CANCEL_STATUS;
            }
            return Status.OK_STATUS;
        }
        public boolean cancel() {
            synchronized(this) {
                fCurrentThread.interrupt();
            }
            return super.cancel();
        }
    }


Unfortunately, this is not possible, because cancel is final :-(

It seems there is no way to cancel jobs that are in blocking calls.....


See also bug 157604.
Comment 1 Michael Scharf CLA 2006-10-27 12:19:17 EDT
Oh, the cancel should also be in the synchronize block:

        public synchronized boolean cancel() {
              fCurrentThread.interrupt();
              return super.cancel();
         }
Comment 2 John Arthorne CLA 2006-10-27 14:53:53 EDT
Note that also, depending on your specific situation, someone could interrupt a job by calling Job.getThread() and then calling interrupt() if a non-null thread is returned.

*** This bug has been marked as a duplicate of 122986 ***