activemq-cpp-3.8.2
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>
21 #include <decaf/lang/Throwable.h>
28 #include <decaf/util/LinkedList.h>
29 #include <decaf/util/ArrayList.h>
30 #include <decaf/util/Config.h>
31 
32 #include <vector>
33 
34 namespace decaf{
35 namespace util{
36 namespace concurrent{
37 
39 
40  class ExecutorKernel;
41 
59  private:
60 
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 
211  virtual ~ThreadPoolExecutor();
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 
219  virtual ArrayList<decaf::lang::Runnable*> shutdownNow();
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 
316  virtual BlockingQueue<decaf::lang::Runnable*>* getQueue();
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 
398  virtual ThreadFactory* getThreadFactory() const;
399 
405  virtual RejectedExecutionHandler* getRejectedExecutionHandler() const;
406 
417  virtual void setRejectedExecutionHandler(RejectedExecutionHandler* handler);
418 
427  virtual bool prestartCoreThread();
428 
436  virtual int prestartAllCoreThreads();
437 
447  bool remove(decaf::lang::Runnable* task);
448 
456  virtual void purge();
457 
458  protected:
459 
473  virtual void beforeExecute(decaf::lang::Thread* thread, decaf::lang::Runnable* task);
474 
489  virtual void afterExecute(decaf::lang::Runnable* task, decaf::lang::Throwable* error);
490 
496  virtual void terminated();
497 
498  protected:
499 
503  virtual void onShutdown();
504 
505  public: // RejectedExecutionHandler implementations.
506 
514  public:
515 
517  }
518 
519  virtual ~AbortPolicy() {
520  }
521 
523  delete task;
524  throw RejectedExecutionException(__FILE__, __LINE__, "Unable to execute task.");
525  }
526 
527  };
528 
537  public:
538 
540  }
541 
542  virtual ~CallerRunsPolicy() {
543  }
544 
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 
572  }
573 
574  virtual ~DiscardPolicy() {
575  }
576 
578  delete task;
579  }
580 
581  };
582 
591  public:
592 
594  }
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 
608  decaf::lang::Runnable* oldest = NULL;
609  executer->getQueue()->poll(oldest);
610  delete oldest;
611 
612  executer->execute(task);
613  } catch(decaf::lang::Exception& ex) {
614  delete task;
615  throw ex;
616  }
617  }
618 
619  };
620 
621  };
622 
623 }}}
624 
625 #endif /*_DECAF_UTIL_CONCURRENT_THREADPOOLEXECUTOR_H_*/
Provides a default implementation for the methods of the ExecutorService interface.
Definition: AbstractExecutorService.h:37
AbortPolicy()
Definition: ThreadPoolExecutor.h:516
This class represents an error that has occurred.
Definition: Throwable.h:44
A decaf::util::Queue that additionally supports operations that wait for the queue to become non-empt...
Definition: BlockingQueue.h:164
Handler policy for tasks that are rejected upon a call to ThreadPoolExecutor::execute this class alwa...
Definition: ThreadPoolExecutor.h:568
Definition: RejectedExecutionException.h:31
#define NULL
Definition: Config.h:33
Handler policy for tasks that are rejected upon a call to ThreadPoolExecutor::execute this class alwa...
Definition: ThreadPoolExecutor.h:590
public interface ThreadFactory
Definition: ThreadFactory.h:52
virtual ~DiscardPolicy()
Definition: ThreadPoolExecutor.h:574
CallerRunsPolicy()
Definition: ThreadPoolExecutor.h:539
virtual ~AbortPolicy()
Definition: ThreadPoolExecutor.h:519
virtual void rejectedExecution(decaf::lang::Runnable *task, ThreadPoolExecutor *executer DECAF_UNUSED)
Definition: ThreadPoolExecutor.h:545
A TimeUnit represents time durations at a given unit of granularity and provides utility methods to c...
Definition: TimeUnit.h:62
virtual BlockingQueue< decaf::lang::Runnable * > * getQueue()
Provides access to the Task Queue used by this Executor.
virtual ~CallerRunsPolicy()
Definition: ThreadPoolExecutor.h:542
Defines a Thread Pool object that implements the functionality of pooling threads to perform user tas...
Definition: ThreadPoolExecutor.h:58
A handler for tasks that cannot be executed by a ThreadPoolExecutor.
Definition: RejectedExecutionHandler.h:36
A Thread is a concurrent unit of execution.
Definition: Thread.h:64
#define DECAF_API
Definition: Config.h:29
Definition: ArrayList.h:39
#define DECAF_UNUSED
Definition: Config.h:160
Interface for a runnable object - defines a task that can be run by a thread.
Definition: Runnable.h:29
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
Definition: Exception.h:38
DiscardPolicy()
Definition: ThreadPoolExecutor.h:571
Handler policy for tasks that are rejected upon a call to ThreadPoolExecutor::execute this class will...
Definition: ThreadPoolExecutor.h:536
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 void rejectedExecution(decaf::lang::Runnable *task, ThreadPoolExecutor *executer DECAF_UNUSED)
Definition: ThreadPoolExecutor.h:522
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 ~DiscardOldestPolicy()
Definition: ThreadPoolExecutor.h:596
Handler policy for tasks that are rejected upon a call to ThreadPoolExecutor::execute this class alwa...
Definition: ThreadPoolExecutor.h:513
virtual void run()=0
Run method - called by the Thread class in the context of the thread.
DiscardOldestPolicy()
Definition: ThreadPoolExecutor.h:593
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
virtual bool isShutdown() const
Returns whether this executor has been shutdown or not.