0

I have method to stop eclipse job thread which is called after button is clicked:

public void stop(final Task task){
    for (TaskWrapperJob job : jobsList){
        if (job.getTask().id().equals(task.id())){
            job.getThread();
            if (!Thread.interrupted()) {
                try {
                    job.getThread().interrupt();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            finished(task, TaskResult.stopped());
            jobsList.remove(job);
            break;
        }
    }
}

And have class which extends Job class:

private class TaskWrapperJob extends Job {

    private final Task task;

    private final StatusController status;

    private TaskWrapperJob(final Task task, final StatusController status) {
        super(task.name());
        this.task = task;
        this.status = checkNotNull(status);
    }

    private Task getTask(){
        return task;
    }

    @Override protected IStatus run(final IProgressMonitor monitor) {
        started(task);
        logger.info("AFTER START");
        final TaskResult result = runTask();
        logger.info("BEFORE END");
        finished(task, result);
        return Status.OK_STATUS;
    }

    private TaskResult runTask() {
        try {
            return task.execute(status);
        } catch (final Throwable e) {
            return TaskResult.failed("Uncought exception during execution", e);
        }
    }

}

Why when job.getThread().interrupt is called, task.execute(status) is still running?

I make logger.info(...) in execute method too and it shows that there is the same thread.

How should I stop job?

Thanks for help.

xav9211
  • 191
  • 1
  • 3
  • 14
  • Probably because the code running in the thread is not implemented to be interruptable. http://stackoverflow.com/questions/3590000/what-does-java-lang-thread-interrupt-do – Gimby Aug 31 '15 at 10:00
  • Thread.interrupt does not cause the thread to stop immediately - see http://stackoverflow.com/q/3590000/2670892 Also you should use the `IJobManager` interface to manage jobs – greg-449 Aug 31 '15 at 10:03
  • So what is the best possible way to make it work? – xav9211 Aug 31 '15 at 10:44

0 Answers0