为什么线程执行会提供不同的输出? [英] Why Thread execution is giving different output?

查看:114
本文介绍了为什么线程执行会提供不同的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在线程(Java)中测试/学习一些基本的东西,并遇到了一个简单但令人困惑的输出。以下是我的课程

I was trying to test/learn a few basic things in threading(Java) and came across a simple but confusing output. Following are my classes

public class CopyMaster implements Runnable{

    @Override
    public void run() {
        System.out.println("Run called from Copy Master for thread "+Thread.currentThread().getName());
    }

}

我要调用的主类线程

public class Main {

    public static void main(String[] args) {
        Thread[] threadArray = new Thread[4];
        for(int i=0; i<threadArray.length; i++){
            threadArray[i] = new Thread(new CopyMaster());
        }
        for(Thread t : threadArray){
            //[Line of intrest]System.out.println("Starting Thread "+t.getName());
            t.start();
        }
    }
}

OP(带感兴趣的行未注释)

Starting Thread Thread-0
Starting Thread Thread-1
Run called from Copy Master for thread Thread-0
Starting Thread Thread-2
Run called from Copy Master for thread Thread-1
Starting Thread Thread-3
Run called from Copy Master for thread Thread-2
Run called from Copy Master for thread Thread-3

OP(感兴趣的线条评论)

Run called from Copy Master for thread Thread-2
Run called from Copy Master for thread Thread-3
Run called from Copy Master for thread Thread-0
Run called from Copy Master for thread Thread-1

我已尝试多次运行代码并观察到打印输出的顺序在感兴趣的注释行中是随机的,并且在评论一个适当的(在序列)...

I have tried running the code multiple times and observed that the sequence of print-outs are random in commented line of interest and in un-commented one its proper(in sequence)...

为什么它会像那样?// EDITED因为这会让我感到困惑

Why is it behaving like that?//EDITED as this confuses what i want to mean

编辑:

我理解线程的行为是不可预测的,但我的主要问题是为什么它在一个案例中保持oredr总是以及为什么在其他情况下存在随机行为?

I understand that threads behaves unpredictably but my main question is why it maintains oredr ALWAYS in one case and why random behavior is there in other case?

推荐答案

这正是Java中多线程的行为,不可预测的

This is precisely the behaviour of multithreading in Java, unpredictable.

for(Thread t : threadArray){
 //[Line of intrest]System.out.println("Starting Thread "+t.getName()); 

上面一行在主线程中运行 - >所以输出总是按顺序排列(尽管其他输出行可以介于两者之间)

the above line runs in main thread --> so output is always in order (though other output lines can come in between)

t.start(); 

以上行启动每个线程(准备执行线程,但可能不会立即调用run方法) )。没人能预测哪个线程的 run()将首先执行。它完全留给JVM和底层操作系统。
}

The above line starts each thread (prepares thread for execution, but the run method might not be called immediately). Nobody can predict which thread's run() will execute first. It is totally left to the JVM and underlying OS. }

如果您希望输出订购,请使用 join()等待其他线程终止。

If you want the output to be ordered, use join() to wait until other thread terminates.

编辑:

OP(利息线未 - 评论)

OP(With Line of Interest un-commented)

Starting Thread Thread-0    --> executed in main thread --> 0--> in order
Starting Thread Thread-1   --> main thread --> 1 --> in order
Run called from Copy Master for thread Thread-0 --> executed in thread-0.
Starting Thread Thread-2    --> executed in main. --> 2 -> in order
Run called from Copy Master for thread Thread-1 --> executed in thread-1
Starting Thread Thread-3 --> main --> 3 --> in order
Run called from Copy Master for thread Thread-2 -->executed in thread-2.
Run called from Copy Master for thread Thread-3 --> executed in thread-3.

注意: 启动Thread Thread-0 打印在线程中。因此,可能甚至没有执行下一行 thread.start()。日志误导

Note : Starting Thread Thread-0 is printed in the main thread. So, the next line thread.start() might not even have been executed. The logs are misleading.

以下几行实际上表明了线程是如何运行的。这些日志没有误导性。

The below lines actually suggest how threads ran. These logs aren't misleading.

OP(有兴趣点评论)

OP(With Line of Interest commented)

Run called from Copy Master for thread Thread-2
Run called from Copy Master for thread Thread-3
Run called from Copy Master for thread Thread-0
Run called from Copy Master for thread Thread-1

这篇关于为什么线程执行会提供不同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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