01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.executors.impl;
18:
19: import java.util.concurrent.BlockingQueue;
20: import java.util.concurrent.ThreadPoolExecutor;
21: import java.util.concurrent.TimeUnit;
22:
23: import org.apache.servicemix.executors.Executor;
24:
25: /**
26: * The default Executor implementation which uses a
27: * ThreadPoolExecutor underneath.
28: *
29: * @author <a href="mailto:gnodet [at] gmail.com">Guillaume Nodet</a>
30: */
31: public class ExecutorImpl implements Executor {
32:
33: private final ThreadPoolExecutor threadPool;
34:
35: private final long shutdownDelay;
36:
37: public ExecutorImpl(ThreadPoolExecutor threadPool,
38: long shutdownDelay) {
39: this .threadPool = threadPool;
40: this .shutdownDelay = shutdownDelay;
41: }
42:
43: public void execute(Runnable command) {
44: threadPool.execute(command);
45: }
46:
47: public void shutdown() {
48: threadPool.shutdown();
49: if (!threadPool.isTerminated() && shutdownDelay > 0) {
50: new Thread(new Runnable() {
51: public void run() {
52: try {
53: if (!threadPool.awaitTermination(shutdownDelay,
54: TimeUnit.MILLISECONDS)) {
55: threadPool.shutdownNow();
56: }
57: } catch (InterruptedException e) {
58: // Do nothing
59: }
60: }
61: }).start();
62: }
63: }
64:
65: public int capacity() {
66: BlockingQueue queue = threadPool.getQueue();
67: return queue.remainingCapacity() + queue.size();
68: }
69:
70: public int size() {
71: BlockingQueue queue = threadPool.getQueue();
72: return queue.size();
73: }
74:
75: }
|