프로그래밍/Java
-
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 가 스케줄링하기 버거워지며 컨텍스트 스위칭도 많이 일어나기 때문에 쓰레드를 많이 만드는 것은 많은 고민이 필요하다. ...
-
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 ..
-
인터페이스 default 메소드와 static 메소드프로그래밍/Java 2022. 6. 9. 14:53
기본 메소드 (Default Methods) 인터페이스에 메소드 선언이 아니라 구현체를 제공하는 방법 해당 인터페이스를 구현한 클래스를 깨트리지 않고 새 기능을 추가할 수 있다. 기본 메소드는 구현체가 모르게 추가된 기능으로 그만큼 리스크가 있다. 컴파일 에러는 아니지만 구현체에 따라 런타임 에러가 발생할 수 있다. 반드시 문서화 할 것. (@implSpec 자바독 태그 사용) public interface Foo { void printName(); /* * @implSpec * 이 구현체는 getName()으로 가져온 문자열을 대문자로 바꿔 출력한다. */ // getName() 에 null 값이 들어올 수 있음. 문제가 된다면 구현 클래스에서 재정의하자. default void printNameUpp..