Commit 918260e7 by yuwencan

init commit

parents
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>shark.arthas</groupId>
<artifactId>arthas-in-action</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>arthas-in-action</name>
<description>arthas-in-action</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>shark.arthas.demo.ArthasInActionApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package shark.arthas.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ArthasInActionApplication {
public static void main(String[] args) {
SpringApplication.run(ArthasInActionApplication.class, args);
}
}
package shark.arthas.demo;
package shark.arthas.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author yuwc
* @ClassName 线程被锁定或死锁
* @description: 线程被锁定或死锁
* @date 2024年03月18日
* @version: 1.0
*/
@RestController
@RequestMapping("01")
public class 线程被锁定或死锁 {
private ReentrantLock lock1 = new ReentrantLock();
private ReentrantLock lock2 = new ReentrantLock();
@GetMapping("block")
public void block() throws InterruptedException {
Thread th1 = new Thread(this::doBlock, "thread1");
th1.start();
TimeUnit.MILLISECONDS.sleep(100);
Thread th2 = new Thread(this::doBlock, "thread2");
th2.start();
}
@GetMapping("deadLock")
public void deadBlock() throws InterruptedException {
Thread th1 = new Thread(this::doDeadLock, "thread1");
th1.start();
// TimeUnit.MILLISECONDS.sleep(50);
Thread th2 = new Thread(this::doDeadLock, "thread2");
th2.start();
}
private synchronized void doBlock() {
try {
TimeUnit.MINUTES.sleep(1);
System.out.println("method running...");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void doDeadLock() {
try {
String threadName = Thread.currentThread().getName();
if ("thread1".equalsIgnoreCase(threadName)) {
if (lock1.tryLock(1, TimeUnit.MINUTES)) {
TimeUnit.MILLISECONDS.sleep(100);
// lock2.lock();
if (lock2.tryLock(1, TimeUnit.MINUTES)) {
TimeUnit.SECONDS.sleep(30);
System.out.println("method running...");
}
}
}else {
if (lock2.tryLock(1, TimeUnit.MINUTES)) {
TimeUnit.MILLISECONDS.sleep(100);
// lock1.lock();
if (lock1.tryLock(1, TimeUnit.MINUTES)) {
TimeUnit.SECONDS.sleep(30);
System.out.println("method running...");
}
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (lock1.isLocked()) {
lock1.unlock();
}
if (lock2.isLocked()) {
lock2.unlock();
}
}
}
}
# 应用服务 WEB 访问端口
server.port=8080
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment