Community
Participate
Working Groups
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.
Oh, the cancel should also be in the synchronize block: public synchronized boolean cancel() { fCurrentThread.interrupt(); return super.cancel(); }
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 ***