activemq-cpp-3.6.0
ExecutorService.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 
18 #ifndef _DECAF_UTIL_CONCURRENT_EXECUTORSERVICE_H_
19 #define _DECAF_UTIL_CONCURRENT_EXECUTORSERVICE_H_
20 
21 #include <decaf/util/Config.h>
22 
23 #include <decaf/lang/Runnable.h>
24 #include <decaf/util/ArrayList.h>
30 
31 namespace decaf {
32 namespace util {
33 namespace concurrent {
34 
57  public:
58 
59  virtual ~ExecutorService() {}
60 
76  virtual bool awaitTermination(long long timeout, const TimeUnit& unit) = 0;
77 
83  virtual void shutdown() = 0;
84 
95  virtual ArrayList<decaf::lang::Runnable*> shutdownNow() = 0;
96 
102  virtual bool isShutdown() const = 0;
103 
109  virtual bool isTerminated() const = 0;
110 
129  template<typename E>
130  Future<E>* submit(Callable<E>* task, bool takeOwnership = true) {
131 
132  // Creates a new FutureTask to wrap the target task, and then creates a clone
133  // that will act as the proxy to return to the caller.
134  Pointer< FutureTask<E> > newTask(new FutureTask<E>(task, takeOwnership));
135  Pointer< FutureTask<E> > proxy(newTask->clone());
136 
137  try {
138  // Its safe to submit and allow the task to be executed only after creating
139  // the proxy so that if its run on the current thread and destroyed the
140  // proxy still holds a vlid reference to the inner FutureTask data keeping it
141  // from being destroyed before the caller has a chance to call get().
142  this->doSubmit(newTask.get());
143 
144  // No exception so we can release our ref, the executor owns it now.
145  newTask.release();
146 
147  return proxy.release();
149  // Policy will delete the submitted task
150  newTask.release();
151  ex.setMark(__FILE__, __LINE__);
152  throw;
153  }
156  }
157 
177  template<typename E>
178  Future<E>* submit(decaf::lang::Runnable* task, const E& result, bool takeOwnership = true) {
179 
180  // Creates a new FutureTask to wrap the target task, and then creates a clone
181  // that will act as the proxy to return to the caller.
182  Pointer< FutureTask<E> > newTask(new FutureTask<E>(task, result, takeOwnership));
183  Pointer< FutureTask<E> > proxy(newTask->clone());
184 
185  try {
186  // Its safe to submit and allow the task to be executed only after creating
187  // the proxy so that if its run on the current thread and destroyed the
188  // proxy still holds a vlid reference to the inner FutureTask data keeping it
189  // from being destroyed before the caller has a chance to call get().
190  this->doSubmit(newTask.get());
191 
192  // No exception so we can release our reference, the executor owns it now.
193  newTask.release();
194 
195  return proxy.release();
197  // Policy will delete the submitted task
198  newTask.release();
199  ex.setMark(__FILE__, __LINE__);
200  throw;
201  }
204  }
205 
223  template<typename E>
224  Future<E>* submit(decaf::lang::Runnable* task, bool takeOwnership = true) {
225 
226  // Creates a new FutureTask to wrap the target task, and then creates a clone
227  // that will act as the proxy to return to the caller.
228  Pointer< FutureTask<E> > newTask(new FutureTask<E>(task, E(), takeOwnership));
229  Pointer< FutureTask<E> > proxy(newTask->clone());
230 
231  try {
232  // Its safe to submit and allow the task to be executed only after creating
233  // the proxy so that if its run on the current thread and destroyed the
234  // proxy still holds a vlid reference to the inner FutureTask data keeping it
235  // from being destroyed before the caller has a chance to call get().
236  this->doSubmit(newTask.get());
237 
238  // No exception so we can release our ref, the executor owns it now.
239  newTask.release();
240 
241  return proxy.release();
243  // Policy will delete the submitted task
244  newTask.release();
245  ex.setMark(__FILE__, __LINE__);
246  throw;
247  }
250  }
251 
252  protected:
253 
263  virtual void doSubmit(FutureType* future) = 0;
264 
265  };
266 
267 }}}
268 
269 #endif /* _DECAF_UTIL_CONCURRENT_EXECUTORSERVICE_H_ */