Skip to content

Commit ff780b0

Browse files
authored
feat: ch10 (#5)
1 parent 2c131a3 commit ff780b0

11 files changed

Lines changed: 458 additions & 1 deletion

File tree

build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ repositories {
1919

2020
dependencies {
2121
implementation("org.springframework.boot:spring-boot-starter")
22+
implementation("org.springframework.boot:spring-boot-starter-web")
23+
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
24+
runtimeOnly("com.h2database:h2:2.2.224")
25+
26+
implementation("org.springframework.boot:spring-boot-starter-actuator")
27+
2228
testImplementation("org.springframework.boot:spring-boot-starter-test")
2329
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
2430
}
Lines changed: 113 additions & 0 deletions
Loading
Lines changed: 94 additions & 0 deletions
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package example.concurrency;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package example.concurrency.ch10;
2+
3+
import org.springframework.stereotype.Service;
4+
import org.springframework.transaction.annotation.Propagation;
5+
import org.springframework.transaction.annotation.Transactional;
6+
7+
@Service
8+
public class InnerService {
9+
10+
@Transactional(propagation = Propagation.REQUIRES_NEW)
11+
public void innerMethod() {
12+
System.out.printf("InnerService.innerMethod is called by %s%n", Thread.currentThread().getName());
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package example.concurrency.ch10;
2+
3+
import java.sql.Connection;
4+
import javax.sql.DataSource;
5+
import org.springframework.jdbc.datasource.DataSourceUtils;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.transaction.annotation.Transactional;
8+
9+
@Service
10+
public class OuterService {
11+
12+
private final InnerService innerService;
13+
14+
public OuterService(InnerService innerService) {
15+
this.innerService = innerService;
16+
}
17+
18+
@Transactional
19+
public void outerMethod() {
20+
System.out.printf("OuterService.outerMethod is called by %s%n", Thread.currentThread().getName());
21+
try {
22+
Thread.sleep(1000);
23+
innerService.innerMethod();
24+
} catch (Exception e) {
25+
throw new RuntimeException(e.getMessage());
26+
}
27+
}
28+
}

src/main/resources/application.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
spring:
2+
datasource:
3+
url: jdbc:h2:mem:testdb
4+
driver-class-name: org.h2.Driver
5+
hikari:
6+
maximum-pool-size: 2
7+
# connection-timeout: 2000
8+
# idle-timeout: 300000
9+
# max-lifetime: 1200000
10+
11+
server:
12+
port: 8888
13+
14+
management:
15+
endpoints:
16+
web:
17+
exposure:
18+
include: "*"

src/test/java/com/example/concurrency/TempApplicationTests.java renamed to src/test/java/example/concurrency/TempApplicationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.concurrency;
1+
package example.concurrency;
22

33
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.test.context.SpringBootTest;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package example.concurrency.ch10;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.TimeUnit;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
11+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
12+
public class DeadLockTest {
13+
14+
@Autowired
15+
private OuterService outerService;
16+
17+
@Test
18+
public void testDeadLoc() throws InterruptedException {
19+
ExecutorService executorService = Executors.newFixedThreadPool(2);
20+
CountDownLatch startLatch = new CountDownLatch(1);
21+
CountDownLatch doneLatch = new CountDownLatch(2);
22+
23+
System.out.println("=== 데드락 테스트 시작 ===");
24+
25+
// Thread 1
26+
executorService.submit(() -> {
27+
try {
28+
startLatch.await(); // 동시 시작을 위한 대기
29+
System.out.println("Thread-1 시작");
30+
outerService.outerMethod();
31+
System.out.println("Thread-1 완료");
32+
} catch (Exception e) {
33+
System.err.println("Thread-1 에러: " + e.getMessage());
34+
e.printStackTrace();
35+
} finally {
36+
doneLatch.countDown();
37+
}
38+
});
39+
40+
// Thread 2
41+
executorService.submit(() -> {
42+
try {
43+
startLatch.await(); // 동시 시작을 위한 대기
44+
System.out.println("Thread-2 시작");
45+
outerService.outerMethod();
46+
System.out.println("Thread-2 완료");
47+
} catch (Exception e) {
48+
System.err.println("Thread-2 에러: " + e.getMessage());
49+
e.printStackTrace();
50+
} finally {
51+
doneLatch.countDown();
52+
}
53+
});
54+
55+
// 잠시 대기 후 동시 실행
56+
Thread.sleep(100);
57+
System.out.println("두 스레드 동시 시작!");
58+
startLatch.countDown();
59+
60+
// 최대 30초 대기 (데드락이면 타임아웃)
61+
boolean completed = doneLatch.await(30, TimeUnit.SECONDS);
62+
63+
if (!completed) {
64+
System.err.println("⚠️ 타임아웃 발생 - 데드락 가능성!");
65+
} else {
66+
System.out.println("✅ 모든 스레드 정상 완료");
67+
}
68+
69+
executorService.shutdownNow();
70+
}
71+
}

0 commit comments

Comments
 (0)