• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

find number of objects for a particular class and create objects accordingly?

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to allow the user to create only some x objects for a class.
If the number of objects for that class exceeds need to display some message.

I have done this by keeping the public static method getInstance()and made the constructor as private.
getInstance() method will return the object if and only if the number of objects limit is not exceeded.
I have used one static variable count which will be incremented each time the method is invoked and based on the count object is returned.
Here i have done the mistake, eventhough the count exceeds, i have to check whether the returned objects are still in use or not, i mean they have been garbage collected or not and accordingly i have to return the object.

Can any one tell me is it possible to check whether the objects of the class are garbage collected or not, if collected i have to return objects.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In such scenarios one should use resource pool mechanism.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't find out whether objects have actually been garbage-collected. But you can find out when objects become available for garbage collection. From the programmer's point of view, the distinction is almost always irrelevant.

Look up the java.lang.ref.WeakReference class. Maybe also PhantomReference.
 
Abhishek Reddy
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply..

is there any other way of solving the problem.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek: is there any other way of solving the problem.



You are trying to keep an account of the number of instances created and being used of a particular class. So, you have to do what Peter has told i.e. have a WeakReference and associate a ReferenceQueue with the reference. So that when this object is eligible for garbage collection i.e. it is no more referenced from anywhere, you can decrease the reference count.

Alternatively, you can assume a well behaved user and provide a method done() in your class that is called by the user of the instance when it is done with the use of the instance.
So, getInstance() marks the start of the use of an instance and done() marks the end of use or return to the pool.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic