본문 바로가기

Programming137

자바 멀티 스레드 프로그래밍 1. 프로세스와 스레드 스레드 - Java 프로그램은 하나의 프로세스로 만들어져 실행 - 프로세스는 실행중인 프로그램 - 스레스는 실행 중인 프로그램 내에 존재하는 소규모 실행 흐름 - 스레드는 경량 프로세스 멀티 스레드 - 하나의 프로세스 내부에서 여러 스레드가 만들어져 동시 실행 될 수 있음 - 자바 프로그램은 하나의 스레드(main)에서 시작함 - main 스레드에서 자식 스레드를 만들어 시작시킬 수 있음 - 그러면 여러 스레드가 동시에 독립적으로 실행되고 종료 프로세스 스레드 멀티스레드 실행 단위 하나의 프로그램 또는 작업 단위 프로세스 내부에서 실행되는 작은 실행 단위 하나의 프로세스 내에서 동시에 실행됨 자원 공유 운영체제에 의해 독립적인 자원 할당 프로세스 내부 자원 공유 프로세스 내부 자원.. 2023. 6. 12.
Java의 Arrays.sort()를 사용할 때 원시형 타입이 아닌 참조형 타입으로 전달하면 성능 향상이 가능하다 (Dual-Pivot Quicksort, Tim Sort) Arrays.sort()를 사용할 때 원시형 타입이 아닌 참조형 타입으로 전달하면 성능 향상이 가능하다. 그 전에 먼저 sort() 메서드에 대해 간단히 살펴보자. 아래와 같이 다양한 sort() 메서드가 오버로딩 되어 있다. public class Arrays { private Arrays() {} public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, 0, a.length); } public static void sort(int[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); DualPivotQuicksort.sort(a, 0, fromIndex, to.. 2023. 6. 8.
자바의 method reference method reference - 기존에 이미 선언되어 있는 메서드를 지정하고 싶을 때 - :: 오퍼레이터 사용 - ClassName::staticMethodName : 객체의 static method를 지정할 때 - ClassName::instanceMethodName : 객체의 instance method를 지정할 때 - ClassName::new : 객체의 constructor를 지정할 때 - objectName::instanceMethodName : 선언된 객체의 instance method를 지정할 때 ClassName::staticMethodName Function strToInt = Integer::parseInt; int five = strToInt.apply("5"); objectName.. 2023. 6. 3.
자바의 여러가지 functional interface + comparator 1. Supplier @FunctionalInterface public interface Supplier { T get(); } 사용 예시 public static void main(String[] args) { Supplier myStringSupplier = () -> {return "hello world!";}; myStringSupplier.get(); Supplier myRandomDoubleSupplier = () -> Math.random(); System.out.println("myRandomDoubleSupplier.get() = " + myRandomDoubleSupplier.get()); printRandomDoubles(myRandomDoubleSupplier, 5); } publi.. 2023. 6. 3.
자바의 Functional Interface와 andThen 메서드 이해하기 1. Function interface @FunctionalInterface public interface Function { R apply(T t); } input 타입은 T 이고 return 타입은 R인 메서드 이것을 Object 형태로 구현하면 어떻게 될까? 아래처럼 쓸 수 있다. public class Adder implements Function { @Override public Integer apply(Integer integer) { return integer + 10; } } public static void main(String[] args) { Function adder = new Adder(); Integer apply = adder.apply(5); System.out.println.. 2023. 6. 2.