Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an ExecutorService that uses the current thread?

What I am after is a compatible way to configure the use of a thread pool or not. Ideally the rest of the code should not be impacted at all. I could use a thread pool with 1 thread but that isn't quite what I want. Any ideas?

ExecutorService es = threads == 0 ? new CurrentThreadExecutor() : Executors.newThreadPoolExecutor(threads);  // es.execute / es.submit / new ExecutorCompletionService(es) etc 
like image 955
Michael Rutherfurd Avatar asked Jul 05 '11 10:07

Michael Rutherfurd


People also ask

Can threads be submitted to ExecutorService?

Once the thread has delegated the task to the ExecutorService , the thread continues its own execution independent of the execution of that task. The ExecutorService then executes the task concurrently, independently of the thread that submitted the task.

How do you get active threads from thread pool executor?

Use a ThreadPoolExecutor implementation and call getActiveCount() on it: int getActiveCount() // Returns the approximate number of threads that are actively executing tasks.

What is difference between ExecutorService and ForkJoinPool?

The Fork/Join framework in Java 7 is an implementation of the Divide and Conquer algorithm, in which a central ForkJoinPool executes branching ForkJoinTasks. ExecutorService is an Executor that provides methods to manage the progress-tracking and termination of asynchronous tasks.

What is ExecutorService how it is used to create pool of threads?

The ExecutorService interface contains a large number of methods to control the progress of the tasks and manage the termination of the service. Using this interface, we can submit the tasks for execution and also control their execution using the returned Future instance.


1 Answers

You can use Guava's MoreExecutors.newDirectExecutorService(), or MoreExecutors.directExecutor() if you don't need an ExecutorService.

If including Guava is too heavy-weight, you can implement something almost as good:

public final class SameThreadExecutorService extends ThreadPoolExecutor {   private final CountDownLatch signal = new CountDownLatch(1);    private SameThreadExecutorService() {     super(1, 1, 0, TimeUnit.DAYS, new SynchronousQueue<Runnable>(),         new ThreadPoolExecutor.CallerRunsPolicy());   }    @Override public void shutdown() {     super.shutdown();     signal.countDown();   }    public static ExecutorService getInstance() {     return SingletonHolder.instance;   }    private static class SingletonHolder {     static ExecutorService instance = createInstance();       }    private static ExecutorService createInstance() {     final SameThreadExecutorService instance         = new SameThreadExecutorService();      // The executor has one worker thread. Give it a Runnable that waits     // until the executor service is shut down.     // All other submitted tasks will use the RejectedExecutionHandler     // which runs tasks using the  caller's thread.     instance.submit(new Runnable() {         @Override public void run() {           boolean interrupted = false;           try {             while (true) {               try {                 instance.signal.await();                 break;               } catch (InterruptedException e) {                 interrupted = true;               }             }           } finally {             if (interrupted) {               Thread.currentThread().interrupt();             }           }         }});     return Executors.unconfigurableScheduledExecutorService(instance);   } } 
like image 200
NamshubWriter Avatar answered Oct 23 '22 11:10

NamshubWriter