package org.drools.core.concurrent;

import java.util.concurrent.CompletionService;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import org.kie.api.concurrent.KieExecutors;

public class ExecutorProviderImpl implements KieExecutors {

    private static final java.util.concurrent.ExecutorService executor = Executors.newCachedThreadPool(new DaemonThreadFactory());

    public Executor getExecutor() {
        return executor;
    }

    public Executor newSingleThreadExecutor() {
        return Executors.newSingleThreadExecutor(new DaemonThreadFactory());
    }

    public <T> CompletionService<T> getCompletionService() {
        return new ExecutorCompletionService<T>(getExecutor());
    }

    private static class DaemonThreadFactory implements ThreadFactory {
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    }
}

@@ -30,16 +30,61 @@
 rrent;%0A%0A
+import org.kie.api.concurrent.KieExecutors;%0A%0A
 import j
@@ -254,16 +254,62 @@
 cutors;%0A
+import java.util.concurrent.SynchronousQueue;%0A
 import j
@@ -347,51 +347,142 @@
 ry;%0A
-%0A
 import 
-org.kie.api.concurrent.KieExecutors
+java.util.concurrent.ThreadPoolExecutor;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.atomic.AtomicInteger
 ;%0A%0Ap
@@ -541,16 +541,62 @@
 tors %7B%0A%0A
+    private static class ExecutorHolder %7B%0A    
     priv
@@ -661,41 +661,283 @@
 utor
- = Executors.newCachedThreadPool(
+;%0A%0A        static %7B%0A            executor = new ThreadPoolExecutor(0, Pool.SIZE,%0A                                              60L, TimeUnit.SECONDS,%0A                                              new SynchronousQueue%3CRunnable%3E(),%0A                                              
 new 
@@ -959,16 +959,32 @@
 tory());
+%0A        %7D%0A    %7D
 %0A%0A    pu
@@ -1028,16 +1028,31 @@
  return 
+ExecutorHolder.
 executor
@@ -1409,80 +1409,232 @@
    p
-ublic Thread newThread(Runnable r) %7B%0A            Thread t = new Thread(r
+rivate static final AtomicInteger threadCount = new AtomicInteger();%0A%0A        public Thread newThread(Runnable r) %7B%0A            Thread t = new Thread(r);%0A            t.setName(%22drools-worker-%22 + threadCount.incrementAndGet()
 );%0A 

allow to configure the size of the executor thread pool