분류 전체보기
-
함수형 인터페이스와 람다 표현식 소개프로그래밍/Java 2022. 6. 7. 22:24
함수형 인터페이스 추상 메서드를 하나만 가지고 있는 인터페이스 Single Abstract Method 인터페이스 @FunctionalInterface 애노테이션을 가지고 있는 인터페이스 다른 형태 (예를들어, static 혹은 default)의 메서드가 있더라도 함수형 인터페이스 이며, 추상 메서드가 하나여야 한다. @FunctionalInterface 는 Java 가 제공해주는 애노테이션이며, 함수형 인터페이스 조건위반시 에러를 띄운다. @FunctionalInterface public interface RunSomething { int doIt(int number); //void doIt(); static void printName() { System.out.println("lhk"); } defa..
-
토비의 봄 TV - Reactive Streams Operators (2)Spring/Webflux 2022. 6. 7. 14:42
Observable 형태를 발전시킨 Publisher 와 Subscriber 를 만드는 방식을 이전 시간에 살펴보았다. 이번엔 이를 조금 더 발전 시켜서 Operator 를 추가하는 방법과 동작방식을 살펴보고 실제 Reactive Strems 의 구현체인 Reactor 까지 사용해보자. 복습 ReactiveStreams 는 아래 기능들을 제공한다. Publisher Subscriber Subscription Processor Publisher 가 가장 중요한데, 데이터스트림을 계속해서 만들어내는 Provider 역할을 한다. 아래 코드를 살펴보자. import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org...
-
싱글톤 패턴 (Singleton Pattern)디자인 패턴 2022. 6. 4. 17:27
싱글톤 패턴이란? 인스턴스를 오직 하나만 제공하는 패턴 생성자가 여러차례 호출되더라도 계속해서 같은 객체를 리턴한 한다. 환경 세팅에 대한 정보 등 인스턴스가 여러개 일때 문제가 발생할 수 있는 경우가 있는데, 이 때 싱글톤 패턴을 사용하여 해결 기본 싱글톤 패턴 구현 package singleton; public class Settings { private static Settings instance; private Settings() {} public static Settings getInstance() { if (this.instance == null) { this.instance = new Settings(); } return this.instance; } } 1. 생성자를 private으로 만든..
-
토비의 봄 TV - Reactive Streams (1)Spring/Webflux 2022. 6. 4. 12:38
Reactive Programming 데이터가 변경될 때 마다 이벤트를 발생시켜서 데이터를 계속적으로 전달하는 Push 방식에 중점을 둔 프로그램 패러다임 Push 방식 (비동기 방식) 비동기 방식으로 데이터의 변화가 발생했을 때 변경이 발생한 곳에서 데이터를 보내주는 방식을 말한다. Iterable 과 Observable Iterable 데이터 순회를 가능하게 해주는 인터페이스이다. java 에서 제공되고 있는 Iterator 패턴의 인터페이스 list 와 같은 collection 들은 Iterable 을 상속 받고 있다. Iterable 를 상속받은 하위 객체들은 for-each loop 를 사용할 수 있다. Iterable 인터페이스 안에는 iterator 라는 메서드가 존재한다. Iterator ..
-
Blocking, Non-blocking, Sync, Async개발(ETC) 2022. 5. 9. 08:59
해당 개념을 이해하기 위해서는 다음의 두 용어를 짚고 넘어가야 한다. 제어권 제어권은 자신(함수)의 코드를 실행할 권리 같은 것이다. 제어권을 가진 함수는 자신의 코드를 끝까지 실행한 후, 자신을 호출한 함수에게 돌려준다. 결과값을 기다린다는 것 A 함수에서 B 함수를 호출했을 때, A 함수가 B 함수의 결과값을 기다리느냐의 여부를 의미한다. Blocking(블로킹) 과 Non-blocking(논블로킹) 블로킹과 논블로킹은 A 함수가 B 함수를 호출했을 때, 제어권을 어떻게 처리하느냐에 따라 달라진다. 1) 블로킹 A 함수가 B 함수를 호출하면 제어권을 B 함수에 넘겨준다. A함수가 B함수를 호출하면 B에게 제어권을 넘긴다. 제어권을 넘겨받은 B는 열심히 함수를 실행한다. A는 B에게 제어권을 넘겨주었..
-
Metaspace프로그래밍/Java 2022. 5. 6. 14:21
JVM의 여러 메모리 영역 중에 PermGen 메모리 영역이 없어지고 Metaspace 영역이 생겼다. PermGen permanent generation, 클래스 메타데이터를 담는 곳. Heap 영역에 속함. 기본값으로 제한된 크기를 가지고 있음. -XX:PermSize=N, PermGen 초기 사이즈 설정 -XX:MaxPermSize=N, PermGen 최대 사이즈 설정 Metaspace 클래스 메타데이터를 담는 곳. Heap 영역이 아니라, Native 메 영역이다. 기본값으로 제한된 크기를 가지고 있지 않다. (필요한 만큼 계속 늘어난다.) 자바 8부터는 PermGen 관련 java 옵션은 무시한다. -XX:MetaspaceSize=N, Metaspace 초기 사이즈 설정. -XX:MaxMetas..
-
배열 Parallel 정렬프로그래밍/Java 2022. 5. 6. 13:45
Arrays.parallelSort() Fork/Join 프레임워크를 사용해서 배열을 병렬로 정렬하는 기능을 제공한다. 병렬 정렬 알고리듬 배열을 둘로 계속 쪼갠다. 합치면서 정렬한다. sort()와 parallelSort() 비교 int size = 1500; int[] numbers = new int[size]; Random random = new Random(); IntStream.range(0, size).forEach(i -> numbers[i] = random.nextInt()); long start = System.nanoTime(); Arrays.sort(numbers); System.out.println("serial sorting took " + (System.nanoTime() - s..
-
CompletableFuture (5)프로그래밍/Java 2022. 5. 6. 11:30
지난 시간에 이어 진행해보자. 이번엔 CompletableFuture 를 가지고 여러 작업을 조합하는 방법과 예외를 처리하는 방법에 대해 살펴보자. Future 만 가지고는 특정 작업들을 이어서 처리하는게 힘들었다. (예를들면, 이벤트 정보 가져온 다음 이벤트에 참석하는 회원 목록 가져오기 등.) 콜백을 줄 수 없었기 때문에 비동기적인 작업을 이어서 처리하기가 힘들었던 것이다. 조합하기 thenCompose( ) 두 작업이 서로 이어서 실행하도록 조합 public class CompletableFutureStudy { public static void main(String[] args) throws ExecutionException, InterruptedException { CompletableFutur..