Link to home
Start Free TrialLog in
Avatar of hawkly
hawkly

asked on

Object / Thread FAQ

My program as follow:
mainProgram.java
newThread= new myThread("Testing");

In the myThread.java :
public class myThread implements Runnable {

My question is if i write the newThread = null will it kill the object or thread?
E.g.
newThread= new myThread("Testing");
newThread = null ;

thanks
Avatar of alikoank
alikoank

No it will not kill a running thread.
Avatar of Mick Barry
> will it kill the object or thread?

neither
An object is never deleted by "killing" a reference to it.
An object is deleted by the garbage collector at a later time.
And, surely a running thread is referenced from various other places!

;JOOP!
Avatar of hawkly

ASKER

then how can i kill the thread when it waiting for an IO?
> And, surely a running thread is referenced from various other places!

The thread above is not running, and so would be available for gc.
So the object *could* get freed at a later time.
> then how can i kill the thread when it waiting for an IO?

you interrupt it (if the blocking is interruptable.
otherwise one solution is to close the stream the thread is being blocked on
Generally speaking you should not kill a thread. Make your thread check for a variable or event periodically and when you want your thread to terminate  set this variable / signal an event, so your thread can end its execution gracefully.
Avatar of hawkly

ASKER

The IO is:
in = httpConnector.openInputStream();

How can i interrupt it?

thanks
you can try interrupting the thread but i suspoct you may have to close the connection.
Avatar of hawkly

ASKER

but my problem is how can i close the connection?
httpConnector.close();
Avatar of hawkly

ASKER

The program is stop at:
in = httpConnector.openInputStream();

so how is possible i trigger the httpConnector.close(); ?

thanks
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial