|
activemq-cpp-3.4.0
|
00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one or more 00003 * contributor license agreements. See the NOTICE file distributed with 00004 * this work for additional information regarding copyright ownership. 00005 * The ASF licenses this file to You under the Apache License, Version 2.0 00006 * (the "License"); you may not use this file except in compliance with 00007 * the License. You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #ifndef _DECAF_UTIL_CONCURRENT_THREADPOOLEXECUTOR_H_ 00018 #define _DECAF_UTIL_CONCURRENT_THREADPOOLEXECUTOR_H_ 00019 00020 #include <decaf/lang/Runnable.h> 00021 #include <decaf/lang/Throwable.h> 00022 #include <decaf/util/concurrent/ThreadFactory.h> 00023 #include <decaf/util/concurrent/BlockingQueue.h> 00024 #include <decaf/util/concurrent/TimeUnit.h> 00025 #include <decaf/util/concurrent/AbstractExecutorService.h> 00026 #include <decaf/util/concurrent/RejectedExecutionHandler.h> 00027 #include <decaf/util/concurrent/RejectedExecutionException.h> 00028 #include <decaf/util/LinkedList.h> 00029 #include <decaf/util/ArrayList.h> 00030 #include <decaf/util/Config.h> 00031 00032 #include <vector> 00033 00034 namespace decaf{ 00035 namespace util{ 00036 namespace concurrent{ 00037 00038 using decaf::lang::Pointer; 00039 00040 class ExecutorKernel; 00041 00058 class DECAF_API ThreadPoolExecutor : public AbstractExecutorService { 00059 private: 00060 00061 ThreadPoolExecutor( const ThreadPoolExecutor& ); 00062 ThreadPoolExecutor& operator= ( const ThreadPoolExecutor& ); 00063 00064 private: 00065 00066 friend class ExecutorKernel; 00067 ExecutorKernel* kernel; 00068 00069 public: 00070 00097 ThreadPoolExecutor(int corePoolSize, int maxPoolSize, 00098 long long keepAliveTime, const TimeUnit& unit, 00099 BlockingQueue<decaf::lang::Runnable*>* workQueue); 00100 00131 ThreadPoolExecutor(int corePoolSize, int maxPoolSize, 00132 long long keepAliveTime, const TimeUnit& unit, 00133 BlockingQueue<decaf::lang::Runnable*>* workQueue, 00134 RejectedExecutionHandler* handler); 00135 00166 ThreadPoolExecutor(int corePoolSize, int maxPoolSize, 00167 long long keepAliveTime, const TimeUnit& unit, 00168 BlockingQueue<decaf::lang::Runnable*>* workQueue, 00169 ThreadFactory* threadFactory); 00170 00205 ThreadPoolExecutor(int corePoolSize, int maxPoolSize, 00206 long long keepAliveTime, const TimeUnit& unit, 00207 BlockingQueue<decaf::lang::Runnable*>* workQueue, 00208 ThreadFactory* threadFactory, 00209 RejectedExecutionHandler* handler); 00210 00211 virtual ~ThreadPoolExecutor(); 00212 00213 virtual void execute(decaf::lang::Runnable* task); 00214 00215 virtual void shutdown(); 00216 00217 virtual ArrayList<decaf::lang::Runnable*> shutdownNow(); 00218 00219 virtual bool awaitTermination(long long timeout, const decaf::util::concurrent::TimeUnit& unit); 00220 00221 virtual bool isShutdown() const; 00222 00223 virtual bool isTerminated() const; 00224 00230 virtual int getPoolSize() const; 00231 00237 virtual int getCorePoolSize() const; 00238 00252 virtual void setCorePoolSize(int poolSize); 00253 00259 virtual int getMaximumPoolSize() const; 00260 00272 virtual void setMaximumPoolSize(int maxSize); 00273 00281 virtual long long getTaskCount() const; 00282 00289 virtual int getActiveCount() const; 00290 00297 virtual long long getCompletedTaskCount() const; 00298 00305 virtual int getLargestPoolSize() const; 00306 00314 virtual BlockingQueue<decaf::lang::Runnable*>* getQueue(); 00315 00325 virtual bool isTerminating() const; 00326 00339 virtual void allowCoreThreadTimeout(bool value); 00340 00349 virtual bool allowsCoreThreadTimeout() const; 00350 00360 virtual long long getKeepAliveTime(const TimeUnit& unit) const; 00361 00375 virtual void setKeepAliveTime(long long timeout, const TimeUnit& unit); 00376 00388 virtual void setThreadFactory(ThreadFactory* factory); 00389 00396 virtual ThreadFactory* getThreadFactory() const; 00397 00403 virtual RejectedExecutionHandler* getRejectedExecutionHandler() const; 00404 00415 virtual void setRejectedExecutionHandler(RejectedExecutionHandler* handler); 00416 00425 virtual bool prestartCoreThread(); 00426 00434 virtual int prestartAllCoreThreads(); 00435 00445 bool remove(decaf::lang::Runnable* task); 00446 00454 virtual void purge(); 00455 00456 protected: 00457 00471 virtual void beforeExecute(decaf::lang::Thread* thread, decaf::lang::Runnable* task); 00472 00487 virtual void afterExecute(decaf::lang::Runnable* task, decaf::lang::Throwable* error); 00488 00494 virtual void terminated(); 00495 00496 public: // RejectedExecutionHandler implementations. 00497 00504 class AbortPolicy : public RejectedExecutionHandler { 00505 public: 00506 00507 AbortPolicy() : RejectedExecutionHandler() { 00508 } 00509 00510 virtual ~AbortPolicy() { 00511 } 00512 00513 virtual void rejectedExecution(decaf::lang::Runnable* task, ThreadPoolExecutor* executer DECAF_UNUSED) { 00514 delete task; 00515 throw RejectedExecutionException(__FILE__, __LINE__, "Unable to execute task."); 00516 } 00517 00518 }; 00519 00527 class CallerRunsPolicy : public RejectedExecutionHandler { 00528 public: 00529 00530 CallerRunsPolicy() : RejectedExecutionHandler() { 00531 } 00532 00533 virtual ~CallerRunsPolicy() { 00534 } 00535 00536 virtual void rejectedExecution(decaf::lang::Runnable* task, ThreadPoolExecutor* executer DECAF_UNUSED) { 00537 00538 if (executer->isShutdown()) { 00539 delete task; 00540 return; 00541 } 00542 00543 try{ 00544 task->run(); 00545 } catch(decaf::lang::Exception& ex) { 00546 delete task; 00547 throw ex; 00548 } 00549 } 00550 00557 class DiscardPolicy : public RejectedExecutionHandler { 00558 public: 00559 00560 DiscardPolicy() : RejectedExecutionHandler() { 00561 } 00562 00563 virtual ~DiscardPolicy() { 00564 } 00565 00566 virtual void rejectedExecution(decaf::lang::Runnable* task, ThreadPoolExecutor* executer DECAF_UNUSED) { 00567 delete task; 00568 } 00569 00570 }; 00571 00579 class DiscardOldestPolicy : public RejectedExecutionHandler { 00580 public: 00581 00582 DiscardOldestPolicy() : RejectedExecutionHandler() { 00583 } 00584 00585 virtual ~DiscardOldestPolicy() { 00586 } 00587 00588 virtual void rejectedExecution( decaf::lang::Runnable* task, ThreadPoolExecutor* executer ) { 00589 00590 if (executer->isShutdown()) { 00591 delete task; 00592 return; 00593 } 00594 00595 try{ 00596 00597 decaf::lang::Runnable* oldest = NULL; 00598 executer->getQueue()->poll(oldest); 00599 delete oldest; 00600 00601 executer->execute(task); 00602 } catch(decaf::lang::Exception& ex) { 00603 delete task; 00604 throw ex; 00605 } 00606 } 00607 00608 }; 00609 00610 }; 00611 }; 00612 00613 }}} 00614 00615 #endif /*_DECAF_UTIL_CONCURRENT_THREADPOOLEXECUTOR_H_*/