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 8367439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:09:31+00:00 2026-06-09T13:09:31+00:00

Is there a way to use ExecutorService to pause/resume a specific thread? private static

  • 0

Is there a way to use ExecutorService to pause/resume a specific thread?

private static ExecutorService threadpool = Executors.newFixedThreadPool(5);

Imagine that I want to stop the thread which as the id=0 (assuming that to each one is assigned an incremental id until the size of the threadpool is reached).

After a while, by pressing a button let’s say, I want to resume that specific thread and leave all the other threads with their current status, which can be paused or resumed.

I have found on Java documentation a uncompleted version of PausableThreadPoolExecutor. But it doesn’t suit what I need because it resume all the threads in the pool.

If there’s no way to do it with the default implementation of the ExecutorService can anyone point me to a Java implementation for this problem?

  • 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-09T13:09:32+00:00Added an answer on June 9, 2026 at 1:09 pm

    You are on the wrong track. The thread pool owns the threads and by sharing them with your code could mess things up.
    You should focus on making your tasks (passed to the threads cancellable/interruptable) and not interact with the threads owned by the pool directly.
    Additionally you would not know what job is being executed at the time you try to interrupt the thread, so I can’t see why you would be interested in doing this

    Update:
    The proper way to cancel your task submitted in the thread pool is via the Future for the task returned by the executor.
    1)This way you know for sure that the task you actually aim at is attempted to be cancelled
    2)If your tasks are already designed to be cancellable then your are half way there
    3) Do not use a flag to indicate cancellation but use Thread.currentThread().interrupt() instead

    Update:

    public class InterruptableTasks {  
    
        private static class InterruptableTask implements Runnable{  
            Object o = new Object();  
            private volatile boolean suspended = false;  
    
            public void suspend(){          
                suspended = true;  
            }  
    
            public void resume(){       
                suspended = false;  
                synchronized (o) {  
                    o.notifyAll();  
                }  
            }  
    
    
            @Override  
            public void run() {  
    
                while(!Thread.currentThread().isInterrupted()){  
                    if(!suspended){  
                        //Do work here      
                    }
                    else{  
                        //Has been suspended  
                        try {                   
                            while(suspended){  
                                synchronized(o){  
                                    o.wait();  
                                }                           
                            }                       
                        }  
                        catch (InterruptedException e) {                    
                        }             
                    }                           
                }  
                System.out.println("Cancelled");        
            }
    
        }
    
        /**  
         * @param args  
         * @throws InterruptedException   
         */  
        public static void main(String[] args) throws InterruptedException {  
            ExecutorService threadPool = Executors.newCachedThreadPool();  
            InterruptableTask task = new InterruptableTask();  
            Map<Integer, InterruptableTask> tasks = new HashMap<Integer, InterruptableTask>();  
            tasks.put(1, task);  
            //add the tasks and their ids
    
            Future<?> f = threadPool.submit(task);  
            TimeUnit.SECONDS.sleep(2);  
            InterruptableTask theTask = tasks.get(1);//get task by id
            theTask.suspend();  
            TimeUnit.SECONDS.sleep(2);  
            theTask.resume();  
            TimeUnit.SECONDS.sleep(4);                
            threadPool.shutdownNow();      
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to use lapply() in a manner that forces it to
Is there any way to use global int constants in Objective C that work
is there a way to use dependency injection in a widget, that is created
Is there a way to use form fields that does not correspond to database
Is there any way to use extension methods on a class that has been
Is there any way to use Google's API to retrieve a user's current zipcode
Is there a way to use the win32clipboard module to store a reference to
Is there a way to use a different css for Android than is used
Is there some way to use Tuning Advisor for SQL Express? Is there some
Is there a way to use numpy from a local directory without adding the

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.