Home > Java > javaTutorial > body text

How to deal with deadlock in Java thread pool?

王林
Release: 2023-05-06 19:28:07
forward
1159 people have browsed it

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;
    }
});
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template