Java多线程按顺序获取输出?

时间:2018-11-24 12:23:57

标签: java concurrency

有人可以帮助我如何同步Java多线程  下面的程序,我正在学习Java中的多线程,这是一个有点复杂的主题,我在网上得到了这个问题,并试图解决它,但我不能。

    public class GoMyThread {
        public static void main(String[] args) {
            MyThread t1 = new MyThread("Louis");
            MyThread t2 = new MyThread("Mathilde");
            MyThread t3 = new MyThread("Toto");
            t1.start();
            t2.start();
            t3.start();
        }
    }


    public class MyThread extends Thread {

        public MyThread(String name) {
            super(name);

        }

        public  void run() {
        for (int i = 0; i < 3; i++)
            {
                System.out.println(getName() + " Finish , level  " + (i+1));
                if (getName().compareTo("Louis") != 0)
                {
                    try{
                        Thread.sleep(100);
                    }
                    catch (InterruptedException e){}
                }
            }
        }

    }                  
 the out put for this is of the programe changes with every run 

例如,下面的输出是其中之一

    Louis Finish , level  1
    Louis Finish , level  2
    Louis Finish , level  3
    Toto Finish , level  1
    Mathilde Finish , level  1
    Toto Finish , level  2
    Mathilde Finish , level  2
    Toto Finish , level  3
    Mathilde Finish , level  3 

  what I want is  the output to be  like ,three of them finish level 1 before passing to the next level ,But i can't achieve it no matter i try ,the 

输出必须如下所示。

    Louis Finish , level  1`
    Mathilde Finish , level  1
    Toto Finish , level  1
    Louis  Finish , level  2
    Toto Finish , level  2
    Mathilde Finish , level  2
    Toto Finish , level  3
    Mathilde Finish , level  3                                                                                  
    Louis  Finish,level  3                                                                                                   

 I  will appriciate if you give me some concepts to understand java Thread programming  too , ,Thank You! 

2 个答案:

答案 0 :(得分:1)

线程本质上是并行运行的,因此每次运行程序都会得到不同的结果是正常的行为

尽管有一些方法可以强制它们按某种顺序运行,例如t1,您将在t2完成后开始执行,等等...

  

我想要的输出是什么样,其中三个完成1级   在进入下一个阶段之前,但是无论如何我都无法实现   尝试

     

输出必须如下所示。

Louis Finish , level  1`
Mathilde Finish , level  1
Toto Finish , level  1
Louis  Finish , level  2
Toto Finish , level  2
Mathilde Finish , level  2
Toto Finish , level  3
Mathilde Finish , level  3                                                                                  
Louis  Finish,level  3

如果您愿意,给他们一些时间让其他竞争对手达到相同的水平。这是代码:

public class GoMyThread {
    public static void main(String[] args) {
            MyThread t1 = new MyThread("Louis");
            MyThread t2 = new MyThread("Mathilde");
            MyThread t3 = new MyThread("Toto");
            t1.start();
            t2.start();
            t3.start();
    }

    static class MyThread extends Thread {

        public MyThread(String name) {
            super(name);
        }

        public  void run() {
            for (int i = 0; i < 3; i++){
                    System.out.println(getName() + " Finish , level  " + (i+1));
                    try{
                        // Force a sleep to give the other threads time to reach the same level
                        Thread.sleep(500);
                    }catch (InterruptedException e) {
                        System.out.println("Something went wrong with sleep");
                    }

                    if (getName().compareTo("Louis") != 0){
                        try{
                            Thread.sleep(100);
                        }
                        catch (InterruptedException e){}
                    }
            }
        }
    }
}

结果将始终是您想要的结果,因为500毫秒的等待就像是一整天的线程等待。

答案 1 :(得分:0)

您可以对线程数使用静态计数器,并在打印级别后添加while循环以检查是否达到了循环数。您还可以使用共享的CountDownLatch,但是您必须在每个级别之后重置它(创建一个新的)。