Reputation: 309
i create three threads in my main program. i have for loop in each thread. After executing the statements in the run() method , each thread automatically gets destroyed or killed by itself. correct? Is my understanding correct ?
IS there any Java standard reference where it mentions that there is no need to explicitly kill a thread and it does by itself. i have been trying to read and browse many articles. but still not getting 100% confidence.
I would highly appreciate if any expert over here could reply and help me out. Thanks in advance!!!
public class Demo {
TestA A = new TestA("TestA",threadAList);
TestA B = new TestB("TestB",threadBList);
TestA C = new TestC("TestC",threadCList);
}
class TestA implements Runnable {
Thread t;
public TestA(String name,List threadAList) {
System.out.println(name);
this.threadAList = threadAList;
t = new Thread(this);
t.start();
}
public void run() {
try {
System.out.println("TestA Thread started");
}
catch(Exception e) {
e.printStackTrace(log);
doing some action to move the faild file to a failure folder
}
finally {
log.close();
}
}
}
Upvotes: 2
Views: 3819
Reputation: 7706
Here is an instructional example to demonstrate a thread is removed from existence without overt action.
The output looks like:
I'm dying.
I've been burried.
class Test {
static boolean flag = true;
public static void main(String[] args) throws Exception {
class T extends Thread {
public T(Runnable r) {
super(r);
}
protected void finalize() {
System.out.println("I've been burried.");
flag=false;
}
}
T t = new T(new Runnable(){
public void run() {
System.out.println("I'm dying.");
}});
t.start();
t=null;
while(flag) {System.gc(); Thread.sleep(13);};
}
}
Upvotes: 0
Reputation: 36562
Yes, the thread is automatically destroyed and made available for garbage collection once its Runnable
's run
method has returned.
Upvotes: 5
Reputation: 38857
When your thread exits the run() method it dies and will be garbage collected. There is no need to explicitly kill it. In fact you should never "kill" or "stop" threads. Threads must exit gracefully through finishing the run method or dying because an uncaught exception was thrown. Make sure you understand what conditions an InterruptedException can be thrown and understand how to properly respond to it.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html
public void run() {
try {
while( !Thread.currentThread().isInterrupted() ) {
try {
// do some long running work here
} catch( ExceptionThatDoesntStopTheThread ex ) {
logger.warn( "Hey I got an exception, but I'm not going anywhere", ex );
}
}
// if you are forced to catch InterruptedException (wait(), sleep(), etc)
// put it outside the loop as it signifies someone has asked this thread to
// shutdown.
} catch( InterruptedException ex ) {
logger.info("Interrupted exception received shutting down.");
} finally {
// clean up anything you need to handle, and log a statement
logger.info("Thread done.");
}
}
If a client wants to request a thread to shutdown it just needs to call interrupt() on the thread instance:
Thread someThread = new Thread( new MyRunnable() );
...
someThread.interrupt();
Upvotes: 0
Reputation: 118754
Well behaved threads are not killed, they die on their own accord. How they die, or how they are signaled to die are not part of Java per se, but an artifact of the application.
Killing of threads in Java is frowned upon because of the severity of process. Much like an OutOfMemory exception, many classes are simply not designed to have the rug yanked out from underneath them. Many check for explicitly checked exceptions, or for other "expected" events within their own code, that they themselves control, but few handled the truly out of the blue events that can happen in the JVM (such as OOM and thread death). For example, if a class it performing a static initializer "that only happens once", and that initializer is interrupted, that class is "ruined forever".
This is why killing a thread is supported, but not recommended.
Upvotes: 0
Reputation: 1998
threads expire automatically when they finish execution. it is however ususally good practice to provide a point in your code (i.e. at shutdown) where you ensure that threads have finished (or wait for them to finsih) this will also let you determain if any of the threads has become stuck in an infinite loop.
Upvotes: 0