프로그래밍/Java
-
메소드 레퍼런스프로그래밍/Java 2022. 6. 8. 22:44
람다가 하는 일이 기존 메소드 또는 생성자를 호출하는 거라면, 메소드 레퍼런스를 사용해서 간결하게 표현할 수 있다. 즉, 람다식을 더 간결하게 표현하는 방법이 메소드 레퍼런스 이다. 메소드를 참조하는 방법 메소드 또는 생성자의 매개변수로 람다의 입력값을 받는다. 리턴값 또는 생성한 객체는 람다의 리턴값이다. 아래 예제코드를 살펴보자. import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.function.Supplier; import java.util.function.UnaryOperator; public class App { public static void main(String..
-
자바에서 제공하는 함수형 인터페이스프로그래밍/Java 2022. 6. 7. 23:12
함수형 인터페이스를 따로 정의하지 않고도 자바에서 기본으로 제공하는 함수형 인터페이스가 있다. 이를통해 람다 표현식, 메서드 레퍼런스, 생성자 레퍼런스를 사용해서 구현할 수 있는 함수형 인터페이스를 살펴보자. 자바에서 미리 정의해둔 자주 사용할만한 함수 인터페이스 Function T타입을받아서R타입을리턴하는함수인터페이스 import java.util.function.Function; class Plus10 implements Function { @Override public Integer apply(Integer integer) { return integer + 10; } } public class Foo { public static void main(String[] args) { // 방법 1 Plus..
-
함수형 인터페이스와 람다 표현식 소개프로그래밍/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..
-
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..
-
CompletableFuture (4)프로그래밍/Java 2022. 5. 6. 10:13
자바에서 비동기(Asynchronous) 프로그래밍을 가능케하는 인터페이스이다. Future 인터페이스는 java5부터 java.util.concurrency 패키지에서 비동기의 결과값을 받는 용도로 사용했지만 비동기의 결과값을 조합하거나, error를 핸들링할 수가 없었다. 자바8부터 CompletableFuture 인터페이스가 소개되었고, Future 인터페이스를 구현함과 동시에 CompletionStage 인터페이스를 구현한다. CompletionStage 는 비동기 연산 Step을 제공해서 계속 체이닝 형태로 조합이 가능하다. CompletionStage 란, 하나의 비동기 작업을 수행하고 완료가 되었을때 여기에 의존적으로 또 다른 작업을 수행할 수 있도록하는 명령들을 가지고있는 인터페이스 Fut..
-
CompletableFuture (2) - Excutors프로그래밍/Java 2022. 5. 6. 01:58
쓰레드를 만들고 관리하는 작업을 기존 Thread 를 생성하여 Runnable 를 구현하는 방식보다 보다 고수준의 API 에게 위임. (내부적으로 Runnable 방식을 쓰는것은 맞다.) Excutors 가 쓰레드를 만들고, 우린 Runnable 만 제공해주면 된다. (Runnable 안에 우리가 해야할 일만 정의해주면 됨) Excutors 를 만들고, 필요없으면 종료 시키는 작업은 Excutors 가 직접 해준다. 다시 위 내용을 정리해보자. 고수준 (High-Level) Concurrency 프로그래밍 쓰레드를 만들고 관리하는 작업을 애플리케이션에서 분리 그런 기능을 Executors 에게 위임 Executors 가 하는 일 쓰레드 만들기 : 애플리케이션이 사용할 쓰레드 풀을 만들어 관리한다. 쓰레드..