]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7168) User Thread.stop to terminate execution in SCL console 59/459/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 24 Apr 2017 11:16:55 +0000 (14:16 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 24 Apr 2017 11:17:28 +0000 (14:17 +0300)
Change-Id: I07b5bc2fd64953c8d67d19582cadcc348addb6e3

bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/console/SCLConsole.java

index 59908c870a1e51c60474ab83cc18ea20f54bbfed..5eacaa093211d68d7d5d29ea8b36aa027c7b0668 100644 (file)
@@ -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<Job> currentJobs = new THashSet<Job>();
-       private Thread currentThread;
        private final IdentitySchedulingRule schedulingRule = new IdentitySchedulingRule();
        private ArrayList<SCLConsoleListener> listeners = new ArrayList<SCLConsoleListener>(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();
         }
     }