简体   繁体   English

Thread.join(毫秒)在java中不准确吗?

[英]Thread.join(millisecond) is not accuracy in java?

As we all know in java, Thread.join(long millis) means "Waits at most millis milliseconds for this thread to die", but i find the millisecond is not very accuracy, please see the following code: 众所周知,在java中,Thread.join(long millis)的意思是“等待最多毫秒数才能使该线程死亡”,但是我发现毫秒数不是很准确,请参见以下代码:

public class MyThreadTest {
public void invokeTest() {
    long executionTimeLimit = 10;
    Runner rn = new Runner();
    rn.start();
    try {
        long time = System.currentTimeMillis();
        rn.join(executionTimeLimit);
        long l1 = (System.currentTimeMillis() - time);
        System.out.println("execution_time_limit="+executionTimeLimit+" the invoke method time is "+l1+" millisecond");
    } catch (Exception e) {
        //e.printStackTrace();
    }
}
public static void main(String args[]) {
    MyThreadTest mtt = new MyThreadTest();
    mtt.invokeTest();
}
private final class Runner extends Thread {
    public void run()
    {
        //to make the program a bit longer
        int size = 3000;
        String[][] bb = new String[size][size];
        for(int i=0;i<size;i++) 
            for(int j=0;j<size;j++) {
                bb[i][j] = "bbbbbbbbbbb";
        }

    }
}
}

the output log is not same when you running more than one times: 运行多次以上时,输出日志不相同:

"execution_time_limit=10 the invoke method time is 11 millisecond" "execution_time_limit=10 the invoke method time is 32 millisecond" "execution_time_limit=10 the invoke method time is 34 millisecond" “” execution_time_limit = 10调用方法时间为11毫秒“” execution_time_limit = 10调用方法时间为32毫秒“” execution_time_limit = 10调用方法时间为34毫秒“

why does that happen? 为什么会这样?

When you wait(n) or sleep(n) and it is not interrupted or notify()ed, it waits for at least the time given. 当您等待(n)或睡眠(n)且未中断或未通知()时,它将至少等待给定的时间。 In join(n) both is possible. 在join(n)中都可能。 It wakes early on a notify/end of thread, but can wake at any time after that. 它早就线程的通知/结束唤醒,但是可以在以后任何时候醒来。

The variation is between when a thread could wake and when it does wake is so wide there are tools which monitor such things. 线程何时可以唤醒与何时唤醒之间的差异如此之大,因此存在监视此类情况的工具。 jHiccup being one of the best. jHiccup是最好的之一。

When a thread wakes up is dependant onload, but also the OS. 线程唤醒的时间取决于加载,也取决于操作系统。 This can lead to threads waking long after they should. 这可能导致线程在应有的时间后醒来。 A recent question for MacOS concerned delays of up to 10 second delay for a idle system. MacOS最近的一个问题是,闲置系统最多延迟10秒。

This is something Java has no control over, it is down to your OS. 这是Java无法控制的,它取决于您的OS。

BTW if you look at jitter on the micro-second level you see much more colourful variations/jitter/interrupts of processes. 顺便说一句,如果您以微秒为单位查看抖动,您会看到更加丰富多彩的变化/抖动/过程中断。

Micro jitter, busy waiting and binding CPUs 微抖动,繁忙的等待和绑定CPU

When we deal with threads, we can never ensure the time accuracy as these operations like wait, join, sleep are also dependant on system resources' availability. 当我们处理线程时,我们永远无法确保时间准确性,因为这些操作(如等待,连接,睡眠)也取决于系统资源的可用性。

Check Oracle tutorials http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html 查看Oracle教程http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html

The doxygen is just confusing you, because it says "Waits at most millis milliseconds for this thread to die". doxygen只是让您感到困惑,因为它说:“等待最多毫秒,该线程死亡。”

The join() function just allows the calling thread to "wait for a while" rather than stuck forever. join()函数仅允许调用线程“等待一段时间”,而不是永远卡住。 When the join() function returns, the thread you called it on might still be running (ie it didn't "joined"). 当join()函数返回时,您在其上调用的线程可能仍在运行(即未“加入”)。 In this case join() would have waited for at least millis milliseconds, then that thread didn't finish and join() gave up. 在这种情况下,join() 至少要等待几毫秒,然后该线程才结束,join()放弃了。 On the other hand, if the thread join() has been called on did actually finish than join() would have waited for less than millis milliseconds. 另一方面,如果调用了线程join()实际上完成了,则join()等待的时间少于毫秒。

Bear also in mind that unless you're JVM is running on a Real Time Operating System (and those threads have certains priorities), other processes/threads can be scheduled causing join() return to be delayed even longer (which happened when you've got 34ms rather than 11ms). 还请记住,除非您的JVM在实时操作系统上运行(并且那些线程具有一定的优先级),否则可以安排其他进程/线程,从而导致join()返回被延迟更长的时间(在您执行我得到了34毫秒而不是11毫秒)。

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

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