Thanks very much!

What's your mean:

"which BTW
sets  *INTERRUPTED* flag in the Thread object,
The use of the an instance variable is redundant since the thread already has
a flag designed for that perpose."

I also think this is a feasible way.
Because I think the root for garbage collection are implementation-dependant.
Depending on the implementation and support for native methods,
roots for garbage collection may be contained in:

o All pointers in all stack frames of the stacks of all JVM threads.
  These include pointers in the local variable areas of the frames
  and pointers in the operand stacks of the frames.

o All pointers to static fields of classes in the class and method
  area of the JVM.

o native methods,

o registers of the JVM (for optimization)

From here, I think there is no way for the GC to collect an alive thread although there is no reference  refer to it.
So, by interrupt method here, which force run() method to finish, which is a safe way to make the thread dead.
 
 
 
 
 
 

Christopher Hinds wrote:

In a previous post I made an error in the code example
it should look like the following.
Allowing for the thread acsessing the hashtable extracting the
referenenced Thread object and calling Thread.interupt() , which BTW
sets  *INTERRUPTED* flag in the Thread object,
The use of the an instance variable is redundant since the thread already has
a flag designed for that perpose.
 
 
 

Example :
<<<  some other thread code >>>>

   Thread t    = htbl.get("XXXXX");
   t.interrupt();

>>>>>>>>>>>>>>>>>>>>>>>>>>
 
 

   class xxxxx extends Thread {

       void run() {

           try{
                   while(true){
                       if(interrupted()){
                          // do all your clean up here
                           return;
                       }

                       // do something else here
 

                   }
           }
           catch( InterruptedException e){
                   e.printStactTrace();
                   return;
           }
       }

Cheers
Chris
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Christopher Hinds wrote:

I am assuming you are using jdk1.2 and in that case the stop method has been
depricated. You should use use Thread interrupt to stop the thread.
When you remove the Thread object from the hashtable issue a Thread.interrup()
call so the thread object unwinds correctly and exits the run method then set the obejct reference to null so on the next GC pass the dead thread object will be garbage collected.

   Example:

       class xxxxxx implements Runnable {
           // class code here

           void run() {
                   try {
                           while(true) {
                          // some code here
                          }
                   }
                   catch(InterruptedException e) {
                           e.printStackTrace();
                           return;
                   }
           }
 
 

           }

       }

Cheers
Chris

yangyuex wrote:

Hi

I have several threads in one hashtable.
When I remove one of them, whether this thread still  consume CPU etc
resources?
That's to say, if I will not need one thread, whether I must stop or
destory it explictly or
just remove it from hashtable for garbage collection?
I am not sure how JVM schedules multiple threads.

Thanks very much!
yangyuexiang


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]




Reply via email to