Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从正在运行的任务提交给执行者_Java_Multithreading_Deadlock - Fatal编程技术网

Java 从正在运行的任务提交给执行者

Java 从正在运行的任务提交给执行者,java,multithreading,deadlock,Java,Multithreading,Deadlock,由运行的任务(aRunnable)提交()任务是否安全?如果使用任何标准Java执行器,会导致死锁吗?如果我想防止死锁,对于标准执行器,我应该使用或避免任何特定的配置吗?我猜将任务提交给其他执行者是安全的,但是将任务提交给运行原始任务的执行者如何?如果出现死锁,它将由部署的可运行程序中的条件创建。ExecutorService本身只是一个可重用的线程池。它处理排队等待执行的可运行程序。无论可运行程序来自何处,ExecutorService本身都没有理由死锁 由执行者运行的任务(可运行)提交(执行

由运行的任务(a
Runnable
)提交()任务是否安全?如果使用任何标准Java执行器,会导致死锁吗?如果我想防止死锁,对于标准执行器,我应该使用或避免任何特定的配置吗?我猜将任务提交给其他执行者是安全的,但是将任务提交给运行原始任务的执行者如何?

如果出现死锁,它将由部署的可运行程序中的条件创建。ExecutorService本身只是一个可重用的线程池。它处理排队等待执行的可运行程序。无论可运行程序来自何处,ExecutorService本身都没有理由死锁

由执行者运行的任务(可运行)提交(执行())任务是否安全

如果它不会创建太多的任务,导致系统过载,那么它是安全的。e、 g.如果你有一个任务创建了两个任务,而他们创建了两个任务

如果使用任何标准Java执行器,会导致死锁吗

执行器是线程安全的,任务被添加到队列中

如果我想防止死锁,对于标准执行器,我应该使用或避免任何特定的配置吗

您可以使用一个线程和一个SynchronousQueue创建一个ThreadPoolExecutor,这可能会阻塞自身。但我不会那样做

我猜将任务提交给其他执行者是安全的,但是将任务提交给运行原始任务的执行者又如何呢

只要你具备以下任何一项,你就不会有问题。通常你都有这些

  • 不断增长的队伍
  • 如果无法添加,即不阻塞,则执行失败
  • 可扩展的线程池

如果您调用get on the future,那么您肯定会死锁,在下面的简单示例中,这可能是显而易见的,但是如果您有一些隐藏执行器用法的类层次结构,那么可能会有人错误地引入此类错误

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");
    }
}
另见
MyCallable: before get 1
MyCallable: before get 2
MyCallable: before get 2
MyCallable: before get 2