Permalink
Browse files

Add an example of using WeakRef::RefQueue

  • Loading branch information...
1 parent 95f83e2 commit 7cbaa2f6fc8a211e1e32390d67bb4ce70e7a91f9 @headius committed Mar 12, 2010
Showing with 29 additions and 1 deletion.
  1. +16 −1 README.txt
  2. +13 −0 examples/refqueue_use.rb
View
@@ -16,7 +16,22 @@ reference types like soft and phantom references.
Just require 'weakling'. It will require 'weakref' along with the refqueue JRuby
extension and the weakling/collections library containing the weak id hash.
-== Example ==
+== RefQueue Example ==
+
+require 'weakling'
+require 'java'
+
+q = WeakRef::RefQueue.new
+wr = WeakRef.new(Object.new, q)
+puts "weakref object: #{wr.__id__}"
+
+puts "running GC"
+java.lang.System.gc
+
+puts "weakref alive?: #{wr.weakref_alive?}"
+puts "weakref object from queue: #{q.poll.__id__}"
+
+== WeakRef::IdHash Example ==
require 'weakling'
View
@@ -0,0 +1,13 @@
+require 'weakling'
+require 'java'
+
+q = WeakRef::RefQueue.new
+wr = WeakRef.new(Object.new, q)
+puts "weakref object: #{wr.__id__}"
+
+puts "running GC"
+java.lang.System.gc
+
+puts "weakref alive?: #{wr.weakref_alive?}"
+puts "weakref object from queue: #{q.poll.__id__}"
+

0 comments on commit 7cbaa2f

Please sign in to comment.