activemq-cpp-3.8.2
Executors.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 
18 #ifndef _DECAF_UTIL_CONCURRENT_EXECUTORS_H_
19 #define _DECAF_UTIL_CONCURRENT_EXECUTORS_H_
20 
21 #include <decaf/util/Config.h>
22 
23 #include <decaf/lang/Thread.h>
24 #include <decaf/lang/Runnable.h>
26 
28 
29 namespace decaf {
30 namespace util {
31 namespace concurrent {
32 
33  class ThreadFactory;
34  class ExecutorService;
35 
44  private:
45 
46  Executors();
47  Executors(const Executors&);
48  Executors& operator= (const Executors&);
49 
50  private:
51 
55  template<typename E>
56  class RunnableAdapter : public decaf::util::concurrent::Callable<E> {
57  private:
58 
60  bool owns;
61  E result;
62 
63  private:
64 
65  RunnableAdapter(const RunnableAdapter&);
66  RunnableAdapter operator= (const RunnableAdapter&);
67 
68  public:
69 
70  RunnableAdapter(decaf::lang::Runnable* task, bool owns, const E& result) :
71  decaf::util::concurrent::Callable<E>(), task(task), owns(owns), result(result) {
72  }
73 
74  virtual ~RunnableAdapter() {
75  try{
76  if (owns) {
77  delete this->task;
78  }
79  }
81  }
82 
83  virtual E call() {
84  this->task->run();
85  return result;
86  }
87  };
88 
89  public:
90 
91  virtual ~Executors();
92 
103  static ThreadFactory* getDefaultThreadFactory();
104 
121  static ExecutorService* newFixedThreadPool(int nThreads);
122 
143  static ExecutorService* newFixedThreadPool(int nThreads, ThreadFactory* threadFactory);
144 
155  static ExecutorService* newSingleThreadExecutor();
156 
173  static ExecutorService* newSingleThreadExecutor(ThreadFactory* threadFactory);
174 
189  static ExecutorService* unconfigurableExecutorService(ExecutorService* executor);
190 
191  public:
192 
206  template<typename E>
207  static Callable<E>* callable(decaf::lang::Runnable* task, bool owns = true) {
208 
209  if (task == NULL) {
210  throw decaf::lang::exceptions::NullPointerException(__FILE__, __LINE__,
211  "The Runnable task argument cannot be NULL");
212  }
213 
214  return new RunnableAdapter<E>(task, owns, E());
215  }
216 
232  template<typename E>
233  static Callable<E>* callable(decaf::lang::Runnable* task, const E& result, bool owns = true) {
234 
235  if (task == NULL) {
236  throw decaf::lang::exceptions::NullPointerException(__FILE__, __LINE__,
237  "The Runnable task argument cannot be NULL");
238  }
239 
240  return new RunnableAdapter<E>(task, owns, result);
241  }
242 
243  private:
244 
245  static void initialize();
246  static void shutdown();
247 
249 
250  };
251 
252 }}}
253 
254 #endif /* _DECAF_UTIL_CONCURRENT_EXECUTORS_H_ */
A task that returns a result and may throw an exception.
Definition: Callable.h:47
#define NULL
Definition: Config.h:33
#define DECAF_CATCHALL_NOTHROW()
A catch-all that does not throw an exception, one use would be to catch any exception in a destructor...
Definition: ExceptionDefines.h:62
public interface ThreadFactory
Definition: ThreadFactory.h:52
An Executor that provides methods to manage termination and methods that can produce a Future for tra...
Definition: ExecutorService.h:56
static Callable< E > * callable(decaf::lang::Runnable *task, const E &result, bool owns=true)
Returns a Callable object that, when called, runs the given task and returns the default value of the...
Definition: Executors.h:233
static Callable< E > * callable(decaf::lang::Runnable *task, bool owns=true)
Returns a Callable object that, when called, runs the given task and returns the default value of the...
Definition: Executors.h:207
Definition: NullPointerException.h:32
#define DECAF_API
Definition: Config.h:29
Interface for a runnable object - defines a task that can be run by a thread.
Definition: Runnable.h:29
Implements a set of utilities for use with Executors, ExecutorService, ThreadFactory, and Callable types, as well as providing factory methods for instance of these types configured for the most common use cases.
Definition: Executors.h:43
virtual void run()=0
Run method - called by the Thread class in the context of the thread.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25