为什么下面的代码一次又一次地编译时却没有给出相同的输出? [英] Why the following code does not give the same output when we compile it again and again?

查看:73
本文介绍了为什么下面的代码一次又一次地编译时却没有给出相同的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的代码一次又一次地编译时却没有给出相同的输出?

Why the following code does not give the same output when we compile it again and again?

class A implements Runnable
{
    Thread t;
    String s;
    A(String st)
    {
        s=st;
        t=new Thread(this,s);
        System.out.println(s+" "+t);
        t.start();
    }
    
    public void run()
    {
        try
        {
            for(int i=5;i>0;i--)
            {
                System.out.println(i);
                Thread.sleep(4000);
            }
        }
        catch(InterruptedException e)
        {
            System.out.println(e);
        }
        System.out.println("In Run()");
    }
}
public class demo14 {
    public static void main(String args[])
    {
        A t1=new A("one");
        A t2=new A("two");
        A o3=new A("three");
        System.out.println("In main");
        
    }
}

推荐答案

所有线程都具有相同的优先级,因此取决于它们在再次运行时的系统.不能保证实际的线程执行顺序,它受您控制范围之外的各种因素的影响!这里有一个描述: http://journals.ecs.soton.ac. uk/java/tutorial/java/threads/priority.html [ ^ ]
All the thread have the same priority, so it is up to the system when they run again. The actual order of thread execution is not guaranteed and is subject to allsorts of factors outside your control! There is a description here: http://journals.ecs.soton.ac.uk/java/tutorial/java/threads/priority.html[^]


这篇关于为什么下面的代码一次又一次地编译时却没有给出相同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆