activemq-cpp-3.9.5
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
29namespace decaf {
30namespace util {
31namespace concurrent {
32
33 class ThreadFactory;
34 class ExecutorService;
35
43 class DECAF_API Executors {
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
104
121 static ExecutorService* newFixedThreadPool(int nThreads);
122
143 static ExecutorService* newFixedThreadPool(int nThreads, ThreadFactory* threadFactory);
144
156
174
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_ */
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.
Definition NullPointerException.h:32
A task that returns a result and may throw an exception.
Definition Callable.h:47
An Executor that provides methods to manage termination and methods that can produce a Future for tra...
Definition ExecutorService.h:56
static ExecutorService * newSingleThreadExecutor()
Creates an Executor that uses a single worker thread operating off an unbounded queue owned by the ex...
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
static ExecutorService * newSingleThreadExecutor(ThreadFactory *threadFactory)
Creates an Executor that uses a single worker thread operating off an unbounded queue owned by the ex...
static ExecutorService * unconfigurableExecutorService(ExecutorService *executor)
Returns a new ExecutorService derived instance that wraps and takes ownership of the given ExecutorSe...
static ExecutorService * newFixedThreadPool(int nThreads)
Creates a new ThreadPoolExecutor with a fixed number of threads to process incoming tasks.
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 ThreadFactory * getDefaultThreadFactory()
Creates and returns a new ThreadFactory that expresses the default behavior for ThreadFactories used ...
static ExecutorService * newFixedThreadPool(int nThreads, ThreadFactory *threadFactory)
Creates a new ThreadPoolExecutor with a fixed number of threads to process incoming tasks.
public interface ThreadFactory
Definition ThreadFactory.h:52
#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
#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