프로그래밍
-
CompletableFuture (2) - Excutors프로그래밍/Java 2022. 5. 6. 01:58
쓰레드를 만들고 관리하는 작업을 기존 Thread 를 생성하여 Runnable 를 구현하는 방식보다 보다 고수준의 API 에게 위임. (내부적으로 Runnable 방식을 쓰는것은 맞다.) Excutors 가 쓰레드를 만들고, 우린 Runnable 만 제공해주면 된다. (Runnable 안에 우리가 해야할 일만 정의해주면 됨) Excutors 를 만들고, 필요없으면 종료 시키는 작업은 Excutors 가 직접 해준다. 다시 위 내용을 정리해보자. 고수준 (High-Level) Concurrency 프로그래밍 쓰레드를 만들고 관리하는 작업을 애플리케이션에서 분리 그런 기능을 Executors 에게 위임 Executors 가 하는 일 쓰레드 만들기 : 애플리케이션이 사용할 쓰레드 풀을 만들어 관리한다. 쓰레드..
-
CompletableFuture (1) - 자바 Concurrent 프로그래밍 소개프로그래밍/Java 2022. 5. 6. 01:35
CompletableFuture 을 학습하려면 이전까지의 Java Concurrent 프로그래밍을 알아야한다. Concurrent 소프트웨어 동시에 여러 작업을 할 수 있는 소프트웨어 ex) 웹 브라우저로 유튜브를 보면서 키보드로 문서에 타이핑을 할 수 있다. ex) 녹화를 하면서 인텔리J로 코딩을 하고 워드에 적어둔 문서를 보거나 수정할 수 있다. Java 에서 지원하는 Concurrent 프로그래밍 멀티 프로세싱 (ProcessBuilder) 멀티 쓰레드 이번 시간에는 멀티 쓰레드에 관한것만 보자. Java 멀티 쓰레드 프로그래밍 Thread / Runnable 아래는 쓰레드를 구현하는 두가지 방법이다. 1. Thread 를 상속받아 구현하는 방법 // Thread 상속 public static vo..
-
Date와 Time API프로그래밍/Java 2022. 5. 5. 17:15
기존에 사용하던 날짜와 시간 Api 는 아래와같다. Date date = new Date(); Calendar calendar = new Gregoriancalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat(); Java8 에 새로운 날짜와 시간 API 가 생긴 이유 그 전까지 사용하던 java.util.Date 클래스는 mutable 하기 때문에 thread safe 하지 않다. .setTime( ) 시 변경이 가능하며, 멀티 쓰레드 환경에서 안전하게 쓰기 어렵다. 클래스 이름이 명확하지 않다. Date 인데 시간까지 다룬다. 버그 발생할 여지가 많다. 타입 안정성이 없고, 월이 0부터 시작한다거나 등 날짜 시간 처리가 복잡한 애플리케이션에서는..
-
Optional프로그래밍/Java 2022. 5. 5. 09:22
Optional Java8 에 추가된 새로운 인터페이스. 비어있을수도 있고, 값 하나를 담고 있을수도 있는 컨테이너 인스턴스의 타입이다. 즉, 오직 값 한 개가 들어있을 수도 없을 수도 있는 컨테이너. 자바 프로그래밍에서 NullPointerException 을 보는 이유. null 를 리턴하니까! ( null 이 리턴되는 자체가 문제이다. ) null 체크를 깜빡했으니까! 메소드에서 작업 중 특별한 상황에서 값을 제대로 리턴할 수 없는 경우 선택할 수 있는 방법 예외를 던진다. 에러가 발생하게되면 자바는 스택 트레이스를 찍는데, ( 이 에러가 발생하기 전까지의 어떠한 콜 스택을 거쳐서 에러가 발생하게 되었는지에 대한 정보) 이 자체로 리소스를 사용하는거여서 부담이 가기 때문에 필요할 때에만 사용해야지 ..
-
Stream프로그래밍/Java 2022. 5. 4. 22:31
Stream 이란? 연속된 데이터를 처리하는 Operation(조작)의 모임. 컬렉션 이나 배열 등에 저장되어 있는 요소들을 하나씩 참조하며 반복적인 처리를 가능케 하는 기능이다. Stream 을 이용한다면 불필요한 반복문이나 분기처리를 쓰지 않고도 직관적인 코드를 작성할 수 있다. Stream 특징 데이터를 담고 있는 저장소(컬렉션)이 아니다. Functional in nature, 즉 Stream 이 처리하는 데이터 소스를 변경하지 않는다. 무제한일 수도 있다. 실시간으로 계속해서 Stream 으로 들어온 데이터를 Stream 으로 받아 처리할 수 있다. Short Circuit 메소드를 사용해서 제한하는 것도 가능하다. 중개 오퍼레이션들은 근복적으로 Lazy 하다. Stream API 는 크게 중계..
-
Lombok 동작원리프로그래밍/Java 2022. 5. 3. 19:02
Lombok 은 컴파일 시점에 바이트코드를 변환하여 원하는 부분을 주입해주는 방식으로 동작한다. 조금 더 구체적으로 설명하자면, 컴파일 시점에 애노테이션 프로세서를 사용하여 소스코드의 AST(abstract syntax tree)를 조작한다. Lombok이 처리되는 과정은 다음과 같다 javac는 소스파일을 파싱하여 AST트리를 만든다. Lombok은 AnnotaionProcessor에 따라 AST 트리를 동적으로 수정하고 새 노드(소스코드)를 추가하고 마지막으로 바이트 코드를 분석 및 생성한다. (컴파일 과정에서 생성된 Syntax Tree는 com.sun.source.tree.*에서 public accesss를 제공한다.) 최종적으로 javac는 Lombok Annotation Processor에 의..
-
다이나믹 프록시프로그래밍/Java 2022. 5. 3. 17:42
런타임(애플리케이션이 실행되는 도중)에 특정 인터페이스들을 구현하는 클래스 또는 인스턴스를 만드는 기술을 다이나믹 프록시라 한다. 프록시 인스턴스 프록시 인스턴스는 Object Proxy.newProxyInstance(ClassLoader, Interfaces, InvocationHandler) 를 통하여 구현할 수 있다. BookService bookService = (BookService) Proxy.newProxyInstance(BookService.class.getClassLoader(), new Class[]{BookService.class}, new InvocationHandler() { BookService bookService = new DefaultBookService(); @Overri..
-
클래스 로더 (class loader)프로그래밍/Java 2022. 5. 1. 15:14
JVM internal 아키텍처 에서 클래스 로더 시스템을 자세하게 그리면 위와 같이 구성되어 있다. 참고 https://hyokeun0419.tistory.com/77 클래스 로더 로딩, 링크, 초기화 순으로 진행된다. 로딩 클래스 로더가 .class 파일을 읽고 그 내용에 따라 적절한 바이너리 데이터를 만들고 "메소드" 영역에 저장 이 때 메소드 영역에 저장하는 데이터 FQCN(fully qualified class name, 패키지 경로까지 포함된 클래스 이름) , 클래스 | 인터페이스 | 이늄, 메소드와 변수 로딩이 끝나면 해당 클래스 타입의 Class 객체를 생성하여 "힙" 영역에 저장 특정 클래스를 읽을 때 최상위 부모에게 먼저 읽어달라는 요청을 한다. (Bootstrap > Extension..