I have a Thread in java in which method is called..I am storing a map for each thread so that i can kill it. My sample code is:
Thread thread = new Thread( new Runnable() {
public void run() {
executeMethod();
}
} );
thread.start();
thread.setName("some Name");
//Create Map to save each method call as thread
threadMap.put( "some Name", thread );
Now I want to stop the method calling by killing the Thread so i have something like :
public static void stop( String threadName ) {
if ( StringUtils.isNotEmpty( threadName ) ) {
Thread t = threadMap.get( threadName );
if ( t != null && t.isAlive() ) {
System.out.println( "Going to kill the thread:" + threadName );
t.interrupt();
System.out.println( "killed!!" );
} else {
System.out.println( "THREAD is null" );
}
}
}
The issue is when i called my stop method, t.isAlive() is false. I assume that the execution time of method will be the alive time of thread..Am i Correct or i am misunderstanding it ?