aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_')
-rw-r--r--Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_40
1 files changed, 40 insertions, 0 deletions
diff --git a/Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_ b/Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_
new file mode 100644
index 00000000..4f001c72
--- /dev/null
+++ b/Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_
@@ -0,0 +1,40 @@
1package hu.bme.mit.inf.dlsreasoner.alloy.reasoner.builder
2
3import org.eclipse.xtext.xbase.lib.Functions.Function0
4import java.util.concurrent.Executors
5import java.util.concurrent.Callable
6import java.util.concurrent.TimeUnit
7import java.util.concurrent.TimeoutException
8import java.util.concurrent.ExecutionException
9
10class FunctionWithTimeout<Type> {
11 val Function0<Type> function;
12
13 new(Function0<Type> function) {
14 this.function = function
15 }
16
17 def execute(long millisecs) {
18 if(millisecs>0) {
19 val executor = Executors.newCachedThreadPool();
20 val task = new Callable<Type>() {
21 override Type call() { function.apply }
22 };
23 val future = executor.submit(task);
24 try {
25 val result = future.get(millisecs, TimeUnit.MILLISECONDS);
26 return result
27 } catch (TimeoutException ex) {
28 // handle the timeout
29 } catch (InterruptedException e) {
30 // handle the interrupts
31 } catch (ExecutionException e) {
32 // handle other exceptions
33 } finally {
34 future.cancel(true); // may or may not desire this
35 }
36 return null
37 }
38 else return function.apply
39 }
40} \ No newline at end of file