• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance of A can only create one Instance of B problem  RSS feed

 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Suppose you have the following 2 classes:



What I need to do is throw an exception if someone calls createB that would result in 2 B's being reachable on the heap.

Client code example:



My inclination is that this cannot be accomplished by my code since I don't know in line 3 that the original b is not reachable until after my code has finished.

If I'm missing something, any help is appreciated.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't possible to know whether there are unreachable objects of a class. It's possible to find out when an object is garbage collected by using classes in the java.lang.ref package, but unreachable objects are not guaranteed to be garbage-collected immediately.

Where did this requirement come from? Could it be replaced by an ordinary singleton?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely the idea is that, once an object is unreachable, it's gone. The fact that the memory may not be reclaimed by GC for a while should not matter.

If you're writing code where it matters if there are unreachable instances of a class, it sounds like you need to re-think that code.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Surely the idea is that, once an object is unreachable, it's gone. The fact that the memory may not be reclaimed by GC for a while should not matter.

If you're writing code where it matters if there are unreachable instances of a class, it sounds like you need to re-think that code.


B instances share a resource whose use can interfere with each other for some instance of A. Removing this liability might be difficult.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just create a single instance of B in your A class and then the createB() method just reinitialises this instance and returns a reference to it.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
It isn't possible to know whether there are unreachable objects of a class. It's possible to find out when an object is garbage collected by using classes in the java.lang.ref package, but unreachable objects are not guaranteed to be garbage-collected immediately.

Where did this requirement come from? Could it be replaced by an ordinary singleton?


I thought (apparently mistakenly) that even though one cannot guarantee that gc be done, you could use the mentioned package to determine whether or not an object was reachable. However, even if that worked, it still doesn't solve the original problem.

I don't think an ordinary singleton will work since different A instances don't create interfering Bs.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
Why don't you just create a single instance of B in your A class and then the createB() method just reinitialises this instance and returns a reference to it.






If I did what you suggest, it would result in a singleton by A instance, but I'd much rather throw an exception than have the user understand that after line 3, b points to a new B instance.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Surely the idea is that, once an object is unreachable, it's gone. The fact that the memory may not be reclaimed by GC for a while should not matter.


Should have mentioned that that is the idea. It does not matter whether or not it has been GC'd, just that it will not be used anymore.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!