activemq-cpp-3.8.2
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_ */
#define DECAF_CATCH_RETHROW(type)
Macro for catching and rethrowing an exception of a given type.
Definition: ExceptionDefines.h:27
Definition: RejectedExecutionException.h:31
An object that executes submitted decaf.lang.Runnable tasks.
Definition: Executor.h:87
An Executor that provides methods to manage termination and methods that can produce a Future for tra...
Definition: ExecutorService.h:56
A cancellable asynchronous computation.
Definition: FutureTask.h:58
Future< E > * submit(decaf::lang::Runnable *task, const E &result, bool takeOwnership=true)
Submits a Runnable task for execution and returns a Future representing that task.
Definition: ExecutorService.h:178
T * release()
Releases the Pointer held and resets the internal pointer value to Null.
Definition: Pointer.h:174
A TimeUnit represents time durations at a given unit of granularity and provides utility methods to c...
Definition: TimeUnit.h:62
Future< E > * submit(decaf::lang::Runnable *task, bool takeOwnership=true)
Submits a Runnable object for execution.
Definition: ExecutorService.h:224
Future< E > * submit(Callable< E > *task, bool takeOwnership=true)
Submits a value-returning task for execution and returns a Future pointer representing the pending re...
Definition: ExecutorService.h:130
PointerType get() const
Gets the real pointer that is contained within this Pointer.
Definition: Pointer.h:188
#define DECAF_CATCHALL_THROW(type)
A catch-all that throws a known exception.
Definition: ExceptionDefines.h:50
Definition: NullPointerException.h:32
#define DECAF_API
Definition: Config.h:29
Definition: ArrayList.h:39
A Future represents the result of an asynchronous computation.
Definition: Future.h:88
Interface for a runnable object - defines a task that can be run by a thread.
Definition: Runnable.h:29
Definition: Exception.h:38
Decaf&#39;s implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition: Pointer.h:53
virtual void setMark(const char *file, const int lineNumber)
Adds a file/line number to the stack trace.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
virtual ~ExecutorService()
Definition: ExecutorService.h:59