为什么eclipse工作线程不会停止?

时间:2015-08-31 09:56:10

标签: java eclipse multithreading jobs

我有方法来停止点击按钮后调用的eclipse作业线程:

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;
        }
    }
}

并且有扩展Job类的类:

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);
        }
    }

}

为什么调用job.getThread()。interrupt时,task.execute(status)仍在运行?

我也在执行方法中创建了logger.info(...),它表明存在相同的线程。

我该如何停止工作?

感谢您的帮助。

0 个答案:

没有答案