00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_UTIL_CONCURRENT_EXECUTORSERVICE_H_
00019 #define _DECAF_UTIL_CONCURRENT_EXECUTORSERVICE_H_
00020
00021 #include <decaf/util/Config.h>
00022
00023 #include <decaf/lang/Runnable.h>
00024 #include <decaf/util/ArrayList.h>
00025 #include <decaf/util/concurrent/Future.h>
00026 #include <decaf/util/concurrent/FutureTask.h>
00027 #include <decaf/util/concurrent/Executor.h>
00028 #include <decaf/util/concurrent/TimeUnit.h>
00029 #include <decaf/lang/exceptions/InterruptedException.h>
00030
00031 namespace decaf {
00032 namespace util {
00033 namespace concurrent {
00034
00056 class DECAF_API ExecutorService : public Executor {
00057 public:
00058
00059 virtual ~ExecutorService() {}
00060
00076 virtual bool awaitTermination(long long timeout, const TimeUnit& unit) = 0;
00077
00083 virtual void shutdown() = 0;
00084
00095 virtual ArrayList<decaf::lang::Runnable*> shutdownNow() = 0;
00096
00102 virtual bool isShutdown() const = 0;
00103
00109 virtual bool isTerminated() const = 0;
00110
00129 template<typename E>
00130 Future<E>* submit(Callable<E>* task, bool takeOwnership = true) {
00131
00132
00133
00134 Pointer< FutureTask<E> > newTask(new FutureTask<E>(task, takeOwnership));
00135 Pointer< FutureTask<E> > proxy(newTask->clone());
00136
00137 try {
00138
00139
00140
00141
00142 this->doSubmit(newTask.get());
00143
00144
00145 newTask.release();
00146
00147 return proxy.release();
00148 } catch(decaf::util::concurrent::RejectedExecutionException& ex) {
00149
00150 newTask.release();
00151 ex.setMark(__FILE__, __LINE__);
00152 throw;
00153 }
00154 DECAF_CATCH_RETHROW(decaf::lang::exceptions::NullPointerException)
00155 DECAF_CATCHALL_THROW(decaf::lang::Exception)
00156 }
00157
00177 template<typename E>
00178 Future<E>* submit(decaf::lang::Runnable* task, const E& result, bool takeOwnership = true) {
00179
00180
00181
00182 Pointer< FutureTask<E> > newTask(new FutureTask<E>(task, result, takeOwnership));
00183 Pointer< FutureTask<E> > proxy(newTask->clone());
00184
00185 try {
00186
00187
00188
00189
00190 this->doSubmit(newTask.get());
00191
00192
00193 newTask.release();
00194
00195 return proxy.release();
00196 } catch(decaf::util::concurrent::RejectedExecutionException& ex) {
00197
00198 newTask.release();
00199 ex.setMark(__FILE__, __LINE__);
00200 throw;
00201 }
00202 DECAF_CATCH_RETHROW(decaf::lang::exceptions::NullPointerException)
00203 DECAF_CATCHALL_THROW(decaf::lang::Exception)
00204 }
00205
00223 template<typename E>
00224 Future<E>* submit(decaf::lang::Runnable* task, bool takeOwnership = true) {
00225
00226
00227
00228 Pointer< FutureTask<E> > newTask(new FutureTask<E>(task, E(), takeOwnership));
00229 Pointer< FutureTask<E> > proxy(newTask->clone());
00230
00231 try {
00232
00233
00234
00235
00236 this->doSubmit(newTask.get());
00237
00238
00239 newTask.release();
00240
00241 return proxy.release();
00242 } catch(decaf::util::concurrent::RejectedExecutionException& ex) {
00243
00244 newTask.release();
00245 ex.setMark(__FILE__, __LINE__);
00246 throw;
00247 }
00248 DECAF_CATCH_RETHROW(decaf::lang::exceptions::NullPointerException)
00249 DECAF_CATCHALL_THROW(decaf::lang::Exception)
00250 }
00251
00252 protected:
00253
00263 virtual void doSubmit(FutureType* future) = 0;
00264
00265 };
00266
00267 }}}
00268
00269 #endif