2021-04-26 Use jstack to view simulated deadlock

summarize

  • The book "Linux Shell Scripting Raiders" has learned the third chapter (83/268)
  • Start learning the book "The Art of JAVA Concurrent Programming"
  • jstackCheck the deadlock situation with
  • Have a general understanding of volatilethe processing logic in the CPU instruction set

Arrangement for tomorrow

  • Continue reading the book "The Art of JAVA Concurrent Programming"

note content

"The Art of JAVA Concurrent Programming"

  • How to reduce context switching?
    • Lock-free concurrent programming . When multiple threads compete for locks, context switching will be caused. Therefore, when multiple threads process data, some methods can be used to avoid using locks. For example, the ID of the data is segmented according to the Hash algorithm, and different threads process different segments of data.
    • CAS algorithm . Java's Atomic package uses the CAS algorithm to update data without locking.
    • Use minimal threads . Avoid creating unnecessary threads. For example, there are few tasks, but many threads are created for processing, which will cause a large number of counties to be in a waiting state.

combat

Use the command to check whether there are a large number of processes jstackin the process , so as to judge whether the size of the thread pool is appropriate. Reduce the number of context switches by reducing the number of idle threadsWAITING

simulated deadlock

The simulation code is as follows


public class 模拟死锁 {
    
    
    private final static String A = "A";
    private final static String B = "B";

    @Test
    public void test() throws InterruptedException {
    
    
        Thread threadA = new Thread(()->{
    
    
            synchronized(A){
    
    
                try {
    
    
                    Thread.sleep(100);
                    synchronized (B){
    
    
                        System.out.println("Get");
                    }
                } catch (InterruptedException e) {
    
     }
            }
        });
        Thread threadB = new Thread(()->{
    
    
            synchronized(B){
    
    
                try {
    
    
                    Thread.sleep(100);
                    synchronized (A){
    
    
                        System.out.println("Get");
                    }
                } catch (InterruptedException e) {
    
     }
            }
        });
        threadA.start();
        threadB.start();
        threadA.join();
        System.out.println("END");
    }
}

Use jstack, there is already a prompt message about the deadlock in the output result

virde@virde : ~ $ jps -l
76342 org.jetbrains.jps.cmdline.Launcher
76424 jdk.jcmd/sun.tools.jps.Jps
67466 com.intellij.idea.Main
76364 com.intellij.rt.junit.JUnitStarter
76126 ORG.JetBrains.idea.maven.server.RemoteMavenserver36
67934 org.jetbrains.idea.maven.server.remotemavenser36 Virde@Virde : ~ $ JSTACK 76364
2021-04- 26 15:43:08
Full Thread Dump Java Hotspot (TM) 64 -Bit Server VM (25.181-b13 mixed mode):
Java stack information for the threads listed above:
… …
=========================== ============================
"Thread-1":
	at multithreading. Simulated deadlock.lambda$test$1(Simulated deadlock.java :26)


	- waiting to lock <0x000000076d73f678> (a java.lang.String) 
	- locked <0x000000076d73f6a8> (a java.lang.String) 
	at multithreading. Simulate deadlock $$Lambda$2/380894366.run(Unknown Source) 
	at java. lang.Thread.run(Thread.java:748) 
"Thread-0": 
	at multithreading. Simulated deadlock.lambda$test$0(Simulated deadlock.java:16) 
	- waiting to lock <0x000000076d73f6a8> (a java. lang.String) 
	- locked <0x000000076d73f678> (a java.lang.String) 
	at multithreading. Simulate deadlock $$Lambda$1/1068824137.run(Unknown Source) 
	at java.lang.Thread.run(Thread.java:748 ) 

Found 1 deadlock. virde@virde : ~ $

Several common methods to avoid deadlock

  • Avoid one thread acquiring multiple locks at the same time
  • Avoid a thread occupying multiple resources in the lock at the same time, and try to ensure that each lock only occupies one resource.
  • Try using timed locks, use lock.tryLock(timeout)instead of using the internal locking mechanism
  • For database locks, locking and unlocking must be in one database connection, otherwise unlocking will fail

IDEA lombokerror resolution record

Used in the project lombok, no error was reported when mvn was built, but an error was reported when it was run.
The get and set methods cannot be found.

Baidu learned that if the maven version is inconsistent with the idea lombok version, an error may be reported. will update to the latest version in
the root directory . Still getting an error.pom.xmllombok

Check External Librariesthe files in the directory of the project and find that
there are two versions oflombok

Maven:org.projectlombok:lombok:1.18.10
Maven:org.projectlombok:lombok:1.18.20

I obviously introduced the latest version 1.18.20, so 1.18.10where did the version come from?
Check the file again pom.xml, it turns out that the sub-project pom.xmlintroduced a lower version.

The sub-project removes the reference, and the problem is solved

Reference: Introduction to idea lombok and solutions to red and error reports

Guess you like

Origin blog.csdn.net/kaka_buka/article/details/116170678