Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:14:29+00:00 2026-06-13T06:14:29+00:00

Consider a user interface that accepts configuration settings for a service that takes a

  • 0

Consider a user interface that accepts configuration settings for a service that takes a long time to initialize (for example, the parameters for a JDBC connection). We want our user interface to remain responsive while the service initialization occurs. If the user makes additional changes, the initialization should be cancelled and restarted using the new parameters.

Because parameters are incorporated in the configuration as the user types each character, it’s possible that a number of initialization requests could be created in a row. Only the last one should be executed.

We have code that we’ve put together that achieves this result, but it seems like this behavior is a very good candidate for implementation as an ExecutorService. Before we refactor everything into an ExecutorService, I figured I would ask if there are similar implementations already out in the world.

To be more specific:

The ExecutorService would have one worker thread. As soon as a new task is submitted, the current task is cancelled (and the worker interrupted). The new task is then captured for the next execution. If another task is submitted, the current task is cancelled again, and the “next execution” task is set to this new task. When the worker thread finally picks up the next task for execution, it will always be the last task submitted – all other tasks are either cancelled or discarded.

Does anyone have an implementation like that they’d be willing to share? Or is there maybe a standard library that covers this type of behavior? It’s not hard to implement, but getting the thread safety nailed down can be tricky, so I’d rather use proven code if we can.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:14:30+00:00Added an answer on June 13, 2026 at 6:14 am

    Here’s what I eventually came up with – I’m interested in any comments:

    public class InterruptingExecutorService extends ThreadPoolExecutor{
        private volatile FutureTask<?> currentFuture;
    
        public InterruptingExecutorService(boolean daemon) {
            super(0, 1, 1000L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(), 
                    daemon ? new DaemonThreadFactory() : Executors.defaultThreadFactory());
    
        }
    
        public static class DaemonThreadFactory implements ThreadFactory{
            ThreadFactory delegate = Executors.defaultThreadFactory();
    
            @Override
            public Thread newThread(Runnable r) {
                Thread t = delegate.newThread(r);
                t.setDaemon(true);
                return t;
            }
    
        }
    
        private void cancelCurrentFuture(){
            // cancel all pending tasks
            Iterator<Runnable> it = getQueue().iterator();
            while(it.hasNext()){
                FutureTask<?> task = (FutureTask<?>)it.next();
                task.cancel(true);
                it.remove();
            }
    
            // cancel the current task
            FutureTask<?> currentFuture = this.currentFuture;
            if(currentFuture != null){
                currentFuture.cancel(true);
            }
        }
    
        @Override
        public void execute(Runnable command) {
            if (command == null) throw new NullPointerException();
    
            cancelCurrentFuture();
            if (!(command instanceof FutureTask)){ // we have to be able to cancel a task, so we have to wrap any non Future
                command = newTaskFor(command, null);
            }
            super.execute(command);
        }
    
        @Override
        protected void beforeExecute(Thread t, Runnable r) {
            // it is safe to access currentFuture like this b/c we have limited the # of worker threads to only 1
            // it isn't possible for currentFuture to be set by any other thread than the one calling this method
            this.currentFuture = (FutureTask<?>)r;
        }
    
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            // it is safe to access currentFuture like this b/c we have limited the # of worker threads to only 1
            // it isn't possible for currentFuture to be set by any other thread than the one calling this method
            this.currentFuture = null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider one has some user defined types, and containers of those types that are
Consider the following example $q = Doctrine::getTable('User')->createQuery('u') ->where('u.username = ?', 'test'); If another related
Consider the following example @Remote public interface RegistrationService { public String register(); public void
I have a client-side interface that allows the user to perform multiple edits against
Consider a user making multiple requests at the same time, do I have to
I have an interface, for example ISomeService . ISomeService provides a common service, but
Consider this: class User < ActiveRecord::Base # name, email, password end class Article <
Consider a facebook User — MB , one of the admins of a facebook
Consider a model user: User(id: integer, name: string, email: string, status: string) When I
I am using Hibernate 4.1 and Spring 3.1. Consider the following User class having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.