java softReference ReferenceQueue

 http://hi.baidu.com/hbc8848/blog/item/3ddb5e94a6b06a1bd31b7038.html

 

《Java的内存管理2》提到finalize方法延缓了GC在heap中对类实例的回收,尤其在JVM内存吃紧时,所幸的是这种延缓仅会发生一次。也正是因为finalize方法只会在GC首次打算回收该实例时被调用,因此finalize方法极大地不同于C++中的析构函数:当一个实例再次被JVM装入内存,直到它被销毁前,你无法在finalize方法中执行关闭文件连接等操作。

在C++缓存管理过程中,程序员通常会自己新建一个队列,将被清除出内存的实例推入队列,等下次被调用时再取出。好在java.lang.ref.ReferenceQueue帮我们实现了这个队列,我们只需将软引用实例在队列中注册即可。因此,如果你需要对象中的一些操作,仅在它的实例被GC回收时才执行,便可以通过ReferenceQueue队列调用它们。JavaDoc1.6写道:

SoftReference

public SoftReference(T referent,
                     ReferenceQueue<? super T> q)
创建一个引用给定对象的新的软引用,并向给定队列注册该引用。

参数:
referent - 新的软引用将引用的对象
q - 该引用向其注册的队列,如果不需要注册,则为 null

我将上篇示例代码中的finalize()方法的内容注释掉,新建了一个GC监听线程,监听哪些实例被GC清理到ReferenceQueue队列中,并显示它们。

ReferenceQueue中的remove()方法将返回队列中下一个新生效的实例引用(所有实例都已经在队列中注册,当一个实例被GC清理出内存后,该实例在队列中的引用就会生效)。线程随后被阻塞,直到队列中有新生效引用为止。JavaDoc1.6写道:

remove

public Reference<? extends T> remove()
                              throws InterruptedException
移除此队列中的下一个引用对象,阻塞到某个对象变得可用为止。

返回:
某个引用对象,阻塞到某个对象变得可用为止。
抛出:
InterruptedException - 如果等待被中断

控制台输出结果如下:

Creating bookshelf: 1
Creating bookshelf: 2
Monitor:1 was collected.
Creating bookshelf: 3
Creating bookshelf: 4
Creating bookshelf: 5
Creating bookshelf: 6
Creating bookshelf: 7
Creating bookshelf: 8
Creating bookshelf: 9
Creating bookshelf: 10
Creating bookshelf: 11
Creating bookshelf: 12
Creating bookshelf: 13
Monitor:2 was collected.
Monitor:3 was collected.
Monitor:4 was collected.
Monitor:5 was collected.
Monitor:6 was collected.
Monitor:7 was collected.
Monitor:8 was collected.
Monitor:9 was collected.
Monitor:10 was collected.
Monitor:11 was collected.
Creating bookshelf: 14
Creating bookshelf: 15
Creating bookshelf: 16
Creating bookshelf: 17
Creating bookshelf: 18
Creating bookshelf: 19
Creating bookshelf: 20
Creating bookshelf: 21
Creating bookshelf: 22
Monitor:12 was collected.
Monitor:13 was collected.
Monitor:14 was collected.
Monitor:15 was collected.
Monitor:16 was collected.
Monitor:17 was collected.
Monitor:18 was collected.
Monitor:19 was collected.
Monitor:20 was collected.
Monitor:21 was collected.
Creating bookshelf: 23
Creating bookshelf: 24
Creating bookshelf: 25
Creating bookshelf: 26
Creating bookshelf: 27
Creating bookshelf: 28
Creating bookshelf: 29
Creating bookshelf: 30
Creating bookshelf: 31
Creating bookshelf: 32
Monitor:31 was collected.
Monitor:22 was collected.
Monitor:23 was collected.
Monitor:24 was collected.
Monitor:25 was collected.
Monitor:26 was collected.
Monitor:27 was collected.
Monitor:28 was collected.
Monitor:29 was collected.
Monitor:30 was collected.
Creating bookshelf: 33
Creating bookshelf: 34
Creating bookshelf: 35
Creating bookshelf: 36
Creating bookshelf: 37
Creating bookshelf: 38
Creating bookshelf: 39
Creating bookshelf: 40
Creating bookshelf: 41
Creating bookshelf: 42
Creating bookshelf: 43
Monitor:32 was collected.
Monitor:33 was collected.
Monitor:34 was collected.
Monitor:35 was collected.
Monitor:36 was collected.
Monitor:37 was collected.
Monitor:38 was collected.
Monitor:39 was collected.
Monitor:40 was collected.
Monitor:41 was collected.
Creating bookshelf: 44
Creating bookshelf: 45
Creating bookshelf: 46
Creating bookshelf: 47
Creating bookshelf: 48
Creating bookshelf: 49
Creating bookshelf: 50

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值