简体   繁体   English

java multithreading,子线程运行时间的差异

[英]java multithreading , Difference in child thread running time

i ran a code 10 times on windows cmd line and every time i got same output 我在windows cmd line上运行了10 times代码, every time得到same output

在此处输入图片说明

i ran the same code on ideone ,10 times and each time i got same output as 我跑的same codeideone ,10 times ,每次我得到same output作为

在此处输入图片说明

My questions are : 我的问题是:

  • according to my code child thread must sleep for 500ms and main thread for 1000ms so i think that after main thread runs once , child thread runs twice but in cmd output , child ran 3 times once and 1 time other time. 根据我的典子线程必须睡500ms和主线程1000ms ,所以我认为,主线程运行后once ,孩子线程运行twice但在cmd output ,孩子跑到3 times一次1 time其他时间。 why? 为什么?

  • output is different on ideone and cmd ? ideonecmd输出是否不同?

here is my code 这是我的代码

class newthread extends Thread {

    newthread() {

    super("demo thread");

    System.out.println("child thread:"+this);

    start();}

    public void run(){

    try{

    for(int i=5;i>0;i--)  {

        System.out.println("child thread:"+i);

        Thread.sleep(500); }

       }

   catch(InterruptedException e){

        System.out.println("child thread interrupted");}

        System.out.println("child thread exiting");

       }

}

    class exthr{

       public static void main(String args[]){

         new newthread();

         try{

         for(int i=5;i>0;i--)    {

         System.out.println("main thread:"+i);

         Thread.sleep(1000);     }

            }

         catch(InterruptedException e){

         System.out.println("main thread interrupted ");     }

       System.out.println("main thread exiting");

       }

       } 

There are several reasons of such a difference: 造成这种差异的原因有很多:

  • It's system scheduler who gives CPU time to the threads and it's free to organize it as he likes. 它是系统调度程序,它为线程分配CPU时间,并且可以随意组织它。 You cannot force it to give CPU to your thread with exact precision. 您不能强制它以精确的精度将CPU提供给线程。
  • When doing synchronization between threads you should not rely on sleep . 在线程之间进行同步时,您不应该依赖sleep This basically deactivates thread for a while, but there are no guarantees that it will awake immediately. 这基本上会禁用线程一段时间,但不能保证它会立即唤醒。 If you want particular order you should use synchronization primitives like mutexes and condition variables. 如果要特定顺序,则应使用同步基元,例如互斥量和条件变量。
  • Most likely you have different hardware on your machine and on ideone server. 您的机器和ideone服务器上最有可能具有不同的硬件。 Moreover ideone is most likely very loaded with other things, so fluctuation is bigger. 此外,二甲酮极可能富含其他物质,因此波动较大。

So main sleeps for 1000 ms., and child sleeps twice for 500 ms. 因此,主体睡眠1000毫秒,孩子睡眠两次500毫秒。 They should thus write to System.out approximately at the same time. 因此,他们必须写信给System.out 大约在同一时间。

Sometimes the main thread comes before, and sometimes after. 有时主线程在之前,有时在之后。 That depends on the precision of the clock, on the other threads running, on the choices of the thread scheduler, on whether the threads sleep exactly for the time they were told to sleep or a bit more, on the time elapsed between the main thread starting the child thread and the time the thread actually starts running, etc. 这取决于时钟的精度,其他正在运行的线程,线程调度程序的选择,线程是否在被告知其休眠的时间准确休眠还是在主线程之间经过的时间更长一点启动子线程以及线程实际开始运行的时间,等等。

You can't expect a strict sequence of events unless you correctly synchronize the threads to make them wait for each other. 您不能期望严格的事件顺序,除非您正确同步线程以使它们彼此等待。

You seem to assume that your program will resume immediately after timer runs out. 您似乎假设您的程序将在计时器用完后立即恢复。 This is not neccessarily the case on any machine, unless you are on a realtime system with control of all processes and priorities. 除非您在具有对所有进程和优先级进行控制的实时系统上,否则在任何计算机上都不一定是这种情况。 It would be likely, though, that on a machine with moderate load, the behaviour would be like the behaviour exhibited by ideone. 但是,在负载适中的机器上,这种行为很可能会像ideone所表现出的行为一样。 Admittedly, it is odd that your windows machine generates the same output allways, but it does not change the fact that the only guarantee you have is that your thread will sleep for AT LEAST the time you specify, not AT MOST. 诚然,您的Windows计算机始终生成相同的输出是很奇怪的,但是它并没有改变这样一个事实,即您唯一保证的是线程将在指定的时间(而不是MOST)至少休眠。 You could include a timestamp in your output to verify this, if you want to. 如果需要,您可以在输出中包含时间戳以进行验证。

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

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