How can I terminate Tasks that have timed out in multithreading?

后端 未结 1 1194
北海茫月
北海茫月 2020-12-21 22:01

I need to make a library in which I will have synchronous and asynchronous methods in it.

  • executeSynchronous() - waits until I have a result, retu
相关标签:
1条回答
  • 2020-12-21 22:54

    If task is still in the queue then cancelling it by simply calling future.cancel() is ok but obviously you don't know if that is in the queue. Also even if you ask future to interrupt the task it may not work as your task can still be doing something which is ignoring the thread interrupted status.

    So you can use the future.cancel(true) but you need to make sure that your task (thread) does regard the thread interrupted status. For example as you mentioned you make http call, so you might need to close the http client resource as soon as thread is interrupted.

    Please refer to the example below.

    I have tried to implement the task cancellation scenario. Normally a thread can check isInterrupted() and try to terminate itself. But this becomes more complex when you are using thread pool executors, callable and if the task is not really like while(!Thread.isInterrupted()) {// execute task}.

    In this example, a task is writing a file (I did not use http call to keep the it simple). A thread pool executor starts running the task but the caller wants to cancel it just after 100 milli seconds. Now future sends the interrupt signal to the thread but the callable task can not check it immediately while writing to file. So to make this happen callable maintains a list of IO resources it is going to use and as soon as future wants to cancel the task it just calls cancel() on all IO resources which terminates the task with IOException and then thread finishes.

    public class CancellableTaskTest {
    
        public static void main(String[] args) throws Exception {
            CancellableThreadPoolExecutor threadPoolExecutor = new CancellableThreadPoolExecutor(0, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
            long startTime = System.currentTimeMillis();
            Future<String> future = threadPoolExecutor.submit(new CancellableTask());
            while (System.currentTimeMillis() - startTime < 100) {
                Thread.sleep(10);
            }
            System.out.println("Trying to cancel task");
            future.cancel(true);
        }
    }
    
    class CancellableThreadPoolExecutor extends ThreadPoolExecutor {
    
        public CancellableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
            super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        }
    
        @Override
        protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
            return new CancellableFutureTask<T>(callable);
        }
    }
    
    class CancellableFutureTask<V> extends FutureTask<V> {
    
        private WeakReference<CancellableTask> weakReference;
    
        public CancellableFutureTask(Callable<V> callable) {
            super(callable);
            if (callable instanceof CancellableTask) {
                this.weakReference = new WeakReference<CancellableTask>((CancellableTask) callable);
            }
        }
    
        public boolean cancel(boolean mayInterruptIfRunning) {
            boolean result = super.cancel(mayInterruptIfRunning);
            if (weakReference != null) {
                CancellableTask task = weakReference.get();
                if (task != null) {
                    try {
                        task.cancel();
                    } catch (Exception e) {
                        e.printStackTrace();
                        result = false;
                    }
                }
            }
            return result;
        }
    }
    
    class CancellableTask implements Callable<String> {
    
        private volatile boolean cancelled;
        private final Object lock = new Object();
        private LinkedList<Object> cancellableResources = new LinkedList<Object>();
    
        @Override
        public String call() throws Exception {
            if (!cancelled) {
                System.out.println("Task started");
                // write file
                File file = File.createTempFile("testfile", ".txt");
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                synchronized (lock) {
                    cancellableResources.add(writer);
                }
                try {
                    long lineCount = 0;
                    while (lineCount++ < 100000000) {
                        writer.write("This is a test text at line: " + lineCount);
                        writer.newLine();
                    }
                    System.out.println("Task completed");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    writer.close();
                    file.delete();
                    synchronized (lock) {
                        cancellableResources.clear();
                    }
                }
            }
            return "done";
        }
    
        public void cancel() throws Exception {
            cancelled = true;
            Thread.sleep(1000);
            boolean success = false;
            synchronized (lock) {
                for (Object cancellableResource : cancellableResources) {
                    if (cancellableResource instanceof Closeable) {
                        ((Closeable) cancellableResource).close();
                        success = true;
                    }
                }
            }
            System.out.println("Task " + (success ? "cancelled" : "could not be cancelled. It might have completed or not started at all"));
        }
    }
    

    For your REST Http client related requirement you can modify the factory class something like this -

    public class CancellableSimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
    
        private List<Object> cancellableResources;
    
        public CancellableSimpleClientHttpRequestFactory() {
        }
    
        public CancellableSimpleClientHttpRequestFactory(List<Object> cancellableResources) {
            this.cancellableResources = cancellableResources;
        }
    
        protected HttpURLConnection openConnection(URL url, Proxy proxy) throws IOException {
            HttpURLConnection connection = super.openConnection(url, proxy);
            if (cancellableResources != null) {
                cancellableResources.add(connection);
            }
            return connection;
        }
    }
    

    Here you need to use this factory while creating RestTemplate in your runnable task.

        RestTemplate template = new RestTemplate(new CancellableSimpleClientHttpRequestFactory(this.cancellableResources));
    

    Make sure that you pass the same list of cancellable resources that you have maintained in CancellableTask.

    Now you need to modify the cancel() method in CancellableTask like this -

    synchronized (lock) {
        for (Object cancellableResource : cancellableResources) {
            if (cancellableResource instanceof HttpURLConnection) {
                ((HttpURLConnection) cancellableResource).disconnect();
                success = true;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题