?
Solved

instantiated objects of one class

Posted on 2000-01-07
6
Medium Priority
?
153 Views
Last Modified: 2013-11-23
Hello,
At runtime, is there a way to get the references of all objects that are instances of one given class?
Tks
st5
0
Comment
Question by:st5
  • 3
  • 3
6 Comments
 

Expert Comment

by:Sasha_M
ID: 2332139
Well, the Class class doesn't define such a method, so I would think there isn't one. You can have a static Vector for your class and add each instance to it on construction and remove it on destruction (finalize()).
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2334347
finalize() is not always guaranteed to be called,for threads at least.

Thread t=new Thread();
Thread [] currentThreads= new Thread [t.activeCount()];
t.enumerate(currentThreads);

there maybe a method to do it
0
 

Expert Comment

by:Sasha_M
ID: 2334418
A. AFAIK finalize() is guaranteed to be called, just the exact time is not guaranteed to be anything.
B. >> At runtime, is there a way to get the references of all
>> objects that are instances of one given class?
What's up with the thread count? The question was about instances of a class.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 5

Accepted Solution

by:
mbormann earned 150 total points
ID: 2334431
Sasha_M,

(A) AFAIK this is not the same even if all conditions are met in ALL versions of Java.

read this from Bruce Eckel's 'Thinking in Java'

When you run the program, you provide a command-line argument of “before” or “after.” The “before” argument will call the System.gc( ) method (to force execution of the garbage collector) along with the System.runFinalization( ) method to run the finalizers. These methods were available in Java 1.0, but the runFinalizersOnExit( ) method that is invoked by using the “after” argument is available only in Java 1.1[19] and beyond. (Note you can call this method any time during program execution, and the execution of the finalizers is independent of whether the garbage collector runs).

The preceding program shows that, in Java 1.1, the promise that finalizers will always be run holds true, but only if you explicitly force it to happen yourself. If you use an argument that isn’t “before” or “after” (such as “none”), then neither finalization process will occur, and you’ll get an output like this:

Created 47
Beginning to finalize after 8694 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 9834, total finalized = 108
bye!

Thus, not all finalizers get called by the time the program completes. [20] To force finalization to happen, you can call System.gc( ) followed by System.runFinalization( ). This will destroy all the objects that are no longer in use up to that point. The odd thing about this is that you call gc( ) before you call runFinalization( ), which seems to contradict the Sun documentation, which claims that finalizers are run first, and then the storage is released. However, if you call runFinalization( ) first, and then gc( ), the finalizers will not be executed.

One reason that Java 1.1 might default to skipping finalization for all objects is because it seems to be expensive. When you use either of the approaches that force garbage collection you might notice longer delays than you would without the extra finalization.


--------------------------------------------------------------------------------

[19] Unfortunately, the implementations of the garbage collector in Java 1.0 would never call finalize( ) correctly. As a result, finalize( ) methods that were essential (such as those to close a file) often didn’t get called. The documentation claimed that all finalizers would be called at the exit of a program, even if the garbage collector hadn’t been run on those objects by the time the program terminated. This wasn’t true, so as a result you couldn’t reliably expect finalize( ) to be called for all objects. Effectively, finalize( ) was useless in Java 1.0.

[20] By the time you read this, some Java Virtual Machines may show different behavior.


(B) as for B I admit that the comment is a bt misleading ,what I wanted to say was this exactly(God what a difference a simple comma makes!)

finalize() is not always guaranteed to be called.

for threads at least.

            Thread t=new Thread();
            Thread [] currentThreads= new Thread [t.activeCount()];
            t.enumerate(currentThreads);

            there maybe a method to do it

but  I dont know ,probably(note the comma now :-) ) you will have to dig deep into JVM related stuff,I do hope some enlightened soul would throw some light into this dark tunnel of ignorance.

to get back to ur question

public static int enumerate(Thread tarray[])

Copies into the specified array every active thread in this thread group and its subgroups.
This method simply calls the enumerate method of this thread's thread group with the array argument.


Cheers .....
0
 

Expert Comment

by:Sasha_M
ID: 2334459
Ok, I see what you mean now :-)
But I think that will only give you the active (running) Threads, not all instances of class Thread.
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2334482
>>>>>At runtime, is there a way to get the references of all objects that are instances of one given class?

Yes Sasha_M I dont know how to get all other objects ,but I think for all threads at least you can keep track of thru this method.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month4 days, 22 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question