|
14 | 14 | */
|
15 | 15 | public class AsyncExecutor {
|
16 | 16 | public static void executeAsync(Runnable action) {
|
17 |
| - try (ExecutorService executorService = Executors.newSingleThreadExecutor()) { |
18 |
| - try { |
19 |
| - executorService.submit( action ).get(); |
20 |
| - } |
21 |
| - catch (InterruptedException e) { |
22 |
| - throw new RuntimeException( "Thread interruption", e ); |
23 |
| - } |
24 |
| - catch (ExecutionException e) { |
25 |
| - throw new RuntimeException( "Async execution error", e ); |
26 |
| - } |
| 17 | + final ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 18 | + try { |
| 19 | + executorService.submit( action ).get(); |
| 20 | + } |
| 21 | + catch (InterruptedException e) { |
| 22 | + throw new RuntimeException( "Thread interruption", e ); |
| 23 | + } |
| 24 | + catch (ExecutionException e) { |
| 25 | + throw new RuntimeException( "Async execution error", e ); |
27 | 26 | }
|
28 | 27 | }
|
29 | 28 |
|
30 | 29 | public static void executeAsync(int timeout, TimeUnit timeoutUnit, Runnable action) {
|
31 |
| - try (ExecutorService executorService = Executors.newSingleThreadExecutor()) { |
32 |
| - try { |
33 |
| - executorService.submit( action ).get( timeout, timeoutUnit ); |
34 |
| - } |
35 |
| - catch (InterruptedException e) { |
36 |
| - throw new TimeoutException( "Thread interruption", e ); |
37 |
| - } |
38 |
| - catch (java.util.concurrent.TimeoutException e) { |
39 |
| - throw new TimeoutException( "Thread timeout exceeded", e ); |
40 |
| - } |
41 |
| - catch (ExecutionException e) { |
42 |
| - throw new RuntimeException( "Async execution error", e.getCause() ); |
43 |
| - } |
| 30 | + final ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 31 | + try { |
| 32 | + executorService.submit( action ).get( timeout, timeoutUnit ); |
| 33 | + } |
| 34 | + catch (InterruptedException e) { |
| 35 | + throw new TimeoutException( "Thread interruption", e ); |
| 36 | + } |
| 37 | + catch (java.util.concurrent.TimeoutException e) { |
| 38 | + throw new TimeoutException( "Thread timeout exceeded", e ); |
| 39 | + } |
| 40 | + catch (ExecutionException e) { |
| 41 | + throw new RuntimeException( "Async execution error", e.getCause() ); |
44 | 42 | }
|
45 | 43 | }
|
46 | 44 |
|
|
0 commit comments