전체 글
-
thread - join()프로그래밍/Java 2024. 7. 28. 14:42
1. 스레드의 상태 및 전이 과정Join() 을 알아보기 전에 스레드의 상태에 대한 이해가 필요하다. 스레드의 상태1) new (새로운 상태) : 스레드가 생성되었으나 아직 시작되지 않은 상태.2) Runnable (실행 가능 상태) : 스레드가 실행 중이거나 실행될 준비가 된 상태3) 일시 중지 상태들 (Suspended State) - Block (차단 상태) : 스레드가 동기화 락을 기다리는 상태- Waiting (대기 상태) : 스레드가 무기한으로 다른 스레드의 작업을 기다리는 상태- Time Waiting (시간 제한 대기 상태) : 스레드가 일정 시간 동안 다른 스레드의 작업을 기다리는 상태4) Terminated (종료 상태) : 스레드의 실행이 완료된 상태 자바 스레드 상태 전이 과정1..
-
Thread Pool (Java, Tomcat, SpringBoot)Web 2024. 7. 25. 21:38
Java, Tomcat, SpringBoot에서의 Thread Pool을 Web server application 관점에서 살펴보자. .... 먼저 Program / Process / Thread를 살펴보자. Program- 어떤 목적을 위해 컴퓨터의 동작을 하나로 모아 둔 것 (코드 덩어리) Process - 현재 실행중인 프로그램 Thread- Cpu core의 실행 단위- Process의 작업을 Thread 단위로 나눌 수 있음- 이를 Cpu core가 처리- 하나의 Process 에서 두가지 이상 작업을 동시에 실행 가능 ... 단순히 Thread만 사용해서 동시에 여러 작업을 실행시킬 수 있는 프로그램을 만들 수 있을까? Thread를 단순하게 사용할 때 - 요청이 올 때마다 새로운 스..
-
프로세스와 스레드개발(ETC) 2024. 7. 17. 20:03
1. 프로세스- 프로그램은 실제 실행하기 전까지는 단순한 파일에 불과- 프로그램을 실행하면 프로세스가 만들어지고 프로그램이 실행됨- 이렇게 운영체제 안에서 실행중인 프로그램을 프로세스라 함- 프로세스는 실행 중인 프로그램의 인스턴스- 자바 언어로 비유하자면 클래스는 프로그램이고, 인스턴스는 프로세스이다. 프로세스는 실행 중인 프로그램의 인스턴스이다. 각 프로세스는 독립적인 메모리 공간을 갖고 있으며, 운영체제에서 별도의 작업 단위로 분리해서 관리된다. 각 프로세스는 별도의 메모리 공간을 갖고 있기 때문에 서로 간섭하지 않는다. 프로세스가 서로의 메모리에 직접 접근할 수 없다. 프로세스는 이렇듯 서로 격리되어 관리되기 때문에, 하나의 프로세스가 충돌해도 다른 프로세스에는 영향을 미치지 않는다. 쉽게 ..
-
멀티태스킹과 멀티프로세싱개발(ETC) 2024. 7. 16. 21:45
멀티 스레드를 이해하기 위해 멀티 태스킹과 프로세스 같은 운영체제 기본 개념들을 알아야한다. 멀티 스레드 이해를 위한 핵심 내용만 살펴보자. 1) 단일 프로그램 실행ex) 프로그램 2개 이상을 동시에 실행했다. 음악을 들으면서, 문서를 작성하는 것이다. 이 때 연산을 처리할 수 있는 CPU 코어는 1개만 있다. - 프로그램의 실행이란 프로그램 코드를 순서대로 CPU에서 연산(실행)하는 일이다.- 여기서 CPU 코어는 하나이므로 한 번에 하나의 프로그램 코드만 실행할 수 있다.- 만약 하나의 프로그램 안에 있는 코드를 모두 읽은 후에야 다른 프로그램의 코드를 실행할 수 있다면 사용자는 답답함을 느낀다.- 실제로 초창기 컴퓨터는 이렇게 실행했다.- 이를 해결하기 위해 CPU 코어로 여러 프로그램을 동시에 ..
-
Virtual Thread (kakao tech)프로그래밍/Java 2024. 7. 14. 13:47
01. Virtual Tread란 무엇인가- JDK 21(LTS)에 추가된 경량스레드- OS 스레드를 그대로 사용하지 않고 JVM 내부 스케줄링을 통해 수십~수백만개의 스레드를 동시에 사용할 수 있게한다. 기존 Thread의 문제점1) 정통적인 Java의 Thread - Java의 Thread는 OS Thread를 Wrapping 한 것 (Platform Thread)- Java 어플리케이션에서 Thread를 사용하면 실제로는 OS Thread를 사용한 것- OS Thread는 생성 갯수가 제한적이고 생성, 유지하는 비용이 비싸다.- 이 때문에 애플리케이션에서는 플랫폼 스레드를 효줄적으로 사용하기 위해 Thread Pool을 사용했다. 2) Throughput - 기본적인 Web Request 처리 ..
-
Virtual Thread (10분 테코톡)프로그래밍/Java 2024. 7. 13. 19:06
1. Thread 란 위 그림은 JVM 내부 Runtime Data Area이다. 1) Heap- new 명령어로 생성된 인스턴스가 저장되는 공간 2) Method Area - 클래스, 변수, static, 정적 변수가 저장되는 공간 - 안에 있는 runtime constant pool은 클래스 정보와 같은 메타 데이터가 저장되는 공간 3) Thread- 프로세스의 작은 작업 단위를 말한다. 3-1) PC (Process Counter Register)- 스레드 작업 흐름 단위를 저장하고 있다.3-2) JVM Stack- 메소드 스택을 저장.- 메소드가 종료되면 메모리 공간에서 사라지게 된다.3-2) Native Method Stack- JNI (Java Nativce Interface) 이다. ..
-
Java21 Virtual Thread프로그래밍/Java 2024. 7. 6. 16:55
1. Java 21에서 가상 스레드를 왜 도입하였을까. 1) IO 중심 작업에서 처리량(성능)을 늘리기 위함- 대표적인 IO 중심 작업은 DB를 사용하는 웹 서버를 예로 들수 있다. - DB를 사용하는 웹 서버는 DB와 통신하기 위해 IO를 사용한다.- 즉, IO 중심이 많은 서버에서 처리량을 늘리기 위해 나온 것이 가상 스레드다. 2) 트래픽이 많음 + IO가 많이 발생함 + 요청마다 쓰레드를 만드는 방식- 이 방식을 사용하게되면 스레드가 메모리를 사용하기 때문에 많은 메모리가 사용된다.- IO가 발생하면 응답 대기 (blocking) 되는 시간, 컨텍스트 스위칭에 따른 CPU 낭비가 발생함.- 위와 같은 문제 때문에 스레드를 늘리고 싶어도 늘리지 못한다. 그리고 서버들은 스레드를 pool로 관리한다..
-
Java Virtual Thread (13), 병목 현상(bottleneck)프로그래밍/Java 2024. 6. 29. 12:46
Virtual Thread를 적용했을 때 운영적인 측면에 대해 생각해보자. 1) tomcat-biz 가 하나의 spring boot application이다. 즉 두 개의 서버가 있고, 하나의 db가 있다.2) client 요청이 오면 톰켓이 받고 biz 로직에 따라 db 쿼리를 처리한다. 여기에 virtual thread를 사용하여 성능을 높일 수 있다. 그리고 서버 한 대를 줄이는 것을 고민하였다. 이 경우 아무런 이슈가 생기지 않을까? tomcat 은 200개의 요청이 400개로 늘어났다. 다만 virtual thread이기 때문에 thread의 개수 제한이 거의 없다고 볼 수 있기 때문에 이는 문제가 되지 않는다. 생각해야할 지점은 DB이다. 기존은 HikariCP를 이용해 Co..