Multithread output not expected

2019-09-21 03:45发布

问题:

I am new to Java and the concept of multi-threading. Here's my experimental code:

public class Multithread implements Runnable {

    Thread t;

    public Multithread(int prior, String name) {
        this.t = new Thread(this, name);
        this.t.setPriority(prior);
        this.t.start();
    }

    public void run() {
        for (int i = 1; i <= 5; i++) {
            if (this.t.getName().equals("thread1")) {
                System.out.println("First Child Thread Loop No " + i);
            }
            else {
                System.out.println("Second Child Thread Loop No " + i);
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }
    }

    public static void main(String[] args) {
        new Multithread(10, "thread1");
        new Multithread(7, "thread2");
    }
}

The output is:

First Child Thread Loop No 1
Second Child Thread Loop No 1
First Child Thread Loop No 2
Second Child Thread Loop No 2
Second Child Thread Loop No 3
First Child Thread Loop No 3
Second Child Thread Loop No 4
First Child Thread Loop No 4
Second Child Thread Loop No 5
First Child Thread Loop No 5

Well I expected to be a simple as this:

First Child Thread Loop No 1
Second Child Thread Loop No 1
First Child Thread Loop No 2
Second Child Thread Loop No 2
First Child Thread Loop No 3
Second Child Thread Loop No 3
First Child Thread Loop No 4
Second Child Thread Loop No 4
First Child Thread Loop No 5
Second Child Thread Loop No 5

I expect first thread to get executed always before the second thread. Please explain my output. Thanks in advance.

回答1:

The reason for that is when threads start running you cannot guess the order of their appearing, very basic threads issue - you can read: http://www.codeproject.com/Articles/616109/Java-Thread-Tutorial or http://www.vogella.com/articles/JavaConcurrency/article.html or any other basic Thread tutor. Good luck.



回答2:

Here's a model for thinking about multithreading in your program. Imagine two people, Thread1 and Thread2, working independently, in separate rooms, each at their own pace and on different coffee break schedules. Each can obtain access to a shared piece of paper and add a line to it.

Assuming no coordination other than a rule that only one can write to the paper at a time, would you have any expectation about how their lines would be interleaved?

Edit==========================================================

Computer threads do not all run at the same effective speed. One may experience different cache misses from another. Unless the system is absolutely dedicated to this one application, a state that is difficult to achieve, there will be interrupts and time sharing of the processor that may slow down one thread.



回答3:

Basic rule of threading: when a thread does its work is undefined. It could be immediately, some time later, or an hour later. There is no guarantee on anything.

If you want to learn about multi-threading in Java, you should definitely start making yourself familiar with the concurrent package. It contains ready-to-be-used tools for common problems. And more important: it comes with a way of handling multi-threaded solutions that avoid most issues in the first place through intelligent code design.