简体   繁体   English

Java中的线程执行时间

[英]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. 我在其中调用方法的java中有一个线程。我为每个线程存储一个映射,以便可以杀死它。 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. 问题是当我调用我的stop方法时,t.isAlive()为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. 线程在run()方法返回后也会死掉。 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. sleep语句添加到run()方法中,并在其之前和之后添加print语句,以确保线程的当前状态。

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. Thread.interrupt不会杀死线程,只是在线程暂停时恢复它,通常,手动杀死线程不是一个好主意。 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. 调用start()方法后,线程处于可运行状态,但是线程调度程序未将其选择为正在运行的线程。

So, invocation of start() method does not necessarily guarantee the execution of thread. 因此, start()方法的调用不一定保证线程的执行。 It depends on how jvm and Opetating System handles threads. 这取决于jvm和Opetating System如何处理线程。 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. 在启动executeMethod方法之前,线程可能会保持runnable状态一段时间,或者在调用start()可能立即启动executeMethod ,但是不能保证这两种行为都可以保证。 This is what javadoc says: 这是javadoc所说的:

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. 当在某个线程中运行的代码创建新的Thread对象时,新线程的优先级最初设置为与创建线程的优先级相等,并且当且仅当创建线程是守护程序时,该线程才是守护程序线程。

So, you should not expect alive time of the thread to be execution time of the method. 因此,您不应期望线程的活动时间就是方法的执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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