diff options
author | ArenBabikian <aren.babikian@mail.mcgill.ca> | 2019-10-25 04:15:39 -0400 |
---|---|---|
committer | ArenBabikian <aren.babikian@mail.mcgill.ca> | 2020-06-07 19:43:49 -0400 |
commit | 32a4f3392a7d0c44439c0c9b960ef1cfb5e3cc2f (patch) | |
tree | 0e67f50df5b4d9a42f0075e1e19be988eae59bf9 /Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/unused/FunctionWithTimeout.xtend_ | |
parent | mid-measurement push (diff) | |
download | VIATRA-Generator-32a4f3392a7d0c44439c0c9b960ef1cfb5e3cc2f.tar.gz VIATRA-Generator-32a4f3392a7d0c44439c0c9b960ef1cfb5e3cc2f.tar.zst VIATRA-Generator-32a4f3392a7d0c44439c0c9b960ef1cfb5e3cc2f.zip |
VAMPIRE: post-submission push
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 @@ | |||
1 | package hu.bme.mit.inf.dlsreasoner.alloy.reasoner.builder | ||
2 | |||
3 | import org.eclipse.xtext.xbase.lib.Functions.Function0 | ||
4 | import java.util.concurrent.Executors | ||
5 | import java.util.concurrent.Callable | ||
6 | import java.util.concurrent.TimeUnit | ||
7 | import java.util.concurrent.TimeoutException | ||
8 | import java.util.concurrent.ExecutionException | ||
9 | |||
10 | class 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 | ||