00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_UTIL_CONCURRENT_EXECUTORS_H_
00019 #define _DECAF_UTIL_CONCURRENT_EXECUTORS_H_
00020
00021 #include <decaf/util/Config.h>
00022
00023 #include <decaf/lang/Thread.h>
00024 #include <decaf/lang/Runnable.h>
00025 #include <decaf/util/concurrent/Callable.h>
00026
00027 #include <decaf/lang/exceptions/NullPointerException.h>
00028
00029 namespace decaf {
00030 namespace util {
00031 namespace concurrent {
00032
00033 class ThreadFactory;
00034 class ExecutorService;
00035
00043 class DECAF_API Executors {
00044 private:
00045
00046 Executors();
00047 Executors(const Executors&);
00048 Executors& operator= (const Executors&);
00049
00050 private:
00051
00055 template<typename E>
00056 class RunnableAdapter : public decaf::util::concurrent::Callable<E> {
00057 private:
00058
00059 decaf::lang::Runnable* task;
00060 bool owns;
00061 E result;
00062
00063 private:
00064
00065 RunnableAdapter(const RunnableAdapter&);
00066 RunnableAdapter operator= (const RunnableAdapter&);
00067
00068 public:
00069
00070 RunnableAdapter(decaf::lang::Runnable* task, bool owns, const E& result) :
00071 decaf::util::concurrent::Callable<E>(), task(task), owns(owns), result(result) {
00072 }
00073
00074 virtual ~RunnableAdapter() {
00075 try{
00076 if (owns) {
00077 delete this->task;
00078 }
00079 }
00080 DECAF_CATCHALL_NOTHROW()
00081 }
00082
00083 virtual E call() {
00084 this->task->run();
00085 return result;
00086 }
00087 };
00088
00089 public:
00090
00091 virtual ~Executors();
00092
00103 static ThreadFactory* getDefaultThreadFactory();
00104
00121 static ExecutorService* newFixedThreadPool(int nThreads);
00122
00143 static ExecutorService* newFixedThreadPool(int nThreads, ThreadFactory* threadFactory);
00144
00155 static ExecutorService* newSingleThreadExecutor();
00156
00173 static ExecutorService* newSingleThreadExecutor(ThreadFactory* threadFactory);
00174
00189 static ExecutorService* unconfigurableExecutorService(ExecutorService* executor);
00190
00191 public:
00192
00206 template<typename E>
00207 static Callable<E>* callable(decaf::lang::Runnable* task, bool owns = true) {
00208
00209 if (task == NULL) {
00210 throw decaf::lang::exceptions::NullPointerException(__FILE__, __LINE__,
00211 "The Runnable task argument cannot be NULL");
00212 }
00213
00214 return new RunnableAdapter<E>(task, owns, E());
00215 }
00216
00232 template<typename E>
00233 static Callable<E>* callable(decaf::lang::Runnable* task, const E& result, bool owns = true) {
00234
00235 if (task == NULL) {
00236 throw decaf::lang::exceptions::NullPointerException(__FILE__, __LINE__,
00237 "The Runnable task argument cannot be NULL");
00238 }
00239
00240 return new RunnableAdapter<E>(task, owns, result);
00241 }
00242
00243 private:
00244
00245 static void initialize();
00246 static void shutdown();
00247
00248 friend class decaf::internal::util::concurrent::Threading;
00249
00250 };
00251
00252 }}}
00253
00254 #endif