프로그래밍
-
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 (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 언어로 ..
-
Java Virtual Thread (3), executorService 생성프로그래밍/Java 2024. 6. 22. 21:41
Executor Service플랫폼 스레드에서 executorService를 실행해보자. @Slf4jpublic class VirtualThreadExecutorCreation { private static final Runnable runnable = new Runnable() { @Override public void run() { log.info("1) run. thread: {}", Thread.currentThread()); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new Ru..
-
Java Virtual Thread (2), Thread와 Virtual Thread프로그래밍/Java 2024. 6. 22. 19:35
Thread@Slf4jpublic class Main { private static final Runnable runnable = new Runnable() { @Override public void run() { log.info("1) run. thread: {}", Thread.currentThread()); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } log.info("2) run. thread: ..
-
Java Virtual Thread (1)프로그래밍/Java 2024. 6. 16. 21:09
예를들어 c 혹은 c++ 로 쓰레드 3개를 만들고, cpu 는 두 개가 있다고 가정해보자. 이 경우, 운영체제는 3개의 쓰레드가 2개의 cpu를 잘 활용하기 위해서 스케줄링을 하고, 컨텍스트 스위칭을 통해 쓰레드가 소외되지 않고 cpu를 골고루 사용하게 해준다. 자바의 경우 자바 쓰레드를 하나 만들 때마다 os 레벨에서 쓰레드가 생긴다. 위와 같이 자바 쓰레드 3개를 생성하면 os 레벨에 쓰레드도 3개 생성된다. 그리고 os 레벨의 쓰레드를 os 쓰레드 혹은 커널 쓰레드라고 부른다. 커널 쓰레드는 생성 시 약 1~2MB 메모리 용량이 소모된다. 그리고 쓰레드가 많아지면 os 가 스케줄링하기 버거워지며 컨텍스트 스위칭도 많이 일어나기 때문에 쓰레드를 많이 만드는 것은 많은 고민이 필요하다. ...
-
코루틴(Coroutine) 기초프로그래밍/Kotlin 2024. 1. 29. 16:22
코루틴을 사용하기 전 의존성을 추가해야한다. dendendcies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.3") testImplementation(kotlin("test)) } runBlocking 코루틴을 생성하는 코루틴 빌더. runBlocking으로 감싼 코드는 코루틴 내부의 코드가 수행이 끝날 때까지 스레드가 블록킹된다. fun main() { runBlocking { println("hello") println(Thread.currentThread().name) } println("world") println(Thread.currentThread().name) } // Hello // main @coroutine#..
-
CompletableFuture (3) - Callable 과 Future프로그래밍/Java 2022. 6. 14. 15:18
Thread는 Runnable과 Callable의 구현된 함수를 수행한다는 공통점이 있지만, 다음과 같은 차이점이 있다. Runnable: 어떤 객체도 리턴하지 않습니다. Exception을 발생시키지 않습니다. Callable: 특정 타입의 객체를 리턴합니다. Exception을 발생킬 수 있습니다. Callable 이전까지 사용했던 Runnable과 유사하지만 작업의 결과(return) 를 받을 수 있다. Future Future 는 자바 1.5 에 등장한 비동기 계산 결과를 나타내는 인터페이스 비동기적인 작업의 현재 상태를 조회하거나 결과를 가져올 수 있다. Future를 이용하면 멀티쓰레드 환경에서 처리된 어떤 데이터를 다른 쓰레드에 전달할 수 있다. Future 내부적으로 Thread-Safe ..