简体   繁体   English

多线程力学?

[英]Multi-Threading Mechanics?

I've been experimenting with multithreading and I'm really confused about a test I did. 我一直在尝试多线程,而我对所做的测试感到非常困惑。 I had been doing research and everything was talking about how multithreading can allow 2 processes to run at the same time. 我一直在做研究,所有事情都在谈论多线程如何允许两个进程同时运行。

I made this program so 3 different threads would use a for loop to count 1-10, 11-20, and 21-30 so that I could see if they actually ran at the same time how I expected them to. 我制作了这个程序,因此3个不同的线程将使用for循环对1-10、11-20和21-30进行计数,这样我就可以查看它们是否实际上按照我的期望同时运行。

After running the program the output is something like 1 2 3 4 5 6 7 8 9 10 21 22 23 24 25 26 27 28 29 30 11 12 13 14 15 16 17 18 19 20 Basically any variation order of the 3 sets of numbers. 运行程序后,输出类似于1 2 3 4 5 6 7 8 9 10 21 22 23 24 25 26 27 28 29 30 11 12 13 14 15 16 17 18 19 20 20基本上是3组数字的任何变化顺序。 So they can all be in order or have 21-30 before 11-20 sometimes. 因此,他们有时会在11-20之前处于有序状态或有21-30。 That doesn't seem like it is running at the same time, just running one after another. 似乎并不能同时运行,只是一个接一个地运行。

In the for loop in the println(i); 在println(i)中的for循环中; if i change it to println(i + "a"); 如果我将其更改为println(i +“ a”); for 1-10 and b, c for 11-20, 21-30 The output is actually in a random order like I had expected. 对于1-10和b,c对于11-20、21-30,输出实际上是随机的,就像我期望的那样。 Like this: 1a 11b 21c 2a 22c 12b 13b 23c 3a 像这样:1a 11b 21c 2a 22c 12b 13b 23c 3a

Does the program know its doing nothing but counting up and just throw all the numbers on the screen without actually doing it? 程序是否知道自己除了计数之外什么也不做,只是将所有数字扔到屏幕上而不实际执行? Or does adding the string at the end make it slow enough for the other threads to sneak in between the operations? 还是在末尾添加字符串会使字符串变慢,以使其他线程在两次操作之间潜行? I know nothing about this ha. 我对此一无所知。

public class Run{

    static Runnable updatePosition;
    static Runnable render;
    static Runnable checkCollisions;

    public static void main(String[] args) {
        // System.out.println(Runtime.getRuntime().availableProcessors());

        updatePosition = new Runnable() {

            @Override
            public void run() {

                for (int i = 1; i <= 10; i++) {
                    System.out.println(i);
                }
            }

        };

        render = new Runnable() {

            @Override
            public void run() {
                for (int i = 11; i <= 20; i++) {
                    System.out.println(i);
                }

            }

        };

        checkCollisions = new Runnable() {

            @Override
            public void run() {
                for (int i = 21; i <= 30; i++) {
                    System.out.println(i);
                }

            }

        };

        Thread updatePositionThread = new Thread(updatePosition);
        Thread renderThread = new Thread(render);
        Thread checkCollisionsThread = new Thread(checkCollisions);
        updatePositionThread.start();
        renderThread.start();
        checkCollisionsThread.start();

    }

}

Also, how do threads get assigned to CPU cores? 另外,如何将线程分配给CPU内核? In depth please, reasonably. 请深入,合理。 What I mean by asking this is: If I were to use a single thread program and use an update method and a draw method and they together took too long and made my program lag, would putting them on separate threads make this not help, or does it not actually run side by side? 我问这个问题的意思是:如果我使用一个单线程程序,并使用一个update方法和一个draw方法,并且它们在一起花费的时间太长,并使我的程序滞后,将它们放在单独的线程上将无济于事,或者它实际上不是并排运行吗? Assuming I can deal with all of the concurrency. 假设我可以处理所有并发问题。

Actually, JIT optimizations, processor architecture and several other things play a part in how these kind of situations turn out. 实际上,JIT优化,处理器体系结构和其他一些因素在这种情况下如何发挥作用。

The actual output should not be an ordered execution of threads like 1-10, 21-30, 11-20. 实际输出不应是1-10、21-30、11-20等线程的有序执行

Changing your code a little to : 将代码稍微更改为:

for (int i = 1; i <= 10000; i++)
for (int i = 10001; i <= 20000; i++)
for (int i = 20001; i <= 30000; i++)

I get (as expected, complete execution of one thread is not happening, as one might assume in your case). 我得到了(正如预期的那样,一个线程没有完全执行,正如您所设想的那样)。 It is all about how much time a thread gets. 这与线程获得多少时间有关。

1
2
3
...
250
251
252
253
20001
20002
20003
...
20127
20128
10001
10002
10003
..

Changing i to "a" + i leads to dynamic construction of new Strings using StringBuilder , this will indeed take some time (and hence CPU cycles). i更改为"a" + i会导致使用StringBuilder 动态构造新的String,这确实会花费一些时间(因此会占用CPU周期)。 On the other hand, primitive ints don't have this delay .. So, you get such an output. 另一方面, 原始int没有此延迟 ..因此,您会得到这样的输出。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM