Improves host programming introducing a pool of worker threads
Change-Id: I979693aa220e2666c13c4015435c66173624ea64
diff --git a/utils/misc/src/main/java/org/onlab/util/PredictableExecutor.java b/utils/misc/src/main/java/org/onlab/util/PredictableExecutor.java
index 2be1ee2..279091c 100644
--- a/utils/misc/src/main/java/org/onlab/util/PredictableExecutor.java
+++ b/utils/misc/src/main/java/org/onlab/util/PredictableExecutor.java
@@ -15,6 +15,8 @@
*/
package org.onlab.util;
+import com.google.common.util.concurrent.MoreExecutors;
+
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
@@ -67,6 +69,18 @@
* @param threadFactory {@link ThreadFactory} to use to create threads
*/
public PredictableExecutor(int buckets, ThreadFactory threadFactory) {
+ this(buckets, threadFactory, false);
+ }
+
+ /**
+ * Creates {@link PredictableExecutor} instance.
+ * Meant for testing purposes.
+ *
+ * @param buckets number of buckets or 0 to match available processors
+ * @param threadFactory {@link ThreadFactory} to use to create threads
+ * @param directExec direct executors
+ */
+ public PredictableExecutor(int buckets, ThreadFactory threadFactory, boolean directExec) {
checkArgument(buckets >= 0, "number of buckets must be non zero");
checkNotNull(threadFactory);
if (buckets == 0) {
@@ -75,7 +89,7 @@
this.backends = new ArrayList<>(buckets);
for (int i = 0; i < buckets; ++i) {
- this.backends.add(backendExecutorService(threadFactory));
+ this.backends.add(backendExecutorService(threadFactory, directExec));
}
}
@@ -93,10 +107,11 @@
* Creates a single thread {@link ExecutorService} to use in the backend.
*
* @param threadFactory {@link ThreadFactory} to use to create threads
- * @return single thread {@link ExecutorService}
+ * @param direct direct executors
+ * @return single thread {@link ExecutorService} or direct executor
*/
- protected ExecutorService backendExecutorService(ThreadFactory threadFactory) {
- return Executors.newSingleThreadExecutor(threadFactory);
+ protected ExecutorService backendExecutorService(ThreadFactory threadFactory, boolean direct) {
+ return direct ? MoreExecutors.newDirectExecutorService() : Executors.newSingleThreadExecutor(threadFactory);
}