View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "DefaultExecutorService.java".  Description: 
10  "Provides a global cached thread pool if Connections and Servers are not
11  initialized with their specific {@link ExecutorService} instances" 
12  
13  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
14  2001.  All Rights Reserved. 
15  
16  Contributor(s): ______________________________________. 
17  
18  Alternatively, the contents of this file may be used under the terms of the 
19  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
20  applicable instead of those above.  If you wish to allow use of your version of this 
21  file only under the terms of the GPL and not to allow others to use your version 
22  of this file under the MPL, indicate your decision by deleting  the provisions above 
23  and replace  them with the notice and other provisions required by the GPL License.  
24  If you do not delete the provisions above, a recipient may use your version of 
25  this file under either the MPL or the GPL. 
26  
27   */
28  package ca.uhn.hl7v2.concurrent;
29  
30  import java.util.concurrent.CompletionService;
31  import java.util.concurrent.ExecutorCompletionService;
32  import java.util.concurrent.ExecutorService;
33  import java.util.concurrent.Executors;
34  import java.util.concurrent.ThreadFactory;
35  import java.util.concurrent.atomic.AtomicInteger;
36  
37  /**
38   * Provides a global cached thread pool if Connections and Servers are not
39   * initialized with their specific {@link ExecutorService} instances.
40   */
41  public class DefaultExecutorService {
42  
43  	static ExecutorService defaultExecutorService;
44  
45  	public static <V> CompletionService<V> completionService(ExecutorService executor) {
46  		return new ExecutorCompletionService<V>(executor);
47  	}
48  
49  	public static synchronized ExecutorService getDefaultService() {
50  		if (defaultExecutorService == null || defaultExecutorService.isShutdown()) {
51  			defaultExecutorService = Executors.newCachedThreadPool(new MyThreadFactory());
52  		}
53  		return defaultExecutorService;
54  	}
55  
56  	/**
57  	 * @return true if the service was created by this class
58  	 */
59  	public static boolean isDefaultService(ExecutorService service) {
60  		return service == defaultExecutorService;
61  	}
62  
63  	/**
64  	 * @see {@link ExecutorService#shutdown()}
65  	 */
66  	public static void shutdown() {
67  		defaultExecutorService.shutdown();
68  	}
69  
70  	/**
71  	 * @see {@link ExecutorService#shutdownNow()}
72  	 */
73  	public static void shutdownNow() {
74  		defaultExecutorService.shutdownNow();
75  	}
76  
77  	private static class MyThreadFactory implements ThreadFactory {
78  
79  		private ThreadGroup group;
80  		private AtomicInteger threadNum;
81  
82  		public MyThreadFactory() {
83  			group = Thread.currentThread().getThreadGroup();
84  			threadNum = new AtomicInteger(1);
85  		}
86  
87  		public Thread newThread(Runnable theR) {
88  			String name = "hapi-worker-" + threadNum.getAndIncrement();
89  			Thread t = new Thread(group, theR, name, 0);
90  			if (t.isDaemon()) {
91  				t.setDaemon(false);
92  			}
93  			if (t.getPriority() != Thread.NORM_PRIORITY) {
94  				t.setPriority(Thread.NORM_PRIORITY);
95  			}
96  			return t;
97  		}
98  
99  	}
100 
101 }