java - when will finalize() be called on my class instance in this scenario? -


i know finalize() called whenever class instance collected garbage collector. however, little bit confused when passing instance of class thread via queue.

let's skeleton of thread1:

for(i=0; i<1000; i++) {    packet pkt = new packet();  // instance of class    pkt.id = i;    thread2.queue.put(pkt); } 

then, thread 2 remove packet queue , perform lengthy operations. second thread "copy" of packet, or form of reference? importance that, if copy, finalize() on instance created in thread 1 can called before thread 2 done packet. if reference, guaranteed finalize() called once information in packet.

this basic example may not show importance, storing c-pointer (from jni) in packet destroy memory when done object. if passed copy, memory may destroyed before second thread done it. if passed reference, should destroyed once gc sees no longer in use both threads (my desired behavior). if latter scenario not guaranteed, not use finalize() , use else more complex.

the second thread receives same actual object instance. you're safe premature finalization.

it receives copy of object reference, if want think of way.

in addition, finalize not run when garbage collector finds object has become garbage - vm free run @ later time, , reclaim memory time after that. can't rely on when finalize run. however, since care knowing finalize won't called before second thread finishes object, that's immaterial. worth knowing!


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -