activemq-cpp-3.9.5
ThreadPoolExecutor.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#ifndef _DECAF_UTIL_CONCURRENT_THREADPOOLEXECUTOR_H_
18#define _DECAF_UTIL_CONCURRENT_THREADPOOLEXECUTOR_H_
19
20#include <decaf/lang/Runnable.h>
30#include <decaf/util/Config.h>
31
32#include <vector>
33
34namespace decaf{
35namespace util{
36namespace concurrent{
37
38 using decaf::lang::Pointer;
39
40 class ExecutorKernel;
41
58 class DECAF_API ThreadPoolExecutor : public AbstractExecutorService {
59 private:
60
61 ThreadPoolExecutor( const ThreadPoolExecutor& );
62 ThreadPoolExecutor& operator= ( const ThreadPoolExecutor& );
63
64 private:
65
66 friend class ExecutorKernel;
67 ExecutorKernel* kernel;
68
69 public:
70
97 ThreadPoolExecutor(int corePoolSize, int maxPoolSize,
98 long long keepAliveTime, const TimeUnit& unit,
100
131 ThreadPoolExecutor(int corePoolSize, int maxPoolSize,
132 long long keepAliveTime, const TimeUnit& unit,
134 RejectedExecutionHandler* handler);
135
166 ThreadPoolExecutor(int corePoolSize, int maxPoolSize,
167 long long keepAliveTime, const TimeUnit& unit,
169 ThreadFactory* threadFactory);
170
205 ThreadPoolExecutor(int corePoolSize, int maxPoolSize,
206 long long keepAliveTime, const TimeUnit& unit,
208 ThreadFactory* threadFactory,
209 RejectedExecutionHandler* handler);
210
212
213 virtual void execute(decaf::lang::Runnable* task);
214
215 virtual void execute(decaf::lang::Runnable* task, bool takeOwnership);
216
217 virtual void shutdown();
218
220
221 virtual bool awaitTermination(long long timeout, const decaf::util::concurrent::TimeUnit& unit);
222
223 virtual bool isShutdown() const;
224
225 virtual bool isTerminated() const;
226
232 virtual int getPoolSize() const;
233
239 virtual int getCorePoolSize() const;
240
254 virtual void setCorePoolSize(int poolSize);
255
261 virtual int getMaximumPoolSize() const;
262
274 virtual void setMaximumPoolSize(int maxSize);
275
283 virtual long long getTaskCount() const;
284
291 virtual int getActiveCount() const;
292
299 virtual long long getCompletedTaskCount() const;
300
307 virtual int getLargestPoolSize() const;
308
317
327 virtual bool isTerminating() const;
328
341 virtual void allowCoreThreadTimeout(bool value);
342
351 virtual bool allowsCoreThreadTimeout() const;
352
362 virtual long long getKeepAliveTime(const TimeUnit& unit) const;
363
377 virtual void setKeepAliveTime(long long timeout, const TimeUnit& unit);
378
390 virtual void setThreadFactory(ThreadFactory* factory);
391
399
406
418
427 virtual bool prestartCoreThread();
428
437
448
456 virtual void purge();
457
458 protected:
459
474
490
496 virtual void terminated();
497
498 protected:
499
503 virtual void onShutdown();
504
505 public: // RejectedExecutionHandler implementations.
506
514 public:
515
518
519 virtual ~AbortPolicy() {
520 }
521
522 virtual void rejectedExecution(decaf::lang::Runnable* task, ThreadPoolExecutor* executer DECAF_UNUSED) {
523 delete task;
524 throw RejectedExecutionException(__FILE__, __LINE__, "Unable to execute task.");
525 }
526
527 };
528
537 public:
538
541
543 }
544
545 virtual void rejectedExecution(decaf::lang::Runnable* task, ThreadPoolExecutor* executer DECAF_UNUSED) {
546
547 if (executer->isShutdown()) {
548 delete task;
549 return;
550 }
551
552 try {
553 task->run();
554 delete task;
555 } catch (decaf::lang::Exception& ex) {
556 delete task;
557 throw ex;
558 }
559 }
560 };
561
569 public:
570
573
574 virtual ~DiscardPolicy() {
575 }
576
577 virtual void rejectedExecution(decaf::lang::Runnable* task, ThreadPoolExecutor* executer DECAF_UNUSED) {
578 delete task;
579 }
580
581 };
582
591 public:
592
595
597 }
598
599 virtual void rejectedExecution(decaf::lang::Runnable* task, ThreadPoolExecutor* executer) {
600
601 if (executer->isShutdown()) {
602 delete task;
603 return;
604 }
605
606 try {
607 decaf::lang::Runnable* oldest = NULL;
608 executer->getQueue()->poll(oldest);
609 delete oldest;
610
611 executer->execute(task);
612 } catch (decaf::lang::Exception& ex) {
613 delete task;
614 throw ex;
615 }
616 }
617
618 };
619
620 };
621
622}}}
623
624#endif /*_DECAF_UTIL_CONCURRENT_THREADPOOLEXECUTOR_H_*/
Definition Exception.h:38
Interface for a runnable object - defines a task that can be run by a thread.
Definition Runnable.h:29
virtual void run()=0
Run method - called by the Thread class in the context of the thread.
A Thread is a concurrent unit of execution.
Definition Thread.h:64
This class represents an error that has occurred.
Definition Throwable.h:44
Definition ArrayList.h:39
A decaf::util::Queue that additionally supports operations that wait for the queue to become non-empt...
Definition BlockingQueue.h:164
Definition RejectedExecutionException.h:31
A handler for tasks that cannot be executed by a ThreadPoolExecutor.
Definition RejectedExecutionHandler.h:36
public interface ThreadFactory
Definition ThreadFactory.h:52
AbortPolicy()
Definition ThreadPoolExecutor.h:516
virtual ~AbortPolicy()
Definition ThreadPoolExecutor.h:519
virtual void rejectedExecution(decaf::lang::Runnable *task, ThreadPoolExecutor *executer DECAF_UNUSED)
Definition ThreadPoolExecutor.h:522
virtual void rejectedExecution(decaf::lang::Runnable *task, ThreadPoolExecutor *executer DECAF_UNUSED)
Definition ThreadPoolExecutor.h:545
CallerRunsPolicy()
Definition ThreadPoolExecutor.h:539
virtual ~CallerRunsPolicy()
Definition ThreadPoolExecutor.h:542
virtual ~DiscardOldestPolicy()
Definition ThreadPoolExecutor.h:596
virtual void rejectedExecution(decaf::lang::Runnable *task, ThreadPoolExecutor *executer)
Method that may be invoked by a ThreadPoolExecutor when execute cannot accept a task.
Definition ThreadPoolExecutor.h:599
virtual void rejectedExecution(decaf::lang::Runnable *task, ThreadPoolExecutor *executer DECAF_UNUSED)
Definition ThreadPoolExecutor.h:577
DiscardPolicy()
Definition ThreadPoolExecutor.h:571
virtual ~DiscardPolicy()
Definition ThreadPoolExecutor.h:574
virtual void execute(decaf::lang::Runnable *task)
This method is the same as calling the two param execute method and passing true as the second argume...
virtual int prestartAllCoreThreads()
This method will create and start new core threads running in an idle state waiting for new tasks up ...
virtual long long getTaskCount() const
Returns the current number of pending tasks in the work queue.
virtual bool allowsCoreThreadTimeout() const
Returns whether this executor has been configured to allow core threads to terminate if they sit idle...
virtual long long getKeepAliveTime(const TimeUnit &unit) const
Returns the currently set value for the maximum amount of time a worker Thread that is not part of th...
virtual void setMaximumPoolSize(int maxSize)
Sets the maximum number of workers this Executor is allowed to have at any given time above the core ...
ThreadPoolExecutor(int corePoolSize, int maxPoolSize, long long keepAliveTime, const TimeUnit &unit, BlockingQueue< decaf::lang::Runnable * > *workQueue)
Creates a new instance of a ThreadPoolExecutor.
virtual void onShutdown()
Used by some Decaf ThreadPoolExecutor extensions to correctly handle the shutdown case.
virtual ThreadFactory * getThreadFactory() const
Gets the currently configured ThreadFactory.
bool remove(decaf::lang::Runnable *task)
Attempts to remove the Runnable from the work queue, if successful then the caller now owns the Runna...
virtual void shutdown()
Performs an orderly shutdown of this Executor.
friend class ExecutorKernel
Definition ThreadPoolExecutor.h:66
virtual long long getCompletedTaskCount() const
Returns the approximate number of Tasks that have been completed by this Executor,...
virtual bool awaitTermination(long long timeout, const decaf::util::concurrent::TimeUnit &unit)
The caller will block until the executor has completed termination meaning all tasks that where sched...
virtual void allowCoreThreadTimeout(bool value)
When true this setting allows the threads in the core pool to terminate if they sit idle longer than ...
ThreadPoolExecutor(int corePoolSize, int maxPoolSize, long long keepAliveTime, const TimeUnit &unit, BlockingQueue< decaf::lang::Runnable * > *workQueue, ThreadFactory *threadFactory, RejectedExecutionHandler *handler)
Creates a new instance of a ThreadPoolExecutor.
virtual bool isTerminating() const
Returns true if the executor has begin the process of terminating but has not yet completed the proce...
virtual RejectedExecutionHandler * getRejectedExecutionHandler() const
Gets the currently configured RejectedExecutionHandler for this Executor.
virtual bool isShutdown() const
Returns whether this executor has been shutdown or not.
virtual void setThreadFactory(ThreadFactory *factory)
Sets the ThreadFactory instance used to create new Threads for this Executor.
virtual int getMaximumPoolSize() const
Returns the configured maximum number of threads for this Executor.
virtual void purge()
Attempts to remove any Future derived tasks from the pending work queue if they have been canceled.
virtual void setRejectedExecutionHandler(RejectedExecutionHandler *handler)
Sets the new RejectedExecutionHandler that this executor should use to process any rejected Runnable ...
ThreadPoolExecutor(int corePoolSize, int maxPoolSize, long long keepAliveTime, const TimeUnit &unit, BlockingQueue< decaf::lang::Runnable * > *workQueue, RejectedExecutionHandler *handler)
Creates a new instance of a ThreadPoolExecutor.
virtual void afterExecute(decaf::lang::Runnable *task, decaf::lang::Throwable *error)
Called upon completion of execution of a given task.
virtual void setKeepAliveTime(long long timeout, const TimeUnit &unit)
Configures the amount of time a non core Thread will remain alive after it has completed its assigned...
virtual bool isTerminated() const
Returns whether all tasks have completed after this executor was shut down.
virtual ArrayList< decaf::lang::Runnable * > shutdownNow()
Attempts to stop all currently executing tasks and returns an ArrayList containing the Runnables that...
virtual void setCorePoolSize(int poolSize)
Set the number of threads that this executor treats as its core threads, this value will override the...
virtual bool prestartCoreThread()
By default a Core thread is only created once the first task is queued, this method forces the creati...
virtual int getLargestPoolSize() const
Returns the most Threads that have ever been active at one time within this Executors Thread pool.
virtual int getCorePoolSize() const
Returns the configured number of core threads for this Executor.
virtual BlockingQueue< decaf::lang::Runnable * > * getQueue()
Provides access to the Task Queue used by this Executor.
virtual void terminated()
Method invoked when the Executor has terminated, by default this method does nothing.
virtual void beforeExecute(decaf::lang::Thread *thread, decaf::lang::Runnable *task)
Method called before a task is executed by the given thread.
virtual int getActiveCount() const
Returns an approximation of the number of threads that are currently running tasks for this executor.
ThreadPoolExecutor(int corePoolSize, int maxPoolSize, long long keepAliveTime, const TimeUnit &unit, BlockingQueue< decaf::lang::Runnable * > *workQueue, ThreadFactory *threadFactory)
Creates a new instance of a ThreadPoolExecutor.
virtual int getPoolSize() const
Returns the number of threads that currently exists for this Executor.
virtual void execute(decaf::lang::Runnable *task, bool takeOwnership)
Executes the given command at some time in the future.
A TimeUnit represents time durations at a given unit of granularity and provides utility methods to c...
Definition TimeUnit.h:62
#define NULL
Definition Config.h:33
#define DECAF_API
Definition Config.h:29
Definition AbstractExecutorService.h:28
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25