Home  >  Article  >  Java  >  How to deal with deadlock in Java thread pool?

How to deal with deadlock in Java thread pool?

王林
王林forward
2023-05-06 19:28:071109browse

Explanation

1. Deadlock refers to the congestion phenomenon caused by two or more processes competing for resources or communicating with each other during the implementation process. Without external force, it cannot advance.

Thread pool deadlock instance

2. Solution: Expand the thread pool and the threads or task results no longer depend on each other.

final ExecutorService executorService =
        Executors.newSingleThreadExecutor();
Future<Long> f1 = executorService.submit(new Callable<Long>() {
 
    public Long call() throws Exception {
        System.out.println("start f1");
        Thread.sleep(1000);//延时
        Future<Long> f2 =
           executorService.submit(new Callable<Long>() {
 
            public Long call() throws Exception {
                System.out.println("start f2");
                return -1L;
            }
        });
        System.out.println("result" + f2.get());
        System.out.println("end f1");
        return -1L;
    }
});

The above is the detailed content of How to deal with deadlock in Java thread pool?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete