Submitting to an Executor from a running task

前端 未结 3 1241
执念已碎
执念已碎 2021-01-17 02:03

Is it safe for a task (a Runnable) being run by an Executor to submit (execute()) a task? Can it result in deadlock, if using any of the standard Java executors

相关标签:
3条回答
  • 2021-01-17 02:42

    If you call get on the future then you can surely deadlock, in simple example as below this might be obvious to spot but if you have some class hierarchy that hides executors usage then someone by mistake can introduce such bug.

    class TestApp {
        public static class MyCallable
                implements Callable {
            public Integer call() throws ExecutionException, InterruptedException {
                Future<Integer> future = pool.submit(new MyCallable());
                System.out.println("MyCallable: before get 2");
                future.get(); // deadlocks here
                System.out.println("MyCallable: after get 2");
                return 0;
            }
        }
    
        static ExecutorService pool = Executors.newSingleThreadExecutor();
        public static void main(String [] args) throws ExecutionException, InterruptedException {
            Future<Integer> future = pool.submit(new MyCallable());
            System.out.println("MyCallable: before get 1");
            future.get();
            System.out.println("MyCallable: after get 1");
        }
    }
    

    prints

    MyCallable: before get 1
    MyCallable: before get 2
    MyCallable: before get 2
    MyCallable: before get 2
    
    0 讨论(0)
  • 2021-01-17 02:46

    Is it safe for a task (a Runnable) being run by an Executor to submit (execute()) a task?

    Provided it doesn't create too many tasks as to overload the system, it is safe. e.g. if you have a task which creates two tasks and they create two tasks ...

    Can it result in deadlock, if using any of the standard Java executors?

    The Executors are thread safe and the task is added to a queue.

    Is there any particular configuration I should use, or avoid, if I want to prevent deadlock, for the standard executors?

    You can create a ThreadPoolExecutor with one thread and a SynchronousQueue and this could block itself. But I wouldn't do that. ;)

    I'm guessing that submitting a task to a different executor is safe, but what about submitting the task to the executor running the original task?

    Provided you have any of the following, you won't have problem. Often you have all of these

    • a growing queue
    • the execute fail if it cannot be added i.e. don't block
    • an expandable thread pool
    0 讨论(0)
  • 2021-01-17 02:58

    If there is a deadlock, it will be created by conditions in your deployed runnables. The ExecutorService itself is just a reusable thread pool. It handles queueing runnables for execution. There should be no reason the ExecutorService itself would be deadlocked regardless of where the Runnables originated from.

    0 讨论(0)
提交回复
热议问题