From 60f01f46ba232ed6416054f0a6115cb2a9b70b4e Mon Sep 17 00:00:00 2001 From: OszkarSemerath Date: Sat, 10 Jun 2017 19:05:05 +0200 Subject: Migrating Additional projects --- .../unused/FunctionWithTimeout.xtend_ | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Solvers/Alloy-Solver/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_ (limited to 'Solvers/Alloy-Solver/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_') diff --git a/Solvers/Alloy-Solver/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_ b/Solvers/Alloy-Solver/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_ new file mode 100644 index 00000000..8c03af6e --- /dev/null +++ b/Solvers/Alloy-Solver/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_ @@ -0,0 +1,40 @@ +package hu.bme.mit.inf.dlsreasoner.alloy.reasoner.builder + +import org.eclipse.xtext.xbase.lib.Functions.Function0 +import java.util.concurrent.Executors +import java.util.concurrent.Callable +import java.util.concurrent.TimeUnit +import java.util.concurrent.TimeoutException +import java.util.concurrent.ExecutionException + +class FunctionWithTimeout { + val Function0 function; + + new(Function0 function) { + this.function = function + } + + def execute(long millisecs) { + if(millisecs>0) { + val executor = Executors.newCachedThreadPool(); + val task = new Callable() { + override Type call() { function.apply } + }; + val future = executor.submit(task); + try { + val result = future.get(millisecs, TimeUnit.MILLISECONDS); + return result + } catch (TimeoutException ex) { + // handle the timeout + } catch (InterruptedException e) { + // handle the interrupts + } catch (ExecutionException e) { + // handle other exceptions + } finally { + future.cancel(true); // may or may not desire this + } + return null + } + else return function.apply + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2