EnumMap vs HashMap, 그리고 InteruptedException
Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.
이라고 한다. JavaDoc 에서 extremely compact and efficient 라고 표현을 하다니. Map 의 K,V 에서 Key 부분의 타입이 Enum 인 경우에는 반드시 EnumMap 을 쓰도록 해야겠다.
우선 checked exception 과 unchecked exception 이 있는데. 이게 뭔가 하면,
checked exception 은 compile 단계에서 검출이 되며, 반드시 catch 를 하거나 method 에 throws 함을 나타내야 한다. 그렇지 않으면 compile 이 안되니까.
unchecked 는 runtime 시에 검출 될수 있는 것들이다.
그럼 error 는? error 는 시스템단에서 발생할 수 있는 것들이어서 exception 을 통해 방어하기가 어렵다.
자바에서 Error 아래의 exceptions 들과 RuntimeException 은 unchecked 이고, 그외의 나머지 throwable 아래의 것들은 전부 checked 이다.
예를들면
checked 에는
IOException
InterruptedException
SQL Exception
IOException
unchecked 에는
NullPoint Exception
ArrayIndexOutOfBoundsException
NumberFormatException
등이 있다.
그럼 이중 InterruptedException 은 왜 중요한가?
try catch 중 ExecutionException 과 InterruptedException 을 하나로 묶어서 처리하고 있었는데, 이번에 보니 둘이 차이가 있다. ExecutionException 의 경우 root casue exception 을 확인해서 Transient 인지 NonTransient 인지 둘도 아니면 Runtime 으로 처리해서 다시 돌린다.
InterruptedException 의 경우, 원래 안했었는데,
Thread.currentThread().interrupt();
를 추가해줬다. 이유는 Thread.interrupted()는 thread 의 interrupt state를 초기화 시키기 때문이다. 그렇기 때문에InterruptedException 을 catch 했을때는 이미 Thread.currentThread().isInterrupted(); 가 false 가 된다. Thread 를 직접 구현한 상황이 아닌 이상, 이 state 를 그대로 유지해서 caller 에게 알리는 것이 best 이므로 위와 같이 Thread.currentThread().interrupt(); 를 호출하는 것이다.
참조 링크
https://dzone.com/articles/why-do-we-need-threadcurrentthreadinterrupt-in-int
https://stackoverflow.com/questions/4906799/why-invoke-thread-currentthread-interrupt-in-a-catch-interruptexception-block
Comments
Post a Comment