无法理解Threads-Runnable Interface的工作原理

发布于 2024-11-01 02:47:11 字数 478 浏览 1 评论 0原文

public class Test {
    private static void funct(final int i) {
        new Thread(new Runnable() {
            public void run() {System.out.println(i);}
         }).start(); 
    }
    public static void main(String[] args) {
        System.out.println(1);
        funct (2);
        System.out.println(3);
        funct (4);
        System.out.println(5);
    }
}

每次运行它时,我都会得到以下解决方案之一。为什么会这样呢? 1 3 5 4 2

1 3 5 2 4

1 3 2 5 4

public class Test {
    private static void funct(final int i) {
        new Thread(new Runnable() {
            public void run() {System.out.println(i);}
         }).start(); 
    }
    public static void main(String[] args) {
        System.out.println(1);
        funct (2);
        System.out.println(3);
        funct (4);
        System.out.println(5);
    }
}

Every time I run it, I am getting one of the below solutions. Why so?
1
3
5
4
2

1
3
5
2
4

1
3
2
5
4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑咖 2024-11-08 02:47:12

这是因为您无法控制首先生成哪个线程。这是由 JVM 决定的。只有“1,3,5”是有序的,因为它们是从 main 执行的。

这个问题可能是相关的:-

  1. 在特定时间运行java线程

This is because you have no control over which thread is spawning first. It's decided by the JVM. Only "1,3,5" will be in order since they are executed from main.

This question may be relevant:-

  1. Run java thread at specific times
迎风吟唱 2024-11-08 02:47:12

因为您的主线程有时会击败其他两个线程。其他时候则不然。

你有三个线程;无法保证哪个将被安排以任何特定顺序运行。

Because your main thread beats the other two threads some of the time. Other times not.

You've got three threads; there's no guarantee which is going to get scheduled to run in any particular order.

囚我心虐我身 2024-11-08 02:47:12

一般来说,线程和异步性是复杂的主题,但不足之处在于,在您的情况下,线程需要一点时间才能旋转,因此它们与其他打印语句交错,具体取决于 JVM 的处理器时间类型(并且,在反过来,操作系统)决定分配给这些线程。

如果您想获得真正扎实的知识,我强烈推荐 Brian Goetz 等人所著的 Java Concurrency in Practice 这本书处理正在发生的事情。

Threading and asynchronicity in general are complex topics, but the short of it is that in your case, the threads take a bit to spin up, so they interleave with the other print statements depending on what kind of processor time the JVM (and, in turn, the OS) decides to allocate to those threads.

I highly suggest the book Java Concurrency in Practice, by Brian Goetz and others, if you want to get a really solid handle on what's going on.

小耗子 2024-11-08 02:47:11

在此示例中,打印数字的顺序是不确定的。您唯一确定的是 1、3 和 5 会按该顺序出现。但在这种安排中,2 和 4 会出现在哪里尚不清楚。原因是您有 3 个线程打印出以下数字系列:(1, 3, 5); (2)和(4)。这三个线程将由 JVM 进行调度,但它认为这是最好的。

多线程编程是一个复杂的主题,由于您似乎刚刚开始深入研究它,因此我建议您阅读 Oracle Java 教程的并发部分:http://download.oracle.com/javase/tutorial/essential/concurrency/index.html

The order at which the numbers will be printed out is indeterminate in this example. The only thing you know for sure is that 1, 3, and 5 will appear in that order. But where in this arrangement 2 and 4 will come is unknown. The reason for this is you have 3 threads printing out the following number series: (1, 3, 5); (2), and (4). The three threads will be scheduled by the JVM however it determines would be best.

Multithreaded programming is a complex topic, and since it looks like you are just starting to dive into it I would recommend the Concurrency portion of Oracle's Java tutorial: http://download.oracle.com/javase/tutorial/essential/concurrency/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文