Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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
0
hawkly
Asked:
hawkly
  • 7
  • 4
  • 2
  • +1
1 Solution
 
alikoankCommented:
No it will not kill a running thread.
0
 
objectsCommented:
> will it kill the object or thread?

neither
0
 
sciuriwareCommented:
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!
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
hawklyAuthor Commented:
then how can i kill the thread when it waiting for an IO?
0
 
objectsCommented:
> 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.
0
 
objectsCommented:
> 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
0
 
alikoankCommented:
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.
0
 
hawklyAuthor Commented:
The IO is:
in = httpConnector.openInputStream();

How can i interrupt it?

thanks
0
 
objectsCommented:
you can try interrupting the thread but i suspoct you may have to close the connection.
0
 
hawklyAuthor Commented:
but my problem is how can i close the connection?
0
 
objectsCommented:
httpConnector.close();
0
 
hawklyAuthor Commented:
The program is stop at:
in = httpConnector.openInputStream();

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

thanks
0
 
objectsCommented:
> so how is possible i trigger the httpConnector.close(); ?

from a different thread.
ie. the thread that you want to stop the blocked thread from.
0
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now