Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在Eclipse和Netbeans中获得了相同java多线程程序的不同输出?_Java_Multithreading_Netbeans - Fatal编程技术网

我在Eclipse和Netbeans中获得了相同java多线程程序的不同输出?

我在Eclipse和Netbeans中获得了相同java多线程程序的不同输出?,java,multithreading,netbeans,Java,Multithreading,Netbeans,我在eclipse和Netbeans ide中得到了这个多线程代码的不同输出,我不知道它是如何产生的,这背后的逻辑是什么 `System.out.println("r1");` `` try `` { Thread.sleep(500); } catch(Exception e) { } System.out.println("r2");

我在eclipse和Netbeans ide中得到了这个多线程代码的不同输出,我不知道它是如何产生的,这背后的逻辑是什么

`System.out.println("r1");`         ``   
    try    ``
    {    
        Thread.sleep(500);    
    }    
    catch(Exception e)    
    {    

    }    
    System.out.println("r2"); 



}    
public static void main(String args[])    
{    
    MyThread2 t1=new MyThread2();    
    MyThread2 t2=new MyThread2();    
    t1.start();    
    t2.start();    
    System.out.println(t1.isAlive());    
    System.out.println(t2.isAlive());    

}    

}    
每次我执行这个代码时,它都会显示不同的输出,请帮助我

`System.out.println("r1");`         ``   
    try    ``
    {    
        Thread.sleep(500);    
    }    
    catch(Exception e)    
    {    

    }    
    System.out.println("r2"); 



}    
public static void main(String args[])    
{    
    MyThread2 t1=new MyThread2();    
    MyThread2 t2=new MyThread2();    
    t1.start();    
    t2.start();    
    System.out.println(t1.isAlive());    
    System.out.println(t2.isAlive());    

}    

}    
公共类MyThread2扩展线程
{

public void run()
`System.out.println("r1");`         ``   
    try    ``
    {    
        Thread.sleep(500);    
    }    
    catch(Exception e)    
    {    

    }    
    System.out.println("r2"); 



}    
public static void main(String args[])    
{    
    MyThread2 t1=new MyThread2();    
    MyThread2 t2=new MyThread2();    
    t1.start();    
    t2.start();    
    System.out.println(t1.isAlive());    
    System.out.println(t2.isAlive());    

}    

}    
{

`System.out.println("r1");`         ``   
    try    ``
    {    
        Thread.sleep(500);    
    }    
    catch(Exception e)    
    {    

    }    
    System.out.println("r2"); 



}    
public static void main(String args[])    
{    
    MyThread2 t1=new MyThread2();    
    MyThread2 t2=new MyThread2();    
    t1.start();    
    t2.start();    
    System.out.println(t1.isAlive());    
    System.out.println(t2.isAlive());    

}    

}    
Eclipse中的输出:
r1
r1
正确
正确
r2
r2

`System.out.println("r1");`         ``   
    try    ``
    {    
        Thread.sleep(500);    
    }    
    catch(Exception e)    
    {    

    }    
    System.out.println("r2"); 



}    
public static void main(String args[])    
{    
    MyThread2 t1=new MyThread2();    
    MyThread2 t2=new MyThread2();    
    t1.start();    
    t2.start();    
    System.out.println(t1.isAlive());    
    System.out.println(t2.isAlive());    

}    

}    
Netbeans中的数据和输出:
r1
正确
正确
r1
r2

r2线程是由JVM调度的,没有特定的顺序。这就是为什么对于涉及线程的相同代码,您会看到不同的输出

`System.out.println("r1");`         ``   
    try    ``
    {    
        Thread.sleep(500);    
    }    
    catch(Exception e)    
    {    

    }    
    System.out.println("r2"); 



}    
public static void main(String args[])    
{    
    MyThread2 t1=new MyThread2();    
    MyThread2 t2=new MyThread2();    
    t1.start();    
    t2.start();    
    System.out.println(t1.isAlive());    
    System.out.println(t2.isAlive());    

}    

}    

JVM使用内部操作系统底层线程管理来管理线程,在几乎所有可用的操作系统中,线程调度的算法都是循环调度。

线程由
JVM
JVM
选择以最高优先级运行可运行线程。每当创建新Java线程时,它都具有相同的优先级y作为创建它的线程。因此,在您的情况下,两个线程具有相同的优先级!因此,JVM使用自己的算法(循环调度)拾取线程,并按该顺序选择和执行它们。它没有固定的顺序,而且执行率完全不可预测

`System.out.println("r1");`         ``   
    try    ``
    {    
        Thread.sleep(500);    
    }    
    catch(Exception e)    
    {    

    }    
    System.out.println("r2"); 



}    
public static void main(String args[])    
{    
    MyThread2 t1=new MyThread2();    
    MyThread2 t2=new MyThread2();    
    t1.start();    
    t2.start();    
    System.out.println(t1.isAlive());    
    System.out.println(t2.isAlive());    

}    

}    

对于同一IDE上的不同示例运行,它将为您提供不同的输出,即
Netbeans
Eclipse
。您可以尝试同时运行这两种运行方式!这可能与前一次运行的顺序不符,因为它们是由JVM安排的。

线程不是确定性的,特别是在涉及竞争条件的情况下。这并不奇怪,n或者在我看来这是一个问题。即使在Eclipse中运行该程序几次。即使在Eclipse中,您也可能看到不同的结果。对方法或变量的线程访问由JVM决定。使用synchronized使单线程一次访问一个方法。@manish,是的,我看到了,但它是如何发生的,是否有任何流的顺序。
`System.out.println("r1");`         ``   
    try    ``
    {    
        Thread.sleep(500);    
    }    
    catch(Exception e)    
    {    

    }    
    System.out.println("r2"); 



}    
public static void main(String args[])    
{    
    MyThread2 t1=new MyThread2();    
    MyThread2 t2=new MyThread2();    
    t1.start();    
    t2.start();    
    System.out.println(t1.isAlive());    
    System.out.println(t2.isAlive());    

}    

}