본문 바로가기

아는 만큼 보인다

Builder Pattern 사용 예 생성자 또는 정적 펙토리 메소드의 인자가 많고, 인자들 중 선택항목이 많은 경우, Builder 패턴의 적용을 생각해 볼 수 있음 NutritionFacts nutrition = new NutritionFacts.Builder(9) .setB(8) .setC(7) .build(); 코드 public class NutritionFacts { private int necessaryA; // immutable object private int optionalB; private int optionalC; // builder inner class public static class Builder { private int a = 0; // 기본값 초기화 private int b = 0; private in.. 더보기
Service Provider Framework Pattern * 구성 Compenent 1) Service Interface : Service 사용자에게 제공하기 위해 표준?으로 정한 API가 정의된 Interface 2) Service Registration API : Provider Interface의 구현체를 등록하는 API 3) Service Access API : Service Interface의 구현체를 얻어오는 API (정적 팩토리 메소드) 4) Provider Interface : Service Interface의 하위 객체를 생성해주는 API가 정의된 Interface * Service Provider Framework Pattern 구현 간단예 (JDBC가 해당됨) // 1) Service Interface public interface Serv.. 더보기
Java 클래스 멤버 변수들의 초기화 순서 Java 클래스 멤버 변수들의 초기화 순서 1. static 변수 선언부 : 클래스가 로드 될 때 (메모리 모델상 Methd area 에 올라감) 변수가 제일 먼저 초기화 됨 2. 필드 변수 선언부 : 객체 생성 될 때 (메모리 모델상 Heap area에 올라감) 생성자 block 보다 앞서 초기화 함 3. 생성자 block : 객체 생성 될 때 (메모리 모델상 Heap area에 올라감) JVM이 내부적으로 locking (thread safe 영역임) 필드 변수 중 final 변수의 가시화는 (다른 스레드에 공개하는 시점은) 생성자 block이 끝난 다음. 필드 변수 선언부에서 이미 초기화 되었다면 그 값들은 덮어 씀 참고: http://cluster1.cafe.daum.net/_c21_/bbs_se.. 더보기
Java Memory Model 영역 대상 Method area (GC의 permanent generation) 메소드의 바이트코드 클래스(static) 변수 constant pool Heap area (GC의 young generation, old generation) instance 변수 (string constant포 함) Stack area local 변수 더보기
Thread pool 에서 결과(Future) 받기 1. 끝나기 전에 받기 - 하나씩 받기 future = executorService.submit(...job...); future = completionService.submit(...job...); 2. 끝날 때 까지 대기하여 받기 - 모두 끝난 후에 한꺼번에 다 받기 futureList = executorService.invokeAll(... job list ...); - 제일 일찍 끝난 결과 하나만 받기 future = executorService.invokeAny(... job list ...); - 먼저 끝나는 순서대로 하나씩 모두 받기 for (...) { future = completionService.take(); } 더보기
Thread 상태 new → Runable↔ waiting by lockObj.wait() or synchronizers (latch, barrier, semaphore ...)↔ timed waiting by lockObj.wait(time) or sleep()↔ blocked by acquiring lock or I/O request→ terminated 더보기
Thread-Safe 를 위한 Keywords Thread-Safe 를 위한 Keywords 1. volatile - 해당 공유되는 변수를 스레드별로 캐시하여 사용하지 말라는 뜻- 64 bit 연산도 32 bit 연산을 두 번에 나눠 하지 않고 단일 연산으로 처리- 해당 변수에대해 (read-연산-write) 를 묶어 thread-safe 하게 처리하는 것은 아님 2. final- 읽기 전용 변수- 유지보수/가독성에도 도움 3. java.util.concurrent.atomic- volatile 특성 + lock free thread-safe- lock를 사용하지 않고 CAS (Compare And Set/Swap)기법 사용하여 thread-safe 실현- thread-safe 메소드 제공increametAndGet, addAndGet, .. 더보기
Executors 팩토리 클래스 Executors 팩토리 클래스 -- ExecutorService 인터페이스 타입의 ThreadPoolExecutor 객체 반환 -- ScheduledExecutorService 인터페이스 타입의 SheduledThreadPoolExecutor 객체 반환 Executor 인터페이스 -- ExecutorService 인터페이스 -- AbstractThreadPoolExecutor 추상클래스 -- ThreadPoolExecutor 클래스 -- ScheduledThreadPoolExecutor 클래스 -- ScheduledExecutorService 인터페이스 -- ScheduledThreadPoolExecutor 클래스 더보기
wait 메소드 사용시 지켜야 하는 것들 1. 항상 조건 검사 : wait(대기) 할지? 진행 할지?2. wait에서 return 한 후에도 조건 검사 (notifyAll 에 의해 깨어난 경우, 여러 조건큐의 스레드가 깨어남)3. 1번 2번의 이유로 wait 메소드는 반복문 안에서 호출4. 조건 검사는 조건 큐의 lock에 의해 동기화5. wait, notify, notifyAll 메소드를 호출할 때는 조건 큐에 해당하는 락을 확보6. 조건 검사후 작업 진행시 작업이 완료 될때 까지 락을 해제하면 안됨 이 모든 것을 만족하는 코드의 표준적인 형태.void myWait() throws InterreuptedException { synchronized(lock) { while(!myConditionCheck()) { lock.wait(); } //.. 더보기
문자열 속 일부 유니코드 치환 // 문자열 속 일부 유니코드 치환public void replaceUnicodeNotation(String input) {while( true ) {Matcher m = Pattern.compile("\\\\u([\\da-fA-F]{4})").matcher(input);if(!m.find()) break; char ch = (char) Integer.parseInt(m.group(1), 16);String a = String.valueOf( ch );input = m.replaceFirst( Matcher.quoteReplacement(a) );}return input;} 더보기