Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the situation that causes a deadlock in java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "what causes deadlocks in java". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the situation that causes deadlocks in java" can help you solve your doubts.

Four situations of deadlock caused by java

1. Cross-lock causes program deadlock: for example, thread A holds the lock of R1 waiting for the lock of R2, thread B holds the lock of R2 waiting for the lock of R1.

2. Insufficient memory: for example, two threads T1 and T2 have acquired 10MB memory, T2 has acquired 15MB memory, T1 and T2 need to acquire 30MB memory to work, but the remaining available memory is 10MB, so that both threads are waiting for each other to release memory resources.

3. Question-and-answer data exchange: the server opens a port and waits for the client to visit. After the client sends the request, the server misses the client request for some reason, causing the client to wait for the server to respond and the server to wait for the client to send the request.

4, deadlock caused by dead cycle: more common, using jstack and other tools can not see the deadlock, but the program does not work, CPU share is high, this deadlock is also called system fake death, difficult to troubleshoot and reproduce.

Deadlock related instance

Public class Main {private final Object MUTEX_READ = new Object (); private final Object MUTEX_WRITE = new Object () Public void read () {synchronized (MUTEX_READ) {synchronized (MUTEX_WRITE) {}} public void write () {synchronized (MUTEX_WRITE) {synchronized (MUTEX_READ) {}} public static void main (String [] args) throws InterruptedException {Main m = new Main () New Thread (()-> {while (true) {m.read ();}}) .start (); new Thread (()-> {while (true) {m.write ();}}) .start () }} after reading this, the article "what causes deadlocks in java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report