简体   繁体   English

为什么在下面的代码中,当我多次编译或运行它时,输出是不同的

[英]Why in the following code the output is different when I compile or run it more than once

class Name implements Runnable {
    public void run() {
        for (int x = 1; x <= 3; x++) {
            System.out.println("Run by "
                               + Thread.currentThread().getName()
                               + ", x is " + x);
        }
    }
}
public class Threadtest {
    public static void main(String [] args) {
        // Make one Runnable
        Name nr = new Name();
        Thread one = new Thread(nr);
        Thread two = new Thread(nr);
        Thread three = new Thread(nr);
        one.setName("A");
        two.setName("B");
        three.setName("C");
        one.start();
        two.start();
        three.start();
    }
}

The answer is different while compiling and running more then one time I don't know why? 答案是不同的,编译和运行超过一次我不知道为什么? any idea. 任何想法。

It should show the same output, but potentially in a different order for each run. 它应该显示相同的输出,但每次运行可能会以不同的顺序显示。

You've got three independent threads: each will show three lines of output, in the obvious order - but there's no guarantee which thread will execute first, etc. In this particular case I don't believe you've got any side effects which would cause truly strange behaviour - just the normal ambiguity of which threads will run when. 你有三个独立的线程:每个都会以明显的顺序显示三行输出 - 但是不能保证哪个线程会先执行,等等。在这种特殊情况下我不相信你有任何副作用会导致真正奇怪的行为 - 只是线程运行时的正常歧义。 Note that on a multicore processor the threads are likely to be running simultaneously - the only synchronization is whatever happens within System.out.println . 请注意,在多核处理器上,线程可能同时运行 - 唯一的同步是System.out.println发生的任何事情。

one of them will start running first then the second and lastly the third . 其中一个将首先开始运行,然后是第二个,最后是第三个。 that is depend on thread scheduler on system 这取决于系统上的线程调度程序

暂无
暂无

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

相关问题 与手动编译和运行IDE相比,为什么手动编译和运行代码时得到不同的输出 - Why am I getting different output when I manually compile and run the code as compared to automatic compiling and running by IDE 这段代码的问题在于输出。 生产者生产不止一次。 为什么以及如何解决? - The problem of this code is in the output. Producer produce more than once. Why and how can I solve it? 当我运行以下代码时,它引发异常,为什么? - When I run the following code, it throws an Exception, why? 我的代码会编译,但是当我运行它时,它有一个错误。 为什么? - My code will compile but when I run it, it has an error. Why? 我想了解为什么在尝试编译并运行以下程序时会出现此警告, - I want to understand why this warning is coming along when i try to compile and run the following program, 为什么我不能多次运行这个简单的服务器-客户端应用程序的客户端? - Why can't i run the client side of this simple server-client app more than once? 为什么递归调用下的代码被处理多次(当基本情况为真时)? - Why is code beneath recursive call processed more than once (when the base case is true)? Makefile运行多次 - Makefile run more than once 为什么我的代码不会随机生成多次? - Why isn't my code randomly generating more than once? Enum *似乎*被多次初始化,构造函数被多次调用。 如果我是对的,那为什么? - Enum *seems* to be initialized more than once, the constructor is called more than once. If I’m right, then why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM