분류 전체보기
-
Java Virtual Thread (12), custom Executor, Scheduler프로그래밍/Java 2024. 6. 29. 12:15
하나의 Spring Boot Application 인데, 이 안에 주문과 배송 등이 구현되어 있다고 가정해보자. 이 때 주문은 Virtual Thread를 사용하고, 다른 비즈니스에서는 CPU를 많이 사용하기 때문에 Platform Thread 를 사용하면서 사용할 수 있다. 만약, yml에 spring-virtual-enable-true로 설정한다면 virtual thread 로 바뀌기 때문에 Platform thread를 사용할 수 없다. @Configuration@Slf4jpublic class SchedulerConfig { @Bean public ThreadPoolTaskScheduler threadPoolTaskScheduler() { ThreadPoolTaskS..
-
Java Virtual Thread (11), @scheduled 어노테이션프로그래밍/Java 2024. 6. 29. 11:23
스케줄러에서 virtual thread가 잘 종료되는지 확인해보자. import lombok.extern.slf4j.Slf4j;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Service@Slf4jpublic class VirtualScheduler { @Scheduled(fixedRate = 5000) public void fixedRate() { log.info("fixedRate. thread: {}", Thread.currentThread()); }}import org.springframework.boot.SpringAppli..
-
Java Virtual Thread (10), @async 어노테이션프로그래밍/Java 2024. 6. 29. 10:51
Controller.java@GetMapping("/async")public void async() { log.info("1) async. thread: {}", Thread.currentThread()); virtualService.async(); log.info("2) async. thread: {}", Thread.currentThread());} Service.javaimport lombok.extern.slf4j.Slf4j;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Service@Slf4jpublic class VirtualService ..
-
Java Virtual Thread (9), Spring Boot에서 Virtual Thread 사용하기카테고리 없음 2024. 6. 23. 16:25
spring: output: ansi: enabled: ALWAYS threads: virtual: enabled: true task: scheduling: thread-name-prefix: mySch- yml 파일에 spring.thread.virtual.enavled = true 로 설정해주면 스프링이 기존 thread pool 의 동작을 모두 virtual thread pool 로 대체해준다. RestController 에서 사용하는 Tomcat의 Thread가 어떻게 Virtual Thread로 동작하는지 살펴보자.@GetMapping("/sleep")public void sleep() throws InterruptedException { ..
-
Java Virtual Thread (8), Virtual Thread와 Platform Thread 성능비교프로그래밍/Java 2024. 6. 23. 15:33
1) IO 바운드 작업 비교 private static final Runnable ioBoundRunnable = new Runnable() { @Override public void run() { log.info("1) run. thread: " + Thread.currentThread()); try { Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } log.info("2) run. thread: " + Thread.currentThread()); }};private static..
-
Java Virtual Thread (7), Virtual Thread Dump프로그래밍/Java 2024. 6. 23. 15:04
기본적으로 인텔리제이에서는 Thread Dump와 비슷한 기능을 제공하고 있다. 현재 인텔리제이에서는 디버그 모드를 통해 Virtual Thread가 몇개 떠 있는지 정확하게 알 수 없다. (2024.01 버전 기준) @Slf4j// jcmd Thread.dump_to_file -format=text public class VirtualThreadDump { private static final Runnable runnable = new Runnable() { @Override public void run() { log.info("1) run. thread: " + Thread.currentThread()); try { ..
-
Java Virtual Thread (6), Reentrant을 이용한 Pinned Virtual Thread 방지카테고리 없음 2024. 6. 23. 14:48
private final ReentrantLock lock = new ReentrantLock(); private final Runnable runnable = new Runnable() { @Override public void run() { //synchronized (this) { lock.lock(); try { Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { loc..
-
Java Virtual Thread (5), Pinned Virtual Thread프로그래밍/Java 2024. 6. 23. 14:17
Pinned Virtual Thread를 직역하면 고정된 가상 스레드이다. Virtual Thread는 blocking 작업이 되면(blocking 메서드를 호출하면) 플랫폼 스레드가 언마운트되고 다른 Virtual Thread가 플랫폼 스레드를 사용함으로써 효율은 높이는 방식인데 그것을 못하게 하는 것이 Pinned Virtual Thread 이다. 즉, virtual thread 는 Synchronized block 혹은 Native 메서드와 함께 사용하는 것은 피해야한다. 1) Native 메서드 Object 클래스를 보면 hascode 라는 메서드가 있다. 이는 native로 설정이 되어 있으며 body 값이 없다. native 로 적혀져 있는 것은 c, c++ 같은 native 언어로 ..