You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
일단 책의 질문은 DelayQueue는 제네릭타입을 Delyed로 한정해놓았기 때문에 클라이언트에서는 컴파일타임에
바로 Queue에서 꺼낸 원소가 Delayed라는 것을 알고있고 클라이언트는 형변환없이; 사용할 수 있어. 아래는 극단적으로 로타입으로 선언한 DelayQueue 에서 Delay 타입을 꺼낼 수 있다는거를 보여주는거얌
DelayedQueue 는 Queue에 삽입하는 원소들에 "Delay" 시간을 지정해 줄 수 있는 Queue 를 말한다.
즉, 넣고 바로 poll 한다고 뺄 수 있는 것이 아니라, Delay 시간만큼 지나야 뺄 수 있다.
여기 삽입하는 원소는 Delayed 인터페이스를 구현해야하고 구현예시는 아래와 같다.
publicclassDelayEventimplementsDelayed {
privateStringdata;
privatelongstartTime;
publicStringgetData() {
returndata;
}
publiclonggetStartTime() {
returnstartTime;
}
publicDelayEvent(Stringdata, longdelayInMilliseconds) { // delay 지정시간을 입력하고 this.data = data;
this.startTime = System.currentTimeMillis() + delayInMilliseconds; // 그 이후에 poll 될 수 있도록 세팅
}
@OverridepubliclonggetDelay(TimeUnitunit) {
longdiff = startTime - System.currentTimeMillis();
returnunit.convert(diff, TimeUnit.MILLISECONDS);
}
@OverridepublicintcompareTo(Delayedo) { // Queue 안에서 만료시간에 따라 Sorting 되어야하기 때문에 필요return (this.startTime - ((DelayEvent) o).startTime) <= 0 ? -1 : 1;
}
}
174쪽 마지막줄
DelayedQueue에 대한 설명과 사용하는 클라이언트는 형변환 없이 곧바로 Delayed클래스의 메서드를 호출하는 예시를 만들어줘
The text was updated successfully, but these errors were encountered: