2021-11-30

1130 finalize 와 cleaner 사용을 피하라 2

 Cleaner manages a set of object references and corresponding cleaning actions.

Cleaning actions are registered to run after the cleaner is notified that the object has become phantom reachable. The cleaner uses PhantomReference and ReferenceQueue to be notified when the reachability changes.

Each cleaner operates independently, managing the pending cleaning actions and handling threading and termination when the cleaner is no longer in use. Registering an object reference and corresponding cleaning action returns a Cleanable. The most efficient use is to explicitly invoke the clean method when the object is closed or no longer needed. The cleaning action is a Runnable to be invoked at most once when the object has become phantom reachable unless it has already been explicitly cleaned. Note that the cleaning action must not refer to the object being registered. If so, the object will not become phantom reachable and the cleaning action will not be invoked automatically.


cleaner 를 우선 만든다.

cleaner객체에게 clean할 객체를 등록한다(register) -> cleanable 객체를 반환해준다. 

혹여나 객체가 수거되지 않을경우 - 안전성을 가한다는 측면에서 , (굳이)  명시적으로 clean을 불러서 해당 객체를 수거할 수 있게된다..는 것같다!!!!!?!!?

이를테면, Autoclosable을 구현한 객체는 close()를 부르면 수거가 된다.

그런데 어떠한 원인에서 이 close()가 호출되지 않았을때, cleaner에게 이 객체를 clean해달라고 register해두면, cleaner가 이 객체를 수거해줄 것이다. (그러나, 완전하지는 않다. 자바는 이러한 객체수거를 개발자가 직접적으로 조절하기를 바라지 않는것같다.)

cleaner의 clean()메서드는 Runnable을 구현하는 스레드이다.  이 스레드를 커스터마이징 한 Runnable & clean할 객체참조를 함께 넘겨주면서 register한다.



댓글 없음:

댓글 쓰기

0328 fdisk, mkfs, mount, fstab

 1. 하드디스크를 붙인다. 2. fdisk -l로 하드디스크를 확인한다.  - interactiive한 커맨드모드 사용하여 (m) 붙인 하드디스크의 파티셔닝을 한다.  - 마지막에 w를 해야 실제로 반영이 된다.  3. mkfs를 하여 어떤 파일시스...