ExecutorServiceHolder.java
01 /*
02  * Copyright 2009-2013 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 
17 package org.codehaus.griffon.runtime.util;
18 
19 import java.util.LinkedHashSet;
20 import java.util.Set;
21 import java.util.concurrent.ExecutorService;
22 
23 /**
24  @author Andres Almiray
25  @since 1.2.0
26  */
27 public class ExecutorServiceHolder {
28     private static final Set<ExecutorService> EXECUTOR_SERVICE_CACHE = new LinkedHashSet<ExecutorService>();
29 
30     public static ExecutorService add(ExecutorService executorService) {
31         if (executorService != null) {
32             EXECUTOR_SERVICE_CACHE.add(executorService);
33         }
34         return executorService;
35     }
36 
37     public static ExecutorService remove(ExecutorService executorService) {
38         EXECUTOR_SERVICE_CACHE.remove(executorService);
39         return executorService;
40     }
41 
42     public static void shutdownAll() {
43         for (ExecutorService executorService : EXECUTOR_SERVICE_CACHE) {
44             if (!executorService.isShutdown()) {
45                 executorService.shutdownNow();
46             }
47         }
48     }
49 }