Clean up code in finalize() or finally()?

前端 未结 9 1040
不知归路
不知归路 2020-12-19 04:16

I had the general view that clean up of resources is done in the finally block,
recently I found this particular code snippet in a class and it was overridi

9条回答
  •  执笔经年
    2020-12-19 04:49

    Joshua Bloch makes a very clear recommendation in his book Effective Java (2nd Edition). Copied from chapter 2 Item 7: Avoid finalizers:

    Finalizers are unpredictable, often dangerous, and generally unnecessary. Their use can cause erratic behavior, poor performance, and portability problems. Finalizers have a few valid uses, which we’ll cover later in this item, but as a rule of thumb, you should avoid finalizers.

    Please read the reference to find out why.

提交回复
热议问题