From 1c897891cc1509bb6b5be8dc1202de0dce3d3172 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Mon, 24 Apr 2017 14:16:55 +0300 Subject: [PATCH] (refs #7168) User Thread.stop to terminate execution in SCL console Change-Id: I07b5bc2fd64953c8d67d19582cadcc348addb6e3 --- .../simantics/scl/ui/console/SCLConsole.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java index 59908c870..5eacaa093 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java @@ -1,7 +1,5 @@ package org.simantics.scl.ui.console; -import gnu.trove.set.hash.THashSet; - import java.util.ArrayList; import org.eclipse.core.runtime.IProgressMonitor; @@ -27,6 +25,8 @@ import org.simantics.scl.ui.Activator; import org.simantics.scl.ui.assist.SCLContentProposalProvider; import org.simantics.scl.ui.assist.StyledTextContentAdapter; +import gnu.trove.set.hash.THashSet; + /** * An SCL console with input and output area that can be embedded * into any editor or view. @@ -34,9 +34,9 @@ import org.simantics.scl.ui.assist.StyledTextContentAdapter; */ public class SCLConsole extends AbstractCommandConsole { public static final String JOB_NAME = "org.simantics.scl.console.job"; + public static final long TERMINATE_GRACE_PERIOD = 1000L; private THashSet currentJobs = new THashSet(); - private Thread currentThread; private final IdentitySchedulingRule schedulingRule = new IdentitySchedulingRule(); private ArrayList listeners = new ArrayList(2); private boolean consoleIsEmpty = true; @@ -126,14 +126,10 @@ public class SCLConsole extends AbstractCommandConsole { @Override protected IStatus run(IProgressMonitor monitor) { try { - synchronized(currentJobs) { - currentJobs.remove(this); - currentThread = Thread.currentThread(); - } session.execute(command, handler); } finally { synchronized(currentJobs) { - currentThread = null; + currentJobs.remove(this); if(currentJobs.isEmpty()) for(SCLConsoleListener listener : listeners) listener.finishedExecution(); @@ -141,6 +137,24 @@ public class SCLConsole extends AbstractCommandConsole { } return Status.OK_STATUS; } + + @Override + protected void canceling() { + Thread thread = getThread(); + if(thread != null) + thread.interrupt(); + + try { + Thread.sleep(TERMINATE_GRACE_PERIOD); + } catch (InterruptedException e) { + e.printStackTrace(); + return; + } + + thread = getThread(); + if(thread != null) + thread.stop(); + } }; job.setRule(schedulingRule); synchronized(currentJobs) { @@ -164,8 +178,6 @@ public class SCLConsole extends AbstractCommandConsole { for(Job job : currentJobs) job.cancel(); currentJobs.clear(); - if(currentThread != null) - currentThread.interrupt(); } } -- 2.47.1