AbstractUIThreadManager.java
01 /*
02  * Copyright 2008-2014 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.codehaus.griffon.runtime.core.threading;
17 
18 import griffon.core.ExecutorServiceManager;
19 import griffon.core.GriffonExceptionHandler;
20 import griffon.core.threading.UIThreadManager;
21 
22 import javax.annotation.Nonnull;
23 import javax.inject.Inject;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 
29 import static java.util.Objects.requireNonNull;
30 
31 /**
32  @author Andres Almiray
33  @since 2.0.0
34  */
35 public abstract class AbstractUIThreadManager implements UIThreadManager {
36     protected static final String ERROR_RUNNABLE_NULL = "Argument 'runnable' must not be bull";
37     protected static final String ERROR_CALLABLE_NULL = "Argument 'callable' must not be null";
38 
39     private final ExecutorService defaultExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
40     private static final Thread.UncaughtExceptionHandler UNCAUGHT_EXCEPTION_HANDLER = new GriffonExceptionHandler();
41 
42     private ExecutorServiceManager executorServiceManager;
43 
44     @Inject
45     public void setExecutorServiceManager(@Nonnull ExecutorServiceManager executorServiceManager) {
46         requireNonNull(executorServiceManager, "Argument 'executorServiceManager' must not be bull");
47         if (this.executorServiceManager != null) {
48             this.executorServiceManager.remove(defaultExecutorService);
49         }
50         this.executorServiceManager = executorServiceManager;
51         this.executorServiceManager.add(defaultExecutorService);
52     }
53 
54     /**
55      * Executes a code block as a Future on an ExecutorService.
56      *
57      @param callable a code block to be executed
58      @return a Future that contains the result of the execution
59      */
60     @Nonnull
61     public <R> Future<R> runFuture(@Nonnull Callable<R> callable) {
62         requireNonNull(callable, ERROR_CALLABLE_NULL);
63         return runFuture(defaultExecutorService, callable);
64     }
65 
66     /**
67      * Executes a code block as a Future on an ExecutorService.
68      *
69      @param executorService the ExecutorService to use. Will use the default ExecutorService if null.
70      @param callable        a code block to be executed
71      @return a Future that contains the result of the execution
72      */
73     @Nonnull
74     public <R> Future<R> runFuture(@Nonnull ExecutorService executorService, @Nonnull Callable<R> callable) {
75         requireNonNull(executorService, "Argument 'executorService' must not be null");
76         requireNonNull(callable, ERROR_CALLABLE_NULL);
77         return executorService.submit(callable);
78     }
79 
80     public void runOutsideUI(@Nonnull final Runnable runnable) {
81         requireNonNull(runnable, ERROR_RUNNABLE_NULL);
82         if (!isUIThread()) {
83             runnable.run();
84         else {
85             defaultExecutorService.submit(new Runnable() {
86                 public void run() {
87                     try {
88                         runnable.run();
89                     catch (Throwable throwable) {
90                         UNCAUGHT_EXCEPTION_HANDLER.uncaughtException(Thread.currentThread(), throwable);
91                     }
92                 }
93             });
94         }
95     }
96 }