Java executorservice pauses / resumes a specific thread

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

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

Imagine that I want to stop threads because id = 0 (assuming that each thread is assigned an incremental ID until the size of the thread pool is reached)

After a while, press the button. I want to resume that particular thread and keep the current state of all other threads. These threads can be paused or resumed

I found an incomplete version of pausablethreadpoolexecutor in the Java documentation But it doesn't fit my needs because it restores all threads in the pool

If there is no way to use the default implementation of executorservice, anyone can point out the Java implementation of this problem?

thank you!

Solution

You're on the wrong track Thread pools own threads and may make things worse by sharing them with your code

Update: the correct way to cancel a task submitted in the thread pool is through the future of the task returned by the performer 1) In this way, you can be sure that the task you actually aim at is cancelled. 2) if your task has been designed to be cancelled, your task is half. 3) do not use a flag to indicate cancellation, but use thread currentThread()

to 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();      
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>