简体   繁体   中英

Thread time of execution in java

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 ?

Threads also die after they run() method returns. In such case the thread will not be alive. Add the sleep statement into run() method, and add print statements before and after it to be sure about the current state of the thread.

Thread.interrupt does not kill the thread, just resumes it if its paused, and in general it is not a good idea to kill threads manually. Read this question for to understand why and what to do instead.

    As per Thread Class java doc
    https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

Thread.getState() will return you state of the thread
which you can check if thread is still running and then can kill it.

Thread.State = getState() Returns the state of this thread.

A thread state. A thread can be in one of the following states:
NEW
A thread that has not yet started is in this state.
RUNNABLE
A thread executing in the Java virtual machine is in this state.
BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.
WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED
A thread that has exited is in this state.

Execution time of the method and alive time of the thread will be different. Here 's what thread lifecycle says:

The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

So, invocation of start() method does not necessarily guarantee the execution of thread. It depends on how jvm and Opetating System handles threads. A thread might stay in runnable state for some time, before starting executeMethod method or it might start executeMethod as soon as start() is called, but either of these behavious is not guaranteed. This is what javadoc says:

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

So, you should not expect alive time of the thread to be execution time of the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM